Close form using the cross button - c#

I'm working on a C# project. I need to do the following but I'm not really experienced and I can't find it on the Internet.
I want to do an action when the user clicks on the cross button (for closing one form). I mean, if the user clicks on the "X" button on the top right of the form I want to use a method that deletes one file.
I just want to know the code for noticing that the user clicked on the "X" (close) button. I hope you guys understand my question. Thank you so much!!

I don't think it's user friendly to delete something on closing a form (especially as the 'X' button is understood as the close button by everybody who uses a computer), but you can override the OnFormClosing method of a form. Something like this:
// this will also close the form
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
if (e.CloseReason == CloseReason.WindowsShutDown) return;
// Do some stuff here (delete the file or whatever)
}
If you want to alter the behavior so bad that the form won't close on pressing 'X', you could do sth like
protected override void OnFormClosing(FormClosingEventArgs e)
{
e.Cancel = true;
// some stuff here...
}
Which is very nasty IMO

You have to handle FormClosing event:
The FormClosing event occurs as the form is being closed. When a form
is closed, it is disposed, releasing all resources associated with the
form. If you cancel this event, the form remains opened. To cancel the
closure of a form, set the Cancel property of the FormClosingEventArgs
passed to your event handler to true.

Related

Calling PerformClick cancels closing event

Something strange is happening in my WinForms app (and with strange things it's normally my fault).
I've got a FormClosing event that will check whether any changes has been made on the form that has not been saved.
If the user confirm to save the changes, I will call the Save button's click event. However, even if I remove all the logic in the OnClick event, the form stays open, and I have to close it again before it closes.
If I skip the line buttonSave.PerformClick(); then the form closes properly.
Why will entering the click event, cancel the close? Any work around?
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
//Check if there changes were made on the form
//Prompt user to save
if (confirmSave)
buttonSave.PerformClick();
}
private void buttonSave_Click(object sender, EventArgs e)
{
//Everything in here commented out.
}
Just call the event instead of PerformClick.
PerformClick probably executes more code that makes the form stop closing. Like showing that the button is clicked, the sound etc.
So change PerformClick to buttonSave_Click. So it will just do the save function, without doing anything with the button itself.

C# distinguish between user form close and o.s. form close

I would like to know how to distinguish a form close between the user request and o.s. request (for example for a system shutdown). In the first case I want to ask exit confirmation, in the second I will exit without any other confirm.
The event args for the FormClosing event has a CloseReason property which exposes a CloseReason Enumerable value. This should tell you why the form is closing.
If the system is shutting down, it will contain the WindowsShutDown value.
If the user is closing the form by clicking the "x" button, then it will contain the UserClosing value.
Note that if you personally call the Form.Close() method from any method or event, it will likely have the None value, so if you are programatically closing your form via an event on an additional close button that you've added or after some action has been performed, you may have to handle that as well.
Every form in its FormClosing event receives a FormClosingEventArgs where there are two properties named Cancel and CloseReason.
The first one allows to stop the closing of the form, the second one is an enum that defines a WindowsShutDown reason, so your code colud be
private void form_FormClosing(Object sender, FormClosingEventArgs e)
{
if(e.CloseReason != CloseReason.WindowsShutdown)
{
DialogResult d = MessageBox.Show("Closing app?", "MyApp", MessageBoxButton.YesNo );
if(d == DialogResult.No)
e.Cancel = true;
}
}

Is ShowDialog handled differently in WPF than Winforms?

I have another issue in converting my Winforms program to a WPF program. In my first program, I had a smaller window open to allow the user to adjust some data, and then when it closed, the other form was activated again with the new data.
I used form2.ShowDialog(); to open the form, which automatically makes the parent form deactivated in Winforms. This way when I closed form2, the parent form was activated, and I was able to use an event handler form1_Activated to reload and re-initialize some of the settings successfully.
However, now when I attempt to do the same thing with WPF, I am still able to open form2 using form2.ShowDialog();, but then when I close the form, it does not register the form1_Activated event handler. Instead, in order to reload the settings, I must click on another window, and then come back into my program to register the form1_Activated event handler.
Am I just doing something wrong, or is there another event handler that I should be using in WPF to achieve the same thing I was able to do in Winforms?
Calling ShowDialog() causes the dialog box top appear in modal mode so I don't understand why you would need an event handler to process the results after the dialog box is closed. Keep in mind that you can access public variables in the DialogBox, as well. If I understand your question, this should do what you are asking:
MainWindow:
My_DialogBox dlg = new My_DialogBox();
dlg.Owner = this;
dlg.MyPublicVariable = ''; //some value that you might need to pass to the dialog
dlg.ShowDialog(); //exection of MainWindow is suspended until dialog box is closed
if (dlg.DialogResult == true)
{
//dlg.MyPublicVariable is still accessible
//call whatever routines you need in order to refresh the main form's data
}
DialogBox:
private void OK_Button_Click(object sender, RoutedEventArgs e)
{
MyPublic variable = something; //accessible after the dialog has closed.
this.DialogResult = true;
}
private void Cancel_Button_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
The MSDN write-up on dialog boxes is pretty good. There may be some tips that might help you even more:
http://msdn.microsoft.com/en-us/library/aa969773.aspx
Good luck!

