form.close() not working
-
Hi, I am using visual studio team suite 2005 for developing a windows app in C# I have 2 forms Form1 and Form2 I have a button control on Form1 called Bttn. In the click event of the button i am writing the following code so that i close the current Form i.e. Form1 and want to show Form2. MY code:: Form1 f = new Form1(); f.Close(); Form2 r = new Form2(); r.Visible = true; but with this i have 2 problems 1) Form1 is not closed and Form2 is open i.e. both forms are opened (f.close() is not working) 2) After running the app, when i close Form1 Strangely Form2 is also closed. but however if i close Form2 only that form2 is closed as expected. I am not implementing any mdi forms concept. Any help is appreciated thanks red yojimbo
-
Hi, I am using visual studio team suite 2005 for developing a windows app in C# I have 2 forms Form1 and Form2 I have a button control on Form1 called Bttn. In the click event of the button i am writing the following code so that i close the current Form i.e. Form1 and want to show Form2. MY code:: Form1 f = new Form1(); f.Close(); Form2 r = new Form2(); r.Visible = true; but with this i have 2 problems 1) Form1 is not closed and Form2 is open i.e. both forms are opened (f.close() is not working) 2) After running the app, when i close Form1 Strangely Form2 is also closed. but however if i close Form2 only that form2 is closed as expected. I am not implementing any mdi forms concept. Any help is appreciated thanks red yojimbo
red60mans wrote:
Form1 f = new Form1(); f.Close();
You've never shown this form, Close will do nothing.
red60mans wrote:
Form2 r = new Form2(); r.Visible = true;
This doesn't work, you need to call ShowDialog(), or Show() for a modeless form.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
red60mans wrote:
Form1 f = new Form1(); f.Close();
You've never shown this form, Close will do nothing.
red60mans wrote:
Form2 r = new Form2(); r.Visible = true;
This doesn't work, you need to call ShowDialog(), or Show() for a modeless form.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Form1 is shown from from Main as follows main() { Application.Run(new Form1()); } buttonClick event on form1 { Form1 f = new Form1(); f.Close(); Form2 r = new Form2(); r.Visible = true; ) This ia modeless form and also am not using any mdi forms concept I tried r.show() also its not working yojimbo
-
Form1 is shown from from Main as follows main() { Application.Run(new Form1()); } buttonClick event on form1 { Form1 f = new Form1(); f.Close(); Form2 r = new Form2(); r.Visible = true; ) This ia modeless form and also am not using any mdi forms concept I tried r.show() also its not working yojimbo
How many times are you creating a Form1 instance ? :)
Luc Pattyn
-
Form1 is shown from from Main as follows main() { Application.Run(new Form1()); } buttonClick event on form1 { Form1 f = new Form1(); f.Close(); Form2 r = new Form2(); r.Visible = true; ) This ia modeless form and also am not using any mdi forms concept I tried r.show() also its not working yojimbo
red60mans wrote:
Application.Run(new Form1());
This is irrelevant
red60mans wrote:
Form1 f = new Form1(); f.Close();
If you want to close the current instance of Form1, call this.Close(). Creating a new instance of form1 has no bearing on any other instance that exists.
red60mans wrote:
Form2 r = new Form2(); r.Visible = true;
This plain won't work, as I said.
red60mans wrote:
This ia modeless form
Your main form cannot be modeless.
red60mans wrote:
I tried r.show() also its not working
Define 'not working' ? If you call Show(), you should make your form a member variable. Only forms for which you call 'showdialog' should be created in the scope of a method.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Form1 is shown from from Main as follows main() { Application.Run(new Form1()); } buttonClick event on form1 { Form1 f = new Form1(); f.Close(); Form2 r = new Form2(); r.Visible = true; ) This ia modeless form and also am not using any mdi forms concept I tried r.show() also its not working yojimbo
I should add, your main form must continue to exist, so you need to hide it, if that's what you want, not close it. That will close your program. Also, you may want to define the two forms as controls, then you can hide/show controls if you want a UI that jumps between two forms.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
I should add, your main form must continue to exist, so you need to hide it, if that's what you want, not close it. That will close your program. Also, you may want to define the two forms as controls, then you can hide/show controls if you want a UI that jumps between two forms.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
i am a newbie so bear with me. Form1:: public void Bttn_Click(object sender, EventArgs e) { Form2 r = new Form2(); r.Show(); this.Hide(); } This works fine for this form ie Form1 as u have said that it is not necessary to create one more instance of Form1 and rather use this.hide(). but i have one more event in Form2 called Form2Button_click event where i need to hide form2 and show Form1. Now if i create a Form1 instance as shown below i will have two instances of Form1. One which was hidden using previous code and Second instance in the Form2 button click event. public Form2Button_click(object sender, EventArgs e) { Form1 newInstanceForm1 = new Form1(); newInstanceForm1 .Show(); this.Hide(); } 1)How do i access Form1 from Form2 without creating a new instance of Form1?? or how do i get hold of the original Form1 instance from Form2 2)You have also said that it is better to create two Forms as controls......where do i create those controls?? i have only two forms Form1 being the main form. So do i need to create these forms as controls in main()?? i am just confused Any help is appreciated, yojimbo
-
i am a newbie so bear with me. Form1:: public void Bttn_Click(object sender, EventArgs e) { Form2 r = new Form2(); r.Show(); this.Hide(); } This works fine for this form ie Form1 as u have said that it is not necessary to create one more instance of Form1 and rather use this.hide(). but i have one more event in Form2 called Form2Button_click event where i need to hide form2 and show Form1. Now if i create a Form1 instance as shown below i will have two instances of Form1. One which was hidden using previous code and Second instance in the Form2 button click event. public Form2Button_click(object sender, EventArgs e) { Form1 newInstanceForm1 = new Form1(); newInstanceForm1 .Show(); this.Hide(); } 1)How do i access Form1 from Form2 without creating a new instance of Form1?? or how do i get hold of the original Form1 instance from Form2 2)You have also said that it is better to create two Forms as controls......where do i create those controls?? i have only two forms Form1 being the main form. So do i need to create these forms as controls in main()?? i am just confused Any help is appreciated, yojimbo
The easiest way of doing that is to modify your Form2 constructor to take a Form1 reference, that you then keep until you need it. Form1:
public void Bttn_Click(object sender, EventArgs e)
{
Form2 r = new Form2(this);
r.Show();
this.Hide();
}Form2:
private Form1 myf1;
public Form2(Form1 f1)
{
...
myf1 = f1;
}
public Form2Button_click(object sender, EventArgs e)
{
myf1.Show();
this.Hide();
}Internet - the worlds biggest dictionary
-
i am a newbie so bear with me. Form1:: public void Bttn_Click(object sender, EventArgs e) { Form2 r = new Form2(); r.Show(); this.Hide(); } This works fine for this form ie Form1 as u have said that it is not necessary to create one more instance of Form1 and rather use this.hide(). but i have one more event in Form2 called Form2Button_click event where i need to hide form2 and show Form1. Now if i create a Form1 instance as shown below i will have two instances of Form1. One which was hidden using previous code and Second instance in the Form2 button click event. public Form2Button_click(object sender, EventArgs e) { Form1 newInstanceForm1 = new Form1(); newInstanceForm1 .Show(); this.Hide(); } 1)How do i access Form1 from Form2 without creating a new instance of Form1?? or how do i get hold of the original Form1 instance from Form2 2)You have also said that it is better to create two Forms as controls......where do i create those controls?? i have only two forms Form1 being the main form. So do i need to create these forms as controls in main()?? i am just confused Any help is appreciated, yojimbo
red60mans wrote:
How do i access Form1 from Form2 without creating a new instance of Form1??
You can't create a new instance, it won't access the old one. The Application object does have a collection of open forms, you could find it there. You could also pass it in, as has been suggested. You could use a delegate to make Form2 tell form1 to wake up. Your best bet IMO is still to create both forms as controls, and embedd them on the one form.
red60mans wrote:
where do i create those controls??
You create new user controls, and design them the same as you would a form. Then you put both controls on your form, and write some plumbing code to make sure one or the other is always visible.
red60mans wrote:
So do i need to create these forms as controls in main()??
You'd create them as controls in the designer, and then add them to your form
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog