FormClosing in c# - c#

Good day stackoverflow. My problem here is that the close form function is not implemented right after the closure of the form. But if I close the form and open it up again and close it again the Close_Form function executes. How can I do it in such a way that after compile and run the program I could use the close form method right away? Please help.tnx
private Form2 ins = new Form2();
private void userManageLink_Click(object sender, EventArgs e)
{
ins.ShowDialog();
ins.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Close_Form);
}
private void Close_Form(object sender, EventArgs e)
{
MessageBox.Show("Hello World");
}

Move the assignment of the event handler to before the call to ShowDialog(). Once you call ShowDialog(), it doesn't return until the form has already closed. By the time, there's no point in installing the event handler because the form is already closed and the event won't be fired.

The problem is likely that you're adding the FormClosing handler after the ShowDialog call. The dialog is shown, you close it, then you add the handler. Try adding the handler before the ShowDialog call.

Related

When closing form and returning to previous, it keeps opening instances of the form

Reports_FormClosing(Object Sender, FormClosingEventArgs e)
{
Dashboard dash = new Dashboard();
dash.Show();
this.Close();
}
Is there something I need to do with the ResumeLayout event?
This code is responding to the FormClosing event:
Reports_FormClosing(Object Sender, FormClosingEventArgs e)
And one of the things this code does is close the form:
this.Close();
Which triggers the FormClosing event, which closes the form, which triggers the FormClosing event, and so on, and so on.
The form is already closing, you don't need to tell it to close. Just remove that line.

Do I need to call Form.Close(), or is it handled for me?

Using C# and WinForms, I have a base menu that calls a variety of forms on button clicks:
private void Some_Button_Click(object sender, EventArgs e)
{
var someForm = new SomeForm();
someForm.MdiParent = MenuForm;
someForm.Show();
someForm.BringToFront();
}
My concern is memory useage. When a user click's "x" to close the form, is Form.Close() already called when the window closes? Or, do I need to create an event in order to explicitly call Form.Close()?
When a user click's "x" to close the form, is Form.Close() already called when the window closes?
No, the FormClosing event is fired. Form.Close tells the system to close the form (the FormClosing event is fired in that case as well).
Close is command. FormClosing is called in response to a command.

Owned form and mdi parent

this is my scenario and hope you can solve it for me
I have a MDI container form called "MainForm". In MainForm there is a simple form call "Form1". In Form1 there is a button. every time you pushed it, It open a new form which instance of "Form2". The followng code is click button event.
Button_Click()
{
Form2 frm=new Form2();
frm.mdiparnt=this.MdiParent;
this.addOwnedForm(frm);
frm.Visible=true;
}
and the following code tries to close owned forms when the user close Form1
Form1_CloseEvent()
{
foreach(var item in this.ownedForm)
{
item.close();
}
}
But when the debugger steps into close event, just close Form1, and the form2 instances remain open. what should I do to solve it
I think you are not setting up the event. Do it like this.
Add it to your Button_Click() method:
this.FormClosed += Form1_FormClosed;
Here is the method:
void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
foreach(var item in this.ownedForm)
{
item.close();
}
}
first of all this code does not compile!
you have several syntax errors: mdiparnt, addOwnedForm, ownedForm, close
you are probably not sharing your actual code and that's gonna be a problem to help you if it's not your code.
now in Button_Click() event you are doing
frm.mdiparnt=this.MdiParent;
this.AddOwnedForm(frm);
even though you only need
this.AddOwnedForm(frm);
or an exception will be thrown. i've checked this code and it's working just fine

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.

Is there a way to have the close window button (X) call a method before actually closing the window?

In a windows form project in C#/.net/Visual Studio 2010, if someone hits the close window button, I want to disable some other things in the program as a consequence. Is there a method call for hitting "X" that I can extend?
The two events that you should use are FormClosing and FormClosed.
In the .Net Framework verion 2.0 these events replaced the obsolete Closing and Closed events.
The reason for the new events was that the old ones were not raised when the Application.Exit method was called.
An example of the usage of the FormClosing event is below:
public Form1()
{
InitializeComponent();
this.FormClosing += Form1_FormClosing;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("Really close this form?", string.Empty, MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
e.Cancel = true;
}
}
That code simply asks the user if they want to close the form, and if No is selected, it then cancels the event. You can put any logic you require in the event handler.
The MSDN article on Form.Closing is here and includes references about why the event was made obsolete. The reference for Form.FormClosing is here
Your window class publishes two events for this purpose:
Closing allows you to cancel the close if it's inappropriate
Closed allows you to react on close.
Just register a handler on the FormClosed event.

Categories

Resources