How to make the child form follow main form.
for example: open a winform [.net2], winform opens form, form follows the mainform if mainform is moving.
Use the LocationChanged event from the MainForm to always set the location of the ChildForm.
Working example:
Form childForm = new Form();
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
childForm.StartPosition = FormStartPosition.Manual;
childForm.Width = this.Width;
childForm.Height = 96;
childForm.Location = new Point(this.Left, this.Bottom);
childForm.Show();
}
protected override void OnLocationChanged(EventArgs e) {
base.OnLocationChanged(e);
if (childForm != null) {
childForm.Location = new Point(this.Left, this.Bottom);
}
}
Superficially simple answer is just to add handlers for when Mainform is moved or resized and then set childform Location and size accordingly.
However do you say want to stop main form being moved such that childform end up off screen.
Can child form be moved independantly.
What about minimise and maximise?
Might you want other arrangements, nore than one child, left and right, child form above main form...
Be worth writing a layout class, and shoving all this stuff off to it.
Related
I have a form that i set it's Opacity as 50% like this:
this.Opacity = 0.5D; <--this==Form
My problem is that everything that on the form is with an Opacity of 50%
I have two buttons on the form and I want them without Opacity.
I know that this.Opacity included all Controls and for some reason the graphics too
My question is, How to Exclude the Opacity of the controls?
Example Image:
Thanks!
Since Control doesn't have Opacity property and plus that, most of the controls doesn't support transparent colors, then a working solution can be this:
Create a Form called MainForm and place all the controls you're going to be excluded.
1.1 Set both of BackColor and TransparencyKey properties of MainForm to the same color, e.g Color.Red
Create another form named TransparentForm and place all controls that must become transparent. Set ShowInTaskbar property to False.
In the MainForm Load event show the TransparentForm and send it to back.
private void MainForm_Load(object sender, EventArgs e)
{
TransparentForm form = new TransparentForm();
form.Opacity = 0.5D;
form.Show();
form.SendToBack();
}
The position of controls in both form must be such that, when combined, it shows the proper user interface.
Crate a C# project and add 3 forms named
MAIN
BACKGOUND
Child
and add the following code for "MAIN" form load event;
private void MAIN_Load(object sender, EventArgs e)
{
Child frm1 = new Child();
BACKGOUND frm2 = new BACKGOUND();
frm2 .WindowState = System.Windows.Forms.FormWindowState.Maximized;
frm2.Opacity = 0.5;
frm2.Show();
frm1.ShowDialog();
frm2.Close();
}
I am trying to create a menu driven application using MDI forms. My problem is that the menu in the MDI parent will create a new child every time i clicked on it. How do i allow only one particular instance of the child to be open for a particular form but allow multiple forms from different menu to be open. For example, I would want a child from "File" to be open along with an "Edit" child. Also, is there a way to close all other forms whenever a new form is open?
You can do all of that my checking the MdiChildren array of your main form. That array will list all of the open MDI children on your form.
You can determine if an instance of a form is open by looping through the array and checking if a form of the type requested is already open.
To close all open forms, just loop through MdiChildren and call Close on all of the forms.
You can check for an existing instance of your form through the MDIChildren collection:
private void form2ToolStripMenuItem_Click(object sender, EventArgs e) {
Form2 f = this.MdiChildren.OfType<Form2>().SingleOrDefault();
if (f == null) {
f = new Form2();
f.MdiParent = this;
f.Show();
} else {
f.BringToFront();
}
}
If you want to close any previous open form, you can also just go through the MDIChildren collection:
if (f == null) {
while (this.MdiChildren.Count() > 0) {
this.MdiChildren[0].Dispose();
}
// etc...
I am working on a Compact Framework application. This particular hardware implementation has a touchscreen, but its Soft Input Panel has buttons that are simply too small to be useful. There are more than one form where typed input is required, so I created a form with buttons laid out like a keypad. The forms that use this "keypad" form are modal dialogs. When a dialog requiring this "keypad" loads, I load the "keypad" form as modeless:
private void CardInputForm_Load(object sender, EventArgs e)
{
...
keypadForm = new KeypadForm();
keypadForm.Owner = this;
keypadForm.SetCallback(keyHandler);
keypadForm.Show();
}
The SetCallback method tells the "keypad" form where to send the keystrokes (as a Delegate).
The problem I'm having is that the modeless "keypad" form does not take input. It is displayed as I expect, but I get a beep when I press any of its buttons, and its caption is grayed-out. It seems like the modal dialog is blocking it.
I've read other posts on this forum that says modal dialogs can create & use modeless dialogs. Can anyone shed light on this situation? Is there a problem with my implementation?
I found the answer: Set the keypad form's Parent property, not its Owner property, to the form instance wanting the keystrokes. The keypad dialog's title bar stays grayed out, but the form is active.
private void CardInputForm_Load(object sender, EventArgs e)
{
// (do other work)
keypadForm = new KeypadForm();
keypadForm.Parent = this;
keypadForm.Top = 190; // set as appropriate
keypadForm.Show();
}
Be sure to clean up when done with the parent form. This can be in the parent's Closing or Closed events.
private void CardInputForm_Closing(object sender, CancelEventArgs e)
{
// (do other work)
keypadForm.Close();
keypadForm.Dispose();
}
There are two panels on the keypad form, one with numerals and one with letters and punctuation that I want. There is also an area not on a panel that is common to both, containing buttons for clear, backspace, enter/OK, and cancel. Each panel has a button to hide itself and unhide its counterpart ('ABC', '123', for example). I have all the buttons for input on the keypadForm fire a common event. All it does is send the button instance to the parent. The parent is responsible for determining what action or keystroke is desired. In my case I named the buttons "btnA", "btnB", "btn0", "btn1", "btnCancel", etc. For keystrokes, the parent form takes the last character of the name to determine what key is desired. This is a bit messy but it works. Any form wishing to use the keypad form inherits from a base class, defining a method for callback.
public partial class TimeClockBase : Form
{
public TimeClockBase()
{
InitializeComponent();
}
// (other implementation-specific base class functionality)
public virtual void KeyCallback(Button button)
{
}
}
The click event on the keypad form looks like this.
private void btnKey_Click(object sender, EventArgs e)
{
// play click sound if supported
(Parent as TimeClockBase).KeyCallback(sender as Button);
}
The method in the parent form looks like this.
public override void KeyCallback(Button button)
{
switch (button.Name)
{
case "btnCancel":
// setting result will cause form to close
DialogResult = DialogResult.Cancel;
break;
case "btnClear":
txtCardID.Text = string.Empty;
break;
// (handle other cases)
}
}
I am working on a application in which I have to show different forms stacked one upon the other. Due to some restrictions, I cannot use MDI and also it has a lot of issues.
I am able to get what I want but with a problem. The forms will be stacked, but they do not remain in the parent form.
Lets take it by an example. The structure goes like this.
1) There is a form A (My parent form)
2) a second form "B" opens on a button click event on Form "A". (Note: B.ShowInTaskBar=False)
3) again, a third form "C" opens on a button click event on Form "B". (Note: C.ShowInTaskBar=False)
Now, when I minimize form A, it gets minimized but the Form B and C, remains as it is. I want them to get minimized at the same time. I want form B and C should remain as a child form of form A.
How to get that.
Just use MDI forms. There is no technical restriction about FormBorderStyle's value for MDI children. Remember to set the IsMdiContainer property to true for the parent form and then set the child form's MdiParent property to the parent form before Show() is called.
Edit:
I'm not exactly sure what you mean by stacking. You can easily control the child positions if that is what you mean:
public void ShowChildren()
{
Child child1 = new Child();
Child child2 = new Child();
child1.MdiParent = this;
child2.MdiParent = this;
child1.Show();
child2.Show();
child1.Size = new System.Drawing.Size(100, 100);
child1.Location = new System.Drawing.Point(0, 0);
child2.Size = new System.Drawing.Size(100, 100);
child2.Location = new System.Drawing.Point(0, 100);
}
Edit #2:
Are you trying to nest the forms? If so you can make the parent a normal form and place a UserControl A in the parent. Then place UserControl B in UserControl A. Allowing the user to move these around becomes more difficult, but if you already wanted no border this may not be an issue for you.
public void formMain_buttonShowA_click() {
FormA formA = new FormA();
formA.ShowDialog();
}
public void formA_buttonShowB_click() {
FormB formB = new FormB();
formB.ShowDialog();
}
i am facing one problem in SCSF.
I have two workspaces
MdiWorkspace
DeckWorkspace
i have two views in a module
Viewer (display in mdiworkspace)
Property Viewer (in deckworkspace)
in Viewer i have a button in toolbar whose purpose is to display PropertyViewer (another View).
how can i display this PropertyViewer in deckworkspace agaist button click event.
NOTE: i am not using Command[CommandName].AddInvoker(control, "click:) and CommandHandler
I'm going to assume your toolbar sits in a SmartPart that implements the MVP pattern. Have the button click event handler in the SmartPart fire an event that its presenter will handle. Your presenter code would look like this:
// Presenter code
protected override void OnViewSet()
{
this.View.ToolbarButtonClick += View_ToolbarButtonClick;
}
public void View_ToolbarButtonClick(object sender, EventArgs e)
{
// remove the handler so the property viewer
// will only be added the first time
this.View.OnToolbarButtonClick -= View_ToolbarButtonClick;
var propertyView = new PropertyViewer();
this.WorkItem.Workspaces[WorkspaceNames.MyDeckWorkspace].Show(propertyView);
}