FormClosing with CloseReason = UserClosing doesn't work expectedly?

I have a main form, in the class of this form, I declare another form. This form lives with the main form until the main form is unloaded. There is a button on the main form, clicking this button will show the member form (I mentioned above). I want to prevent the member form from closing when user closes that form and I added the following FormClosing event handler for that form:
private void MemberForm_FormClosing(object sender, FormClosingEventArgs e) {
if(e.CloseReason == CloseReason.UserClosing){
e.Cancel = true;
Hide();
}
}
That works OK for that form. However if user closes the main form, this form is not closed, and it's hidden somewhere making my application seem to run silently. I want this form also to be closed. This is very simple by adding some FormClosed event handler for my main form to close the member form manually. Closing it manually is OK, but why do I have to do that? It seems that when user closes the main form, the FormClosing event of the member form is fired with a parameter FormClosingEventArgs passed in and the CloseReason is the same as the CloseReason of the main form (which is UserClosing). I don't understand this, I thought the CloseReason of the form is UserClosing only when user clicks on the X button, I thought the CloseReason for my member form is something like "MainFormClosing".
Is there some way to close the member form automatically as by default?
UPDATE
Here is the method showing the member form (showing it as a dialog):
private void ShowMemberForm_Click(object sender, EventArgs e){
memberForm.ShowDialog();
}
But I don't think this matters, because when I load my main form, even I don't need to click on the ShowMemberForm button, and try closing my main form first, it doesn't still close the member form.
Thanks!
UPDATE
There is something strange here, I've tried commenting out the line e.Cancel = true, or even all the FormClosing event handler and the problem is still there. This is so strange, it works OK before, I've just added the member form and this form relates to some Thread handling, but the thread starts only when a button on the member form is clicked. I didn't click that button.
What I have done in the past is set a flag when programatically closing
so in the MemberForm
private bool _ForceClose = false;
public void ForceClose()
{
_ForceClose = true;
this.Close();
}
private void MemberForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(!_ForceClose)
{
e.Cancel = true;
Hide();
}
}
Then in your MainForm you can call
memberForm.ForceClose();
From within your MainForms FormClosing method or from your MainForms Dispose() or Deconstructor.
It's low tech, but it works. Im not sure if you should put _ForceClose = true in your MemberForm's Dispose method, i'm fairly certain when it gets there its already been closed BUT it couldn't really hurt.

Show a tooltip on the Close (X) button on the form

I have a form, wherein I prohibit the user from closing it when the user clicks the Close (X) button. Is it possible to show a tooltip on the Close (X) button whenever it is clicked? I want to do it to notify the user why the form would not close.
I thought of a messagebox but then I thought it would be too annoying to close the messagebox every time you click the Close (X) button.
Is there a better way to notify the user, than what I'm trying to do?
EDIT:
This is my code for the FormClosing event
private void InputForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
if (!mCloseReason)
{
e.Cancel = false;
}
}
}
I have a method that will save the inputs in the form. Once that method is finished, I need to automatically close the form. The if-statement will be true once I call this.Close(), this means I can't close the form. That's why I used a variable called mCloseReason to be able to close the form automatically. Now, that's why I was asking if I can notify the user through a tooltip once the Close (X) button is clicked.
I believe the default tooltip for the close(X) button is not editable without some hacking (and it is not practical).
Maybe what you could do is have a status field below your form, so when the user clicks the close button, it says in the status field "cannot close form (...)"
Or another idea is to have a message pop up somewhere on the form and go away after a little, indicating the form cannot be closed.
Another good idea was mentioned by Roger... just have a Close button somewhere on the form with a tooltip coded to it, and hide the forms' title bar?

Categories

Resources