Exiting application on X button of form pressed - c#

I have a modal form (login) which I want that on closing it using "X" button, force application to exit.
I tried this:
private void Login_Deactivate(object sender, EventArgs e)
{
Application.Exit();
}
this cause my application to exit on pressing login button (closing). but I want exiting only on "X" button of window.
How can I do it?

Get rid of the Deactivate event, you don't need it.
The issue here is that you cannot tell that just the "X" button was pressed. What you really do is track all the other conditions that cause form close, and react when it's not one of those.
You should leverage the DialogResult enum, and allow the calling application to react, instead of trying to do it all in the Login form.
In your Login form constructor, set the property to None:
public Login()
{
DialogResult = DialogResult.None;
}
Then, in your button handler for "OK", set the property: DialogResult = DialogResult.OK.
If you have a specific Cancel event, you can set DialogResult = DialogResult.Cancel.
Ultimately, you could have a FormClosing event in the Login form, in which you can check this value and react. But more importantly, your calling application can check the result from the login form and react there as well.

I think you should use this.Parent.Application.Exit(), it kills whole application.

Instead of Deactivate event, subscribe to the "FormClosing" event of the "Login" form. Write the same code which you have written.
Keep the Deactivate event only if it is required to close the Application when you click outside login form.

Related

Why does FormClosing fire twice when calling Hide() in modal dialog?

We created a new form that we're showing via ShowDialog and added a "Cancel" button to it. Here's how we're opening the form from its parent:
// _modalForm is a class-level instance of our ModalForm class
var result = _modalForm.ShowDialog(this);
MessageBox.Show(result.ToString());
Here's the cancel button's Click event handler in ModalForm.
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
In our FormClosing event, we have this code (based on this answer).
private void ModalForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
_parentForm.RefreshData();
}
Surprisingly, when we click the "Cancel" button (or use the "X" button at the top of the form), the FormClosing event is raised twice. Both times the CloseReason is UserClosing.
I double checked to make sure InitializeComponent isn't call twice and that we only subscribe to the event once. btnCancel is not set at the CancelButton property for the form. It also doesn't have DialogResult set in the designer. When I check the return value of ShowDialog though, it is set to DialogResult.Cancel.
Changing btnCancel_Click to just be DialogResult = DialogResult.Cancel instead of Close() and doing nothing except _parentForm.Refresh() in the FormClosing event fixes the issue of the event getting raised twice.
Does anyone know why in this particular scenario the FormClosing event gets raised twice?
That's because hiding a modal form will cause it to close with DialogResult.Cancel as dialog result. So if you call this.Hide() in FormClosing event, the event will be raised again.
Imagine if it didn't close the form, your application had been blocked by a hidden modal form!
Note: The answer describes about the reason of raising the event twice. But as described here and others mentioned, For modal forms (which you showed using ShowDialog), the Dispose method will not be called and the form exists after closing and you can use its properties to get some data or you can show it again. So you don't need to call hide method.
For more information take a look at: Do I need to Dispose a Form after the Form got Closed?
The workaround you mention is unnecessary, because modal dialogs are not disposed when closed. They are designed to keep their data after being closed (since it is required by the caller of the modal dialog), and to be reused as needed.
Just let it close properly. It causes no harm to a modal dialog :)
Note that this also means that unlike normal forms, you must dispose of modal dialogs manually if they are not persistent. In your scenario, this most likely means you'd want to dispose of the dialog when the parent form is disposed (this happens automatically if you added the dialog as a component, but needs to be done manually if you create it yourself).

Form in C# returns DialogResult.Cancel to Parent When It is Minimized

