I'm facing a problem with form show. I have a main form where I have my GUI and I choose an option that creates an instance of a form. For example in my main form I have:
Form2 f2 = new Form2();
f2.Show();
The problem is that the form shows for about 1-2 secs and then goes behind the main form.
I tried some instructions in the main form below f2.Show() command like
f2.BringtoFront();
this.SendtoBack();
Also I added commands to the new form (Form2) load method:
this.BringtoFront();
this.Activate();
this.Focus();
Nothing of the above commands seems to be a solution for this. Only when I use f2.ShowDialog(); instruction in my main form but I don't want to do that because I need immediate access to both of these forms at the same time.
Any help? Thanks
If you don't want your second form to never go behind your main form then pass the owner in the overload of Show method that accepts the owner parameter
Form2 f2 = new Form2();
f2.Show(this); // Assuming this code runs inside the main form
If you remove or change it to comment this.SendtoBack(); :
f2.BringtoFront();
//this.SendtoBack();
it'll be OK!
Related
I have 2 winforms in an application in visual studio. The first one is kind of a loading screen, making sure there are no connection problems, then a second winforms opens, which would be a login form. I use this method to close the first form and open the second one:
this.Close();
th = new Thread(opennewform); // [opennewform thread: Application.Run(new Login());]
th.SetApartmentState(ApartmentState.STA);
th.Start();
and it works fine, but when the second form opens it loses focus. I've tried adding this.Activate, this.BringtoFront, this.Show to the second form, but it doesn't work. And what I need is after the first form closes and the second one opens, this second form is the "active" form, so it always pops up. I also needed to use the close/thread method so that the first form really closes and not just this.hide(); but actually stays in the background. Thanks in advance for the help.
You do not need to create a new thread. Instead, change the start code to something like this
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form1 = new Form1();
form1.Show();
Application.Run();
}
I.e., remove the first form from Application.Run(), so that the application does not exit when this form closes.
Then open the second form with
Close();
var form2 = new Form2();
form2.Show();
Now, the application will not exit automatically when you close any form. Therefore, you must exit it explicitly with Application.Exit();, e.g. in the FormClosed event of the second form.
try:
Form2 f2 = new Form2();
this.Hide();
f2.ShowDialog();
this.Close();
I have facing a problem in my application.
I have a MainForm which is MDIPARENT
and a login form which is MDICHILD
the problem which i was facing is when
i login as a user with correct details
then my login form is dispose. the problem
is when login is dispose i want to open another
form F2 which is mdichild. following code doesn't work
at all.
Form1 F1 = new Form();
F1.mdiparent = this;
F1.show();
the code above runs a new instance of a form and doesn't
make it mdi.
You need to show the MDIParent form first then at the Form_Load event show the login form using ShowDialog method. Once login is successful the login form will unload it self since it is just a Dialog form. Then instantiate the other form you want to show.
I have a Main form.I want to launch another form from it and launch another from the launched form.I want to ensure that the Main form is not editable when sub forms are displayed so i use showdialog()
Mainform>(Showdialog)>form1>(showDialog+dispose)>form2(dispose)>Mainform
From Mainform i call form2.ShowDialog() then from form2 i use the following code to launch another form
this.visible=false;
form3.showdialog();
this.dispose();
But there are some problems in this.Is there a better way to achieve what im looking for?
edit:more description
I have a Main form,User clicks a Button on Mainform>Form1 is lauched>User clicks a Button in Form1>Form 2 is lauched(diposing/hiding form1) after form2 is closed Mainform should be brought to front and made editable,until then all other forms should be on top of Mainform and Mainform should be un-editable
The problem is that you have to specify the MainForm as the parent for (both) form2 and form3. When you use the overload of ShowDialog that has no parameters, WinForms uses the active form as the parent, so form3's parent becomes form2 automatically. You are then trying to close/dispose form2 causing form3 to become orphaned.
There are several options for getting the reference to MainForm, but the simplest is to use:
form2/3.ShowDialog(Application.OpenForms["MainForm"]);
Assuming that you have set the Name property on MainForm to "MainForm".
In your code, this.dispose() is executed only after form3 is closed. What i think you want is to close form2 after form3 was closed, so you can call this.Close() instead of this.Dispose().
this.visible=false;
form3.showdialog();
this.Close();
Or maybe, after form3 is shown up you dont need form2 any more. That meas:
this.visible=false;
//show instead of showdialog so it wont wait until form3 is closed
form3.show();
this.Close();
It looks like you are trying to implement something like a wizard. The best solution would be to launch all the child forms sequentially, in the main form.
If you need to pass data along the sequence, you should pass it from each dialog to the main form, which then passes it to the next dialog.
MainForm:
Form1 f = new Form1();
if (f.ShowDialog(this) == DialogResult.OK) {
Form2 f2 = new Form2();
f2.ShowDialog(this);
}
Form1 (button click which will open the form 2):
button1_click(object sender, EvengArgs e) {
this.DialogResult = DialogResult.OK;
Close();
}
everyone.
How do I call a number of forms one after another (meaning call new form after previous one is closed).
I wrote something like this;
Form1.Show();
Form2.Show();
and it naturally results in opening both forms simultaneously. How to make function to wait for the fist form to be closed?
Open one form, handle the FormClosed event, and open the second form from within that. Note that this is only necessary because your forms are not modal. If you were using ShowDialog() the call would not return until the first form was closed and your code would work as it is currently structured.
If you require non-modal behavior (i.e., your user must be able to interact with the owner form while the owned form is open) then use something like this:
// very simplistic example...
Form1 frm = new Form1();
frm.FormClosed += delegate { new Form2().Show(); }
frm.Show();
I have two forms named frmRegistration & frmMain in my project in c#.
I have set frmRegistration as my start form.
User enters data in the frmRegistration form & presses submit button to get registered. Then, I want to close frmRegistration form & show frmMain form to the user.
I'm trying this by using Dispose() method of the frmRegistration. But, when I use this method, it disposes all my application execution because frmRegistration is the startup form.
I don't want this to happen. Can anyone solve this problem?
thanks.
Use Show() and Hide() methods.
private void btnSubmit_Click(object sender, EventArgs e)
{
...
var frm = new frmMain();
frm.Location = this.Location;
frm.StartPosition = FormStartPosition.Manual;
frm.Show();
this.Hide();
}
UPDATE:
If you don't want to have frmRegistration in memory, start your program in main form and add this in your MainForm's Shown event:
var frm = new frmRegistration();
frm.Location = this.Location;
frm.StartPosition = FormStartPosition.Manual;
frm.FormClosing += delegate { this.Show(); };
frm.Show();
this.Hide();
Now you can just close the registration form and automatically get back to main form.
Try setting frmMain as start up form and hiding it initialy, show frmRegistration, do what you have to do, and Dispose it.
You can also change you Program.cs main class with the Main() function to start frmRegistration and after positive DialogResult or another check it will then start with frmMain - as your main form and message loop.
At least there are two options:
1.Turn your Start up form into a singleton
When you need to hide it, call it's hide method
2.Have new different startup form, call it MainApp form or whatever, have it set to invisible, the you can do what ever you like with the other non-startup forms.