C# How to pause Form1 while Form2 is active? - c#

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

Related

Form 1 detects if Form 2 closes C#

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.

creating the windows forms with pages

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?

get focus on first form

I have two forms form1 and form2. I navigate to form2 using a button in form1. In form2 I have a button control; on button click, I show messageBox. when messageBox comes then it also lost focus of form1 but I want that it should not lost focus of form1. I have no concern with form2.
There's no way a message box doesn't show like a dialog. Like Eliran Pe'er said, you should make a Form like a messagebox with a label and a button and use it like this.
MessageForm form = new MessageForm.Show();
If you use ShowDialog it's going to be the same thing as MessageBox.
In your form 1 you can use TopMost property = true in order to keep it in front all the time no matter what. But this is going to keep your form on top of all other open programs.
Another workaround would be after messagebox is closed by the user (this is not a bad option) you can call form 1 to BringToFront(). To do this, you can pass the instance of form1 to the form2 in the Show method. Use that parameter in your form2 constructor.
I don't think there's an easy way to prevent MessageBox from taking focus, and thats because a MessageBox is a dialog. (dialogs take focus from the program until they being closed)
The only way I can think of is creating new form that looks like a MessageBox, and using it instead.
try this
if(MessageBox.Show("something")==DialogResult.OK)
{
form1.Focus();
}
Or
if(MessageBox.Show("something")==DialogResult.OK)
{
form1.Select();
}
Are you using ShowDialog() method or Show() method to show your form2?
If you are using ShowDialog() method, modify it as Show().
Because ShowDialog() method will not allow you to change the focus to main form (form1), until you close the sub form (form2)
Make sure you are using method,
form2.Show()
to Display form2.

What is the best way to invoke action in Form1 When Form2 is closed without using ShowDialogue in C# Windows Form Application?

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.

Showing a form via Form.Show is not given focus?

I two forms, Form1, and a UserControl which hosts Form2. Within that UserControl on Form1 I call Form2.Show();. I have also tried Form2.Show(this);. Either way, the focus is not given to the form. I have to click once within the new form to give it focus, and then I can click items within that form.
I figured that control is passing back to my main control/form, and thus the focus is getting lost. So I am waiting until Form2 is closed via:
while (form2.Visible == true)
{
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
This seems to work. However after I close the form, now the reverse holds true. Form1 is not given focus (even if I call this.Focus()) until I click once within the main form window.
Any ideas how to handle this properly. I want to show a child form (modeless) and immediatley be able to click on it, and when that form is closed, immediately be able to take action back on the parent form.
You should probably use .ShowDialog(), this can also be extended to give your response on if the user performed Form2's operation properly or aborted early.
This does make the form locked in focus up front and will halt your code execution on the first form till that form is closed.
use this.Activate(); in place of this.Focus();
Not sure I follow completely, but from your UC try opening Form2 like this:
form2.Show(Parent);
This should specify the UC's Parent form as form2's owner.
This came from the fact that I was overriding WndProc in order to display the form. When I received the CBN_DROPDOWN message, I would display the form. I fixed this by instead Invoke'ing the method that shows the form and it fixed it.
case CBN_DROPDOWN:
Invoke(new MethodInvoker(Show_DropDown));
return;

Categories