How to switch forms in C# using a button event
I have some code here:
private void button1_Click(object sender, EventArgs e)
{
Application.Run(new Form3());
}
Although I don't think this is how you are meant to change forms, when I ran it, it threw an error stating:
Starting a sec开发者_如何转开发ond message loop on a single thread is not a valid operation
You cannot use Application.Run
- that is for starting windows form application (internal message loop which is shared among all forms in the application), not for showing a form. Each form has Show
and Hide
method so you should simply call:
private void button1_Click(object sender, EventArgs e)
{
Form3 f = new Form3(); // This is bad
f.Show();
}
But you should not create form each time you want to show it. If you want to have only one instance of the form you should keep it as global and only show or hide it on demand. Otherwise you will have to call Close
instead of Hide
to clear all resources the form consumes.
you can do as simple as it is :
test mp = new test();
mp.Text = " Welcome Mr." + textBox1.Text;
this.Hide();
mp.ShowDialog();
this.Close();
where test is your new form and then if you wona pass variables to the other form just make you variable as public then you can make :
mp.Text = " Welcome Mr." + textBox1.Text;
then showdialog to show your new form and close the old form..
Your first form should create the second form on the first usage and hide itself from view. Then the second form will hide itself and show the first form when you switch back. Something like this...
private Form _firstForm;
private Form _secondForm;
private void switchToSecond_Click(object sender, EventArgs e)
{
if (_secondForm == null)
_secondForm = new SecondForm();
Hide();
_secondForm.Show();
}
private void switchToFirst_Click(object sender, EventArgs e)
{
Hide();
_firstForm.Show();
}
You would assign to firstForm the this value inside the constructor of the first form class and because this is the main form it will always be created at application startup. If you want to save resources you could also dispose of the second form when you switch back to the first and so recreate it each time you switch.
this.Hide(); // Hide the current form.
Menu menu = new Menu(); // Create new instance of the new form.
menu.Show(); // Show it
Assume you have Form1
and Form2
Your scope is not clear if you want
Switch between
Form1
andForm2
and keep both of them alive.Keep only either
Form1
orForm2
at a time.
for case 1 simple Form1.Hide() and Form2.Show()
would be adequate.
for case 2 the solution will be a little bit complicated.
- Create your own instance of
ApplicationContext MyAppCxt= new ApplicationContext()
. - instead of the default
Program.Run(Form1)
callProgram.Run(MyAppCxt(Form1))
in the handler of some event, when you need to switch the
Form1
andForm2
Form2 f2 = new Form2();//Create the new form MyAppCxt.MainForm = f2;//set Form2 as MainForm in the message queue Form2.Close()//Close Form2 and release all the resource F2.Show();//display Form2 to the user
the same scenario would be repeated when required to switch back between Form2
and Form1
精彩评论