I have a form "Main" and in Main I am creating another form which I use .ShowDialog() to display it. I do this because I don't want the rest of the code to in Main to execute until after the new form is completed. I am allowing a user to minimize the new form to system tray.
The problem: When the form gets minimized it is returning DialogResult.Cancel to the Main form that called it, causing the next line to run early.
Code to create form from Main form:
for(int i = 0; i < lvAll.SelectedItems.Count; i++)
{
this.Hide();
this.run = new RunProfile(this.profiles[lvAll.SelectedItems[i].Text]);
DialogResult result = run.ShowDialog();
MessageBox.Show(result.ToString());
}
In the new form a user will get a list of files that copy... now the user can continue and copy those files and I would expect to return a result of OK and if not I assume they are going to cancel and return Cancel...
Is my only recourse to return Abort from the new form if a user specifies a cancel and assume that a cancel is someone trying to minimize? This just seems odd
You could handle this in a number of ways, one way is like what follows:
MainTestForm mainTestForm = new MainTestForm();
if (mainTestForm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
return;
}
You must set the DialogResult state when you close the form like
this.DialogResult = System.Windows.Forms.DialogResult.OK;
I hope this helps.
This is entirely by design. When you hide a modal dialog, the user doesn't have any way to get back to the program. Dialogs don't have a taskbar button and the rest of the windows in the app are disabled so cannot be activated. The only recourse the user would have is to kill your program with Task Manager.
So Winforms does the logical thing, it automatically closes the dialog to avoid this UI trap. And of course you'll get DialogResult.Cancel.
Use proper UI design, a dialog should always have its MinimizeBox property set to False.
I'm not sure what you are trying to achieve with that. However, to get the Dialog to return anything else, you have to set it in the form's (Dialog's) DialogResult property before the form is closed.
You may also use the Form_Closing event of the dialog to set the DialogResult property to what you want. This is done before the form closes.
Here is a quote from MSDN regarding ShowDialog:
When this method is called, the code following it is not executed
until after the dialog box is closed.
After further reading it does state the X does not close the form but hides it and so you must dispose of it:
When a form is displayed as a modal dialog box, clicking the Close
button (the button with an X in the top-right corner of the form)
causes the form to be hidden and the DialogResult property to be set
to DialogResult.Cancel. The Close method is not automatically called
when the user clicks the Close button of a dialog box or sets the
value of the DialogResult property. Instead, the form is hidden and
can be shown again without creating a new instance of the dialog box.
Because of this behavior, you must call the Dispose method of the form
when the form is no longer needed by your application.
It seems this same issues applies to minimizing a userform as well.

C# save confirmation message while leaving form

What is the best way to show a save confirmation message while leaving (not closing - set focus to another form) a none modal form?
The user has the options of
yes (save changes and set focus to targetform)
no (rollback and set focus to target form)
cancel (stay in active form - cancel setting focus to target form)
I can't find a solution to prevent setting focus to another form. Similar to closing event, where I can use FormClosingEventArgs.Cancel property.
To use a modal form is not acceptable in my case.
Thanks in advance
First of all you can use the Deactivated event to detect when the form loses focus. Then, if the user wants to stay in the form that lost focus, you can call the BringToFront method to set the form active again.
You can not prevent the user from activating another form, but you can try (using the above) to change activation back to the current form.
The best way to do it would be to override the OnLostFocus event. Here's the code:
protected override void OnLostFocus(EventArgs e) {
base.OnLostFocus(e);
this.Focus();
}

How to keep the focus back to form when clicking on OK button in windows forms?

I have a windows form with multiple controls and OK and Cancel buttons. In the Click event handler of OK button, it checks to see if there are errors on the form. I need the form to stay open if there are errors. But the code below is not working. If there are errors, it is closing the form and returning to the caller. And I have the below two lines to invoke and show the form.
PtForm paymentForm = new PtForm();
ptForm.ShowDialog();
private void btnOk_Click(object sender, EventArgs e)
{
this.ValidateChildren(ValidationConstraints.Visible);
string ErrorString = GetValidationErrors();
MessageBox.Show(ErrorString, "Errors");
if (!string.IsNullOrEmpty(ErrorString))
{
return;
}
//Processing
}
Thanks for any help.
There's nothing in this code that will close the form. Therefore, the culprit must be outside this code.
Did you set your OK button's DialogResult property to DialogResult.OK? That would explain why the form is closing. AFAIK, if you set DialogResult on the button, that's what happens -- there's no way to veto it in code.
So in that case, you would need to go back to the designer and set the button's DialogResult back to None. Then at the end of your btnOk_Click method, once you've validated all the input and decided that it's safe to close the dialog, add a line that sets your Form's DialogResult property to OK.
Remove DialogResult property of a button-i.e. set it to None.

WinForms Close() sets DialogResult to Cancel

If I call Close() in my WinForm, it seems that even though DialogResult is None at the time, right after I call Close() I see that it is set to Cancel.
Does this sound normal?
Or even easier, you can set DialogResult just after Close. For example, assuming ValidateSettings will show the user any problems with the form or return true otherwise:
private void btnOK_Click(object sender, EventArgs e)
{
if (ValidateSettings())
{
SaveSettings();
Close();
DialogResult = DialogResult.OK;
}
}
That is completely normal, as it is the intended behavior. However, it is not equivalent to clicking the red "X" in the top right corner of the Form if you are using an MDI or ShowDialog().
When a form is displayed as a modal dialog box, clicking the Close
button (the button with an X in the top-right corner of the form)
causes the form to be hidden and the DialogResult property to be set
to DialogResult.Cancel. The Close method is not automatically called
when the user clicks the Close button of a dialog box or sets the
value of the DialogResult property. Instead, the form is hidden and
can be shown again without creating a new instance of the dialog box.
Because of this behavior, you must call the Dispose method of the form
when the form is no longer needed by your application.
The DialogResult value can be overridden though:
You can override the value assigned to the DialogResult property when
the user clicks the Close button by setting the DialogResult property
in an event handler for the Closing event of the form.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult(v=VS.100).aspx

Categories