I have two forms, Form1 and Form2. I currently have a button in the startup form(Form1) that loads up Form2. What I would like to do is have Form1 become minimized and only show Form2 once it is told to load so that Form1 is not in the background while Form 2 is shown.
So far I have tried things such as:
this.WindowState = FormWindowState.Minimized;
but the problem I'm having is that it causes Form2 to become minimized as well. Is there a way to specifically cause only Form1 to be affected or work around this problem?
below you will find my code, I may have made some mistakes!
Form2 add = new Form2();
this.WindowState = FormWindowState.Minimized;
add.ShowDialog();
I had also tried putting the windowstate behind the showdialog but it may not work because it will only execute after the dialog is done working.
Your issue is due to two things.
You're using ShowDialog which launches Form2 as a modal dialog. Hint: Go read about modal dialogs
You need to minimize after showing Form2
Change your code to this:
var add = new Form2();
add.Show();
WindowState = FormWindowState.Minimized;
Related
I have created the multiple windows form in C#.For EX. We have two forms and we have next button in Form1 and previous button in Form2 . But when we try to go form Form2 to Form1 using previous button Form2 is not closing and Form1 is appearing on Form2. To show forms we are using ShowDialog. So how can we create the form one linked to another as pages.
What you can do is like this:
In form1's button handler,
form2 f2=new form2();
f2.show();
this.hide();
And in Form2's button handler,
form1 f1=new form1();
f1.show();
this.hide();
But make sure you write proper code in close button handler as the forms are just hidden. Not closed.
It looks like you're trying to implement a "wizard" in your application.
The more typical approach is to have each "page" as a control/user control and load the appropriate step in the same dialog as the user moves along.
You may benefit from looking at some examples leveraging existing libraries to make this simpler. One example is here: https://www.codeproject.com/articles/120607/simple-wizard-for-winforms
This answer also covers this topic and provides a few more resources:
Which wizard control can I use in a WinForms application?
I have a form with an MDI container marked as true. This form is a parent form which has a child form. In the child form I use this code for loading it in maximized size.
this.WindowState = FormWindowState.Maximized;
I use this codes for showing the child form :
Form2 f2 = new Form2();
f2.MdiParent = this;
f2.Show();
And also child form is fixed single so that it won't be able to resize. But however i disable maximize button it will disapper and can be restore down.
Try this:
f2.MinimizeBox = false;
You can try
f2.ControlBox = False;
That could hide both the MaximumBox and MinimumBox but not the Close Button.
Had the same problem, what ended up doing the trick was handling the OnMove event and resetting the visibilities there (even though they are already set to false). Not sure why that works, but it did for me.
I have two open winforms, say that winform1 gets a modal dialog, this means that all forms(winform1 and winform2) will be "disabled". If we minimize all forms and then bring up winform1, then the modal dialog will be shown above it. If we again minimize all the forms but this time brings up winform2, it will look like the finform2 is ready to be used while its really disabled like winform1.
What I need is to clearly show that the modal dialog needs to be handled before using winform2 again.
Is there anything built in to handle this or am I on my own here?
In your winform2.Activated event handler, call this:
static void FocusModalForm()
{
foreach (Form form in Application.OpenForms)
if (form.Modal)
{
form.WindowState = FormWindowState.Normal;
form.BringToFront();
}
}
e.g.
Form f2 = new Form();
f2.Activated += (_, __) => FocusModalForm();
f2.Show();
You may need to do the same thing for winform1's Activated event. It depends how winform2 gets created. Just try it and if you find that winform1 (or any other nonmodal form) is still able to get in front of the modal form, just call FocusModalForm() from its Activated event.
I tried this in Windows 7. I tried hiding all windows (click the Show Desktop button on the taskbar) and then selecting form2 directly from the taskbar, and I also tried just selecting form2 from the taskbar without hiding all windows. Form3 always stayed on top.
I have a similiar app (vb.net) where win1 calls win2 & win2 displays win3 and it works as you would like but wins 2 & 3 are both modal. I don't know if that is why it works or not. Perhaps that is an option for you?
If you do this:
var winform2 = new Winform2();
winform2.Show(winform1);
Then winform2 will always be shown above winform1, but it won't be modal. May be this can help you.
From main form (Form1) I am calling to show another form(Form2).
but I want it to show exactly the same place and size that form1 is, so that we wont be able to see form1 anymore until we either close form2 or move it somewhere else.
so I wrote these lines:
Form2 f2 = new Form2();
f2.Left = this.Left;
f2.Top = this.Top;
f2.Size = this.Size;
f2.Show();
But it still has problems. form2 us not completely on form1. any other thing I should add to the code?
Yes, you are doing this the wrong way around. The actual size of the form is only the same as the design size if the machine you run this on has the exact same user preferences, system font size and video DPI setting. If it is off a lot then the DPI setting is different. If it is off a little then the user preferences are different. Like a larger title bar font or bigger buttons. Fix:
Form2 f2 = new Form2();
f2.Show();
f2.Left = this.Left;
f2.Top = this.Top;
f2.Size = this.Size;
If that's too noticeable then you should let the Form2's Load event do this. Pass a reference to your main form or use the Owner property and Show(owner). In other words:
Form2 f2 = new Form2();
f2.Show(this);
in Form2:
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
this.Location = Owner.Location;
this.Size = Owner.Size;
}
If you don't want the user to interact with Form1 until Form2 is closed, then change your last line to
f2.ShowDialog();
Then it doesn't matter if the user can still see Form1. Windows won't let Form1 get the focus again until Form2 is closed.
What will happen when show your Form2 with the same size and same locations with Form1? Form1 will be invisible right? So, why you dont use form1.Hide(); ?
try this...
Form2 f2 = new Form2();
f2.Show();
f2.SetBounds(this.Location.X, this.Location.Y,this.Width, this.Height);
//this.Hide(); // if you want to hide 1stform after showing 2nd form
In your form properties for form2, set it to "center on parent"
If the forms are the same size, then this will place form2 over form1 and form 1 is not accessible. Still open the form using modal (form2.ShowDialog()) so focus stays with form2 even if the user moves form two manually.
You will still be able to move form2 like I just mentioned off of form one, but that was not specified as part of this question.
When I want to Display a form (C#) by clicking a button in another form I usually create an object from the form that I want to show and use the show method :
Form2 f2 = new Form2();
f2.Show();
or I work with the "Owner" :
Form2 tempForm = new Form2();
this.AddOwnedForm(tempForm);
tempForm.Show();
the two ways generate the same results but what is the best and what are the differences between them?
The only difference apart from the naming is that in the second you call AddOwnedForm, and in the first you do not. Looking at the documentation we see:
When a form is owned by another form, it is minimized and closed with the owner form. For example, if Form2 is owned by form Form1, if Form1 is closed or minimized, Form2 is also closed or minimized. Owned forms are also never displayed behind their owner form. You can use owned forms for windows such as find and replace windows, which should not be displayed behind the owner form when the owner form is selected.
So if you want this behavior of the forms being minimized together, and one always being shown above the other, use AddOwnedForm. If you don't want this behavior, don't use it.
Microsoft uses Form f = new Form(); f.Show(); by default when creating a new Windows Forms project to show the main form, and there is probably negligible difference (in performance) between such methods. Using the Show() method, instead of just setting f.Visible = true; is also more logical.
When you use AddOwnedForm() you essentially lock the forms together in such way that when one form is minimized, the other is also. The form is also always displayed on top of the owning form, similar to a modal dialog.