i have a problem in a program in which i use on closing event in order to trap the X button like above :
this.closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing)
The compiler (visual C#) underlines the word 'closing' as an error and tells me that there is no definition for closing and maybe i am missing a using directive.
Does anyone know which is that directive ?
Any help would be fully appreciated
Thanks
C# on closing event
Started by giannis123456789, May 09 2010 04:34 AM
12 replies to this topic
#1
Posted 09 May 2010 - 04:34 AM
|
|
|
#2
Posted 09 May 2010 - 06:38 AM
I don't understand what your asking, are you try to cancel the form closing? If so you can use this inside your closing event.
e.Cancel = true;
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.
Science is only an educated theory, which we cannot disprove.
#3
Posted 09 May 2010 - 10:44 AM
Yes i want to cancel the form from closing with the 'closing' event but the compiler underlines the word closing
like an error and it says that there is no definition 'closing' and maybe i am missing a using directive at the top.
If you know which using directive is that or anything i could do to avoid this error please tell me..
Thanks
like an error and it says that there is no definition 'closing' and maybe i am missing a using directive at the top.
If you know which using directive is that or anything i could do to avoid this error please tell me..
Thanks
#4
Posted 09 May 2010 - 01:50 PM
OK well, IDK why your useing that "this.closing += new System.ComponentModel.CancelEventHandler(this.Form 1_Closing)" deal but just add this code to your form closing event:
DialogResult result = MessageBox.Show("Are sure you want to quit?", " . . . ", MessageBoxButtons.YesNo);
switch (result)
{
case DialogResult.Yes:
break;
case DialogResult.No:
e.Cancel = true;
break;
} It just displays a messagebox asking if you want to quit, if "yes" close if "no" don't close.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.
Science is only an educated theory, which we cannot disprove.
#5
Posted 11 May 2010 - 12:26 PM
The reason i use this code "this.closing += new System.ComponentModel.CancelEventHandler(this.Form 1_Closing)"
is to create the event for closing. I added the code you gave me to the event but now the problem is that the event is not called at all and i dont understand why.
The code I have used for all the closing event is that :
[BrowsableAttribute(false)]
public event CancelEventHandler Closing;
......
......
private void Form1_Load(object sender, EventArgs e)
{
this.Closing +=new CancelEventHandler(Form1_Closing);
}
void Form1_Closing(object sender,System.ComponentModel.CancelEventArgs e)
{
DialogResult result = MessageBox.Show("Are sure you want to quit?", " . . . ", MessageBoxButtons.YesNo);
switch (result)
{
case DialogResult.Yes:
break;
case DialogResult.No:
e.Cancel = true;
break;
}
}
is to create the event for closing. I added the code you gave me to the event but now the problem is that the event is not called at all and i dont understand why.
The code I have used for all the closing event is that :
[BrowsableAttribute(false)]
public event CancelEventHandler Closing;
......
......
private void Form1_Load(object sender, EventArgs e)
{
this.Closing +=new CancelEventHandler(Form1_Closing);
}
void Form1_Closing(object sender,System.ComponentModel.CancelEventArgs e)
{
DialogResult result = MessageBox.Show("Are sure you want to quit?", " . . . ", MessageBoxButtons.YesNo);
switch (result)
{
case DialogResult.Yes:
break;
case DialogResult.No:
e.Cancel = true;
break;
}
}
#6
Posted 11 May 2010 - 12:31 PM
OK I'll show you how to make the event work correctly:
First select your form go into its properties, and on the very top on the bar there should be a lighting bolt, click it, what that does is gives you a list of events you can create for the form so just find the formClosing event and double click it, and it should create this:
First select your form go into its properties, and on the very top on the bar there should be a lighting bolt, click it, what that does is gives you a list of events you can create for the form so just find the formClosing event and double click it, and it should create this:
[COLOR=#0600FF]private[/COLOR] [COLOR=#0600FF]void[/COLOR] Form1_FormClosing[COLOR=#000000]([/COLOR][COLOR=#FF0000]object[/COLOR] sender, FormClosingEventArgs e[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#000000]}[/COLOR]
Then just add the code I gave you(or your own) and it should work. ;)[COLOR=#0600FF]private[/COLOR] [COLOR=#0600FF]void[/COLOR] Form1_FormClosing[COLOR=#000000]([/COLOR][COLOR=#FF0000]object[/COLOR] sender, FormClosingEventArgs e[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
DialogResult result = MessageBox.Show("Are sure you want to quit?", " . . . ", MessageBoxButtons.YesNo);
switch (result)
{
case DialogResult.Yes:
break;
case DialogResult.No:
e.Cancel = true;
break;
}
[COLOR=#000000]}[/COLOR]
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.
Science is only an educated theory, which we cannot disprove.
#7
Posted 12 May 2010 - 01:16 AM
Lol what do you know :lol:
replace Form1 with the name of your form then put the code in CodeFile.
+rep a day Keeps lousy posters from CodeCall away
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("You are closing the form Because:" + e.CloseReason.ToString());
}
replace Form1 with the name of your form then put the code in CodeFile.
+rep a day Keeps lousy posters from CodeCall away
#8
Posted 12 May 2010 - 03:10 AM
Quote
Lol what do you know :lol:
replace Form1 with the name of your form then put the code in CodeFile.
+rep a day Keeps lousy posters from CodeCall away
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("You are closing the form Because:" + e.CloseReason.ToString());
} replace Form1 with the name of your form then put the code in CodeFile.
+rep a day Keeps lousy posters from CodeCall away
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.
Science is only an educated theory, which we cannot disprove.
#9
Posted 12 May 2010 - 06:32 AM
thegamemaker said:
I still can't understand what he's saying.:w00t:
I hate people who abandon the post after making a appeal
#10
Posted 12 May 2010 - 09:09 AM
thanks a lot. That was what i was trying to do but with the wrong way...:p
thanks again
thanks again
#11
Posted 12 May 2010 - 09:10 AM
thanks a lot. That was what i was trying to do but with the wrong way...:p
thanks again
thanks again
#12
Posted 12 May 2010 - 11:17 PM


Sign In
Create Account

Back to top









