Ive actually searched for this and i dont understand a thing. I even tried coding this but im still confused. How will Form1 detects if Form2 closes? I dont get how the Formclose event works and linking it to Form1. I was hoping someone will clarify this
If your are opening Form2 from inside Form1 you can subscribe to the Form2 closing event inside Form1 (I'm not 100% sure how this works)
Pseudo-code:
Start Form2 inside Form1 using Form2.Show
Form2.OnClosing (or Form2.OnClosed) += Form2ClosedEventHandler
Create function inside Form1:
Form2ClosedEventHandler
Do your work here.
If you don't want to work with EventHandlers, you could open Form2.ShowDialog instead of Form2.Show, from Form1.
Any code you put after Form2.ShowDialog will run after Form2 has closed.
Related
I have two forms in my c# windows project . The first form is called is called "loginform" and the other one is called "mainform" . I want to move to the mainform on button click But when i reached at the mainform , the problem is when i tries to close my second form which is named "mainform" here , the application does not close and i have to use the visual studio to terminate the project. below is the code for moving to second form
this.Hide();
mainform mf = new mainform();
mf.Show();
I found the answer. On the second form add the following code on the formclosed event .
private void mainform_FormClosed(object sender, FormClosedEventArgs e)
{
Application.ExitThread();
}
The problem you have is because “your code” basically “throws away” ANY reference back to this when the code sets it as “hidden”…
this.Hide();
After this line of code is executed, mainform is created and shown…
mainform mf = new mainform();
mf.Show();
Assuming, there is no code “after” the code above… then basically this has now been lost. The user can NOT see the form nor interact with it. mainform is shown… but it knows nothing about this. this is now lost and you have NO way of getting it back. That’s why it keeps running.
Obviously, mainform has no idea about this i.e. … the form that “created” it, however it DOES need this to stay alive. IF we “close” this and this created mainform… then mainform depends on this and will also close when this closes.
mainform mf = new mainform();
mf.Show();
this.Close();
Then, mainform gets killed as soon a this closes. So that will not work. However… if you changed the Show to ShowDialog… for the mf.ShowDialog(), then it would work and this would get closed properly.
Therefore, IF you want to “CLOSE” the form that “created” another form, then you should spin off another process for the mainform or pass this to the mainform and let it close it when it is done. I am sure there are other ways to do this and if Application.ExitThread(); ... works for you then go for it.
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?
As the title says; that's all I need. I have 2 forms : Form1 and Form2. At some point of my code I want to do Form2.ShowDialog() and after Form2 is closed I want to resume Form1. How can i do this?
I tried using Thread.Sleep(sometime) but this will just disable any controls,timers, etc from Form1 and resume after the period passed. The problem is that I cant know how much time it will take for my user to press something in Form2.
As per KDecker mentioned in a comment to your question:
If you want to show something Modally (that is the form behind is unusable) use the ShowDialog() method. This will make it so you can only use the form that ShowDialog() was called on.
If you want to show it Modeless, then just use the Show() method on Form. This will make it so you can use both forms
See the MSDN documentation for reference:
https://msdn.microsoft.com/en-us/library/aa984358(v=vs.71).aspx
I have two forms, Form1 and Form2, after opening Form1 I have to open Form2, if I click on Form1 close dialog that should not be closed before closing the Form2.
Use the Form.ShowDialog() method instead of Show() when you display the child form.
You can simply open Form2 using Form2.ShowDialog(): it this is called from Form1 code then Form2 is modal with respect to Form1 (that is you can interact no more with Form1 until you close Form2) and so the user is forced to close Form2 before being able to close Form1.
Please mark this answer as accepted if it responds your question.
Do you need interact with both windows?
If only Form2 gets the attention, you can solve it with the mentioned ShowDialog ().
Otherwise you can check with OnClosing if Form2 still open and refuse it.
Here you have a nice code snip to disable the close button:
http://social.msdn.microsoft.com/Forums/windows/en-US/32148556-2640-4d79-a7b6-f13f9ce3420a/how-to-enabledisable-the-close-buttonx-in-window-form-?forum=winforms
Code below. Maybe this is what you need.
private void btnExit_Click(object sender, EventArgs e)
{
this.Visible = false;
}
I've a project that contain 2 forms e.g. (Form1 , Form2 ).The user should be able to use any of the 2 forms while both of them are visible(i.e. Can use Form1 While Form2 is Open).At the same time when Form2 is closed there is an action that should be invoked in Form1 related to From2_close. I can't use Form2.ShowDialogue as it trap focus only to Form2 but user should be able to edit some data in Form1 while Form2 is loaded. Hope the question is clear, please help. Thanks
Your question is not entirely clear, but I think you are trying to send a notification to a given set of forms based on any other forms actions. This type of notification scenerio can be very easily handled by implementing the Observer Pattern. Basically you have a single object that is the "observer" and it watches subscribers that register with it for messages. When the observer receives a message from one of its subscribers (ie a form) it then notifies the other subscribers that it received a message.
Hope this helps.
Handle either the FormClosed or VisibileChanged event. When one of those events fires you know the form has been closed or hidden and can react accordingly.
ShowDialog creates a modal dialog, which means that no other code in the calling thread will execute until the form closes. In this case, the dialog is blocking the GUI thread on your other form, which means that the user can't use it.
Instead of using ShowDialog to create an instance of Form1 from Form2 (or vice-versa), as I'm guessing you are currently doing it, just instantiate the other form.
Form1's Load handler:
Form2 form2 = new Form2();
form2.Show();
*EDIT
To allow each form to be closed without the other closing or the application exiting, do this:
In Program.cs, you'll see a call to Application.Run that creates a new instance of Form1 and starts the application. Replace that with the code from above, repeated twice, to create Form1 and Form2. After those two calls, add in a plain Application.Run() call.
Then, in each form, handle the FormClosing event by instead exiting the application if both forms are closed.