Click here to Skip to main content
14,772,357 members
Home » Discussions » C#
   

C#

 
Questionproblem with registering flormclosing event with button click event Pin
prasadbuddhika16-Jun-09 22:31
Memberprasadbuddhika16-Jun-09 22:31 
AnswerRe: problem with registering flormclosing event with button click event Pin
musefan16-Jun-09 22:59
Membermusefan16-Jun-09 22:59 
AnswerRe: problem with registering flormclosing event with button click event Pin
DaveyM6916-Jun-09 23:04
professionalDaveyM6916-Jun-09 23:04 
GeneralRe: problem with registering flormclosing event with button click event Pin
prasadbuddhika16-Jun-09 23:29
Memberprasadbuddhika16-Jun-09 23:29 
GeneralRe: problem with registering flormclosing event with button click event Pin
prasadbuddhika17-Jun-09 0:58
Memberprasadbuddhika17-Jun-09 0:58 
AnswerRe: problem with registering flormclosing event with button click event [modified] Pin
Alan N17-Jun-09 1:10
MemberAlan N17-Jun-09 1:10 
GeneralRe: problem with registering flormclosing event with button click event Pin
DaveyM6917-Jun-09 1:10
professionalDaveyM6917-Jun-09 1:10 
GeneralRe: problem with registering flormclosing event with button click event Pin
OriginalGriff17-Jun-09 1:12
mveOriginalGriff17-Jun-09 1:12 
I wondered about that when I saw your original code.
private void Form1_FormClosing(object sender,EventArgs e)
   {
   DialogResult ds = MessageBox.Show("exit?", "exit", MessageBoxButtons.YesNo);
   FormClosingEventArgs ee = (FormClosingEventArgs)e;
   if (ds == DialogResult.Yes)
      {
      ee.Cancel = false;
      }
   else
      {
      ee.Cancel = true;
      }
   }

private void Form1_Load(object sender, EventArgs e)
   {
   button1_Click+= Form1_FormClosing;
   }


Are you perhaps thinking that linking the ForClosing event to a button click will cause the button to generate the FormClosing event and thus close the form? If so, that will not happen.
What you need to do in your button Click event is "this.Close()" which will close the current form. That will raise the FormClosing event where you can then ask for confirmation:
private void Form1_FormClosing(object sender,EventArgs e)
   {
   DialogResult ds = MessageBox.Show("exit?", "exit", MessageBoxButtons.YesNo);
   if (e is FormClosingEventArgs)
      {
      FormClosingEventArgs ee = e as FormClosingEventArgs;
      ee.Cancel = (ds == DialogResult.No);
      }
   }

private void Button1_Click(object sender, EventArgs e)
   {
   this.Close();
   }


No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.

This message is made of fully recyclable Zeros and Ones

QuestionHelp me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
radhikasharma16-Jun-09 22:08
Memberradhikasharma16-Jun-09 22:08 
AnswerRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
Thomas Krojer16-Jun-09 22:32
MemberThomas Krojer16-Jun-09 22:32 
GeneralRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
Mirko198016-Jun-09 22:43
MemberMirko198016-Jun-09 22:43 
GeneralRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
radhikasharma16-Jun-09 23:08
Memberradhikasharma16-Jun-09 23:08 
GeneralRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
radhikasharma16-Jun-09 23:06
Memberradhikasharma16-Jun-09 23:06 
GeneralRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
Thomas Krojer16-Jun-09 23:13
MemberThomas Krojer16-Jun-09 23:13 
GeneralRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
radhikasharma17-Jun-09 1:21
Memberradhikasharma17-Jun-09 1:21 
GeneralRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
DaveyM6917-Jun-09 1:37
professionalDaveyM6917-Jun-09 1:37 
GeneralRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
OriginalGriff16-Jun-09 23:39
mveOriginalGriff16-Jun-09 23:39 
AnswerRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
DaveyM6916-Jun-09 23:50
professionalDaveyM6916-Jun-09 23:50 
AnswerRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
Alan N17-Jun-09 1:05
MemberAlan N17-Jun-09 1:05 
JokeRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
akyriako7817-Jun-09 4:34
Memberakyriako7817-Jun-09 4:34 
GeneralRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
radhikasharma17-Jun-09 4:50
Memberradhikasharma17-Jun-09 4:50 
GeneralRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
akyriako7817-Jun-09 4:54
Memberakyriako7817-Jun-09 4:54 
JokeReminds me of an Aggie Joke Pin
Andrew Rissing17-Jun-09 5:07
MemberAndrew Rissing17-Jun-09 5:07 
GeneralRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
radhikasharma17-Jun-09 5:35
Memberradhikasharma17-Jun-09 5:35 
AnswerRe: Help me iam getting stack overflow exeception , while my solution build successfully , but it threw the stack overflow error Pin
Lutosław17-Jun-09 9:37
MemberLutosław17-Jun-09 9:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.