Why is the messagebox shown before the form closes?
SomeForm myForm = new SomeForm();
myForm.Show();
MessageBox.Show("Some text");
Do I need to start a new thread, and wait for it to finish? I've never come across this problem before.
You need to use Form.ShowDialog() instead. Form.Show shows a modeless window, whereas Form.ShowDialog shows a modal form, i.e., that the user has to close before continuing.
Your example never actually closes myForm...only shows it. I would expect the code above to show myForm and then immediately show the MessageBox.
If you want myForm to close before showing the MessageBox, you'll need to call myForm.Close() at some point.
The reason for this is that myForm.Show(); starts the opening of a new window, however it is not a blocking call. It's not a dialog, it's a separate window, which runs on it's own.
If you want SomeForm to show as a Dialog (blocking window, waits for close to continue execution), then use ShowDialog. If you want to wait till the window is opened before you show the MessageBox, then add the MessageBox to the OnLoaded on the SomeForm claa.
If it's important that the user not be able to interact with the main form while this other form is being shown then you can use myForm.ShowDialog instead of Show. ShowDialog is a blocking function, so it won't return until the other form is closed. Show, on the other hand, just shows the other form and then immediately returns without waiting for the form to be closed.
If it's intentional that the other form is not being shown modally, and you don't (or can't) make that change, then you can use events to show a message box when the other form is closed.
SomeForm myForm = new SomeForm();
myForm.FormClosed += (form, args) =>
{
MessageBox.Show("Some text");
};
myForm.Show();
This will add an event handler, which fires when the other form is closed, and executes the desired code at the appropriate time.
Related
look this please:
var form = new Form();
form.Shown += (_, __) =>
{
var timer = new System.Windows.Forms.Timer { Interval = 1000 };
timer.Tick += (x, xx) =>
{
timer.Stop();
GC.KeepAlive(timer);
timer.Dispose();
form.Close();
Application.DoEvents(); // no effect
// it will cause form keep show
MessageBox.Show("asdf");
// but if this, that's fine
// BeginInvoke(new Action(() => MessageBox.Show("asdf")));
};
timer.Start();
};
form.ShowDialog();
form.Close before MessageBox.Show, but form will not close until close msgBox, please help.
--end--
all in code, why need more words? all in code, why need more words? all in code, why need more words?
When you show a form as modal using ShowDialog(), calling Close in fact sends a WM_CLOSE message and then it sets DialogResult to Cancel which acts as a flag for modal message loop to exit the loop.
So Close will not close or hide the modal dialog immediately. Then after finishing the modal message loop, the modal dialog will be hide (but not destroyed).
MessageBox method also blocks the execution of code, so codes after the message box will execute just after closing the message box. So now it's clear why after calling Close, first the MessageBox shows then after closing message box, form closes.
Just to make it easier to understand, here is a pseudo-code which shows what is happening when you call ShowDialog in your code:
Form Shows
While Form.DialogResult != None
{
Form.Close → Sends WM_CLOSE → Sets Form.DialogResult = Cancel
MessageBox.Show and wait until MessageBox closes
}
Form Hides
Just keep in mind, Close is not equal to return, it means the code which you have after Close will run as well. Here the code is MessageBox which blocks the loop until after MessageBox closes.
To hide the dialog immediately, replace the form.Close() with form.Hide(), this way without waiting for the loop, you are commanding the form to get hidden. But it doesn't mean the form has been closed and therefore lines of code which you have after ShowDialog will not run until after the loop finishes.
For more information about how Close and ShowDialog work, you may want to take a look at a Windows Forms source code, specially the following lines:
LocalModalMessageLoop in Application
CheckCloseDialog in Form
WMClose in Form
Form2 is user confirmation window,i want to close the application by stop button on mainform,but currently form2 is active and there is no dependency between form2 and mainform.How to close application ?
You must be opening Form2 using ShowDialog() method. Instead of that use Show() method and on MainForm button's click event call Application.Exit() method
You can use...
// for WinForms application
if (System.Windows.Forms.Application.MessageLoop)
{
System.Windows.Forms.Application.Exit();
}
// for Console application
System.Environment.Exit(1);
So your problem is the parent window will not have focus/input until the modal window (the confirmation dialog) has finished.
So, to avoid this, just make the confirmation dialog using a custom window (instead of a common dialog / msgbox) and show it. This way if the user wants to click your main form they can (the confirmation will be on background, covered by the main window because of this).
it looks like you're showing the dialog as ShowDialog() or showing a built in MessageBox which is also a ShowDialog that needs to be closed in order for focus to return back to main window. So what you should do is create a custom Dialog and use dialog.Show() method. This will allow you to interact with your main window while the dialog is open and you can click on a button to close your application regardless of the dialog being open or not. Following code should do the closing work for you
Application.Current.ShutDown();
I have a dialog form:
_myLogin = new LoginPage(_myDriver);
_myLogin.ShowDialog();
and then I want to load another form
new RegularUser().Show();
Why dosn't my second form load?
Or you can simply call new RegularUser().Show(); in the load method of your LoginPage form.
Because your are showing your first form as a Modal Dialog which must be closed or hidden before the user can continue working with the rest of the application, like a MessageBox.
You can instead use:
_myLogin.Show();
to display it as a Modeless form.
From the link:
If a form is displayed as modal, the code following the ShowDialog method is not executed until the dialog box is closed. However, when a form is shown as modeless, the code following the Show method is executed immediately after the form is displayed.
I believe the second form doesn't load because you've not closed first Modal dialog form.
ShowDialog() displays Modal dialog window
MSDN, Form.ShowDialog Method:
When this method is called, the code following it is not executed
until after the dialog box is closed.
Try out using Show() method instead, so WIndow would be shown and all code after Show() call will be executed as well.
PS: Considering your form names, may be Modal dialog is the right decision for LoginForm? SO user see LoginForm, enter credentials, and only after the successfull authorization would be able access RegularUser form, I'm not sure about this because just assuming having only form names, but anyway Modal dialogs are excatly for such cases.
Is there a way to show a modal form without ShowDialog metod calling? By showing a modal form I mean that the Form overlays the current Form and prevent user input to the bacground Form. The Form that is to be shown is a MessageBox style form that is not fullscreen.
Thanks
Dominik
I would assume that you could set the "dialog" form to stay on top (TopMost property) and then disable the main form in order to prevent input.
This is only a partial solution as the main form will still be able to be focused, closed etc. You will need to handle all these cases yourself in order to prevent them.
Is there any particular reason you don't want to use ShowDialog?
You could try to do this:
MyForm frm = new MyForm(); // this would be your modeless dialog
frm->show(this); // "this" being the instance that invokes it.
I want a help. I have a window form application. Whenever I click on the "close" of the form, the application should itself close.
Can anyone help me.
Regards,
Justin Samuel.
After your explanation:
In Form1, do something like:
Form2 f2 = new Form2();
f2.ShowDialog();
this.Close();
Application.Exit();
This is assuming Form2 can be shown Modal (Dialog), which I think is correct
A Winforms application will exit automatically when the main form is closed. The main form is the form that is instantiated and passed to Application.Run method in the Main method of the application.
If the application process does not exit when this form is closed, something is preventing it from closing, such as a thread (that is not a background thread) that is performing some work.
By your description, it sounds like you may have multiple forms in your application and one form is still running even if its's not visible. In this case, the close button, would close the form, but not exit the application.
You need to add an event handler for the Form_FormClosed event and then call Application.Exit() - Ideally, you could also handle the Form_FormClosing event if you want to give the user a dialog to ask if they really meant to close.
Maybe you are looking for the Application.Exit() method.