Problem with Form
-
Hi guys,I have a program that starts with a menu form. It will create a new form and remove the menu form after the user pressed a certain button. So I tried the following method :
private void btn2Player_Click(object sender, EventArgs e)
{
frmMenu frmMenu = new frmMenu();// menu form
frm2Players frm2P = new frm2Players();//new form
frm2P.Show();//display new form
frmMenu.Close();//Close the menu form
}The new form that it is supposed to create shows up but strangely the menu form is still there. What should I do ?
Lim Yuxuan wrote:
frmMenu frmMenu = new frmMenu();// menu form
Remove this line since it is not the current instance of menu form.
Lim Yuxuan wrote:
frmMenu.Close();//Close the menu form
Replace this line by:
this.Hide();
Do not usethis.Close()
since it will also dispose objectfrm2P
and second form would be closed as well.It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
modified on Monday, September 14, 2009 10:07 AM
-
Hi guys,I have a program that starts with a menu form. It will create a new form and remove the menu form after the user pressed a certain button. So I tried the following method :
private void btn2Player_Click(object sender, EventArgs e)
{
frmMenu frmMenu = new frmMenu();// menu form
frm2Players frm2P = new frm2Players();//new form
frm2P.Show();//display new form
frmMenu.Close();//Close the menu form
}The new form that it is supposed to create shows up but strangely the menu form is still there. What should I do ?
where are you calling your initial frmMenu form from? I think your problem is you are creating frm2Players from within frmMenu which means when that closes it will close all children. What you want to do is call frmMenu from wherever it is you call that now, and have that return a value (suggested through a property), when btn2Player is clicked then the property is set and the frmMenu is closed - the parent form/class can then use that property to determine what form to open next. something like...
if(frmMenu.ShowDialog == DialogResult.OK)
{
switch(frmMenu.OptionProperty)
{
case 1://1 player
frm1Player frm1P = new frm1Player();
frm1P.Show();
break;
case 2://2 player
frm2Players frm2P = new frm2Players();
frm2P.Show();
break;
}}
Life goes very fast. Tomorrow, today is already yesterday.
-
where are you calling your initial frmMenu form from? I think your problem is you are creating frm2Players from within frmMenu which means when that closes it will close all children. What you want to do is call frmMenu from wherever it is you call that now, and have that return a value (suggested through a property), when btn2Player is clicked then the property is set and the frmMenu is closed - the parent form/class can then use that property to determine what form to open next. something like...
if(frmMenu.ShowDialog == DialogResult.OK)
{
switch(frmMenu.OptionProperty)
{
case 1://1 player
frm1Player frm1P = new frm1Player();
frm1P.Show();
break;
case 2://2 player
frm2Players frm2P = new frm2Players();
frm2P.Show();
break;
}}
Life goes very fast. Tomorrow, today is already yesterday.
where are you calling your initial frmMenu form from?
I think your problem is you are creating frm2Players from within frmMenu which means when that closes it will close all children.Yup. That is what happens when I tried to execute the .Close() method. I tried d@nish 's method and it worked the way I wanted it to be. Thanks anyway :)
-
Lim Yuxuan wrote:
frmMenu frmMenu = new frmMenu();// menu form
Remove this line since it is not the current instance of menu form.
Lim Yuxuan wrote:
frmMenu.Close();//Close the menu form
Replace this line by:
this.Hide();
Do not usethis.Close()
since it will also dispose objectfrm2P
and second form would be closed as well.It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
modified on Monday, September 14, 2009 10:07 AM
Thanks it worked the way I wanted it to be.
-
Thanks it worked the way I wanted it to be.
-
where are you calling your initial frmMenu form from?
I think your problem is you are creating frm2Players from within frmMenu which means when that closes it will close all children.Yup. That is what happens when I tried to execute the .Close() method. I tried d@nish 's method and it worked the way I wanted it to be. Thanks anyway :)
No problem, but may I say that the apparent quick fix you have chosen may not be the best option. Will you need to display the frmMenu again at any point? Out of interest, I assume you are making a game or some sort? any particular reason to use windows forms?
Life goes very fast. Tomorrow, today is already yesterday.
-
No problem, but may I say that the apparent quick fix you have chosen may not be the best option. Will you need to display the frmMenu again at any point? Out of interest, I assume you are making a game or some sort? any particular reason to use windows forms?
Life goes very fast. Tomorrow, today is already yesterday.
-
Yes. btn2Player calls a new form and after calling the new form, it will remove the old one (menu form)
As ElliotA has said you are creating a new menu form and closing that, rather than the original one as you intend. To do what you want, change your code to something like:
private void btn2Player\_Click(object sender, EventArgs e) { // frmMenu frmMenu = new frmMenu();// menu form // <=============== Delete this line \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* frm2Players frm2P = new frm2Players();//new form frm2P.Show();//display new form // frmMenu.Close();//Close the menu form // <=============== Change this line \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* this.Close();//Close the menu form }
However, you said that the menu form is the first form in your application, so if you close it, you will close your whole application, including the new
frm2Players
that you have just created.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Application.OpenForms collection would help out then.
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
Yeah maybe, I think it depends on the usage of the menu form, if its a 'main menu' kind of form then it should be re-opened when the game form is done, unless a complete quit selection of some sort is made... though, you could of course use your method to show the menu form again. I guess it's just preference at the minute (maybe i'll think of a good reason either way later ;) )
Life goes very fast. Tomorrow, today is already yesterday.
-
No problem, but may I say that the apparent quick fix you have chosen may not be the best option. Will you need to display the frmMenu again at any point? Out of interest, I assume you are making a game or some sort? any particular reason to use windows forms?
Life goes very fast. Tomorrow, today is already yesterday.
Yup Its a tic-tac-toe. I am using labels for the "O" and "X". But. I think I will use MDI instead because I could not figure out where to put the
frmMenu.Show();
code to show the menu form once i close the game form (I could not find any event handler that triggers when you close the form). EDIT:
Application.OpenForms collection would help out then.
oh wait ...
modified on Monday, September 14, 2009 7:26 PM
-
Application.OpenForms collection would help out then.
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
Erm.. where should I put the
Application.OpenForms[0].Show();
code ? I cant find an event handler that triggers when you closes a form.
-
Yup Its a tic-tac-toe. I am using labels for the "O" and "X". But. I think I will use MDI instead because I could not figure out where to put the
frmMenu.Show();
code to show the menu form once i close the game form (I could not find any event handler that triggers when you close the form). EDIT:
Application.OpenForms collection would help out then.
oh wait ...
modified on Monday, September 14, 2009 7:26 PM
-
There is a
Form_Closing
event. You can make use of that.It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
thanks
-
Hi guys,I have a program that starts with a menu form. It will create a new form and remove the menu form after the user pressed a certain button. So I tried the following method :
private void btn2Player_Click(object sender, EventArgs e)
{
frmMenu frmMenu = new frmMenu();// menu form
frm2Players frm2P = new frm2Players();//new form
frm2P.Show();//display new form
frmMenu.Close();//Close the menu form
}The new form that it is supposed to create shows up but strangely the menu form is still there. What should I do ?
Hi, you can't close it. because 'frm2players' is one child form of the 'frmMenu'; Give you two : 1) hide the 'frmMenu'; use 'frmMenu.visible = false;' or 'frmMenu.Hide();' 2) Create one Thread to create 'frm2Players'; /********************/ 'Thread td = new thread(method);' // method is 'create frm2player' td.start(); /********************/ note: thread is 'light', so can't have Resources as 'Main thread'.
modified on Tuesday, September 22, 2009 2:13 AM
-
Hi guys,I have a program that starts with a menu form. It will create a new form and remove the menu form after the user pressed a certain button. So I tried the following method :
private void btn2Player_Click(object sender, EventArgs e)
{
frmMenu frmMenu = new frmMenu();// menu form
frm2Players frm2P = new frm2Players();//new form
frm2P.Show();//display new form
frmMenu.Close();//Close the menu form
}The new form that it is supposed to create shows up but strangely the menu form is still there. What should I do ?
you can't close it. because 'frm2players' is one child form of the 'frmMenu'; Give you two : 1) hide the 'frmMenu'; use 'frmMenu.visible = false;' or 'frmMenu.Hide();' 2) Create one Thread to create 'frm2Players'; /********************/ 'Thread td = new thread(method);' // method is 'create frm2player' td.start(); /********************/ note: thread is 'light', so can't have Resources as 'Main thread'.