Jump to content

C# on closing event

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
giannis123456789

giannis123456789

    Newbie

  • Members
  • Pip
  • 5 posts
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

#2
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
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.

#3
giannis123456789

giannis123456789

    Newbie

  • Members
  • Pip
  • 5 posts
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

#4
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
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.

#5
giannis123456789

giannis123456789

    Newbie

  • Members
  • Pip
  • 5 posts
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;
}
}

#6
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
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:
[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.

#7
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
Lol what do you know :lol:
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
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts

Quote

Lol what do you know :lol:
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
Is that what he's trying to do? I still can't understand what he's saying.:w00t:
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#9
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts

thegamemaker said:

I still can't understand what he's saying.:w00t:
I am in the same Fix ,:mad:
I hate people who abandon the post after making a appeal

#10
giannis123456789

giannis123456789

    Newbie

  • Members
  • Pip
  • 5 posts
thanks a lot. That was what i was trying to do but with the wrong way...:p
thanks again

#11
giannis123456789

giannis123456789

    Newbie

  • Members
  • Pip
  • 5 posts
thanks a lot. That was what i was trying to do but with the wrong way...:p
thanks again

#12
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
Glad you got your Answer ^^