In InitializeComponent(), the following line of code causes the button's DialogResult 
property to be set to cancel:

   this.CancelButton = this.btnCancel;

One popular decompiler shows that there's a specific check that sets the button's 
DialogResult to Cancel, if it's None.

A way to prevent the automatic closure of a form is to place the following code:

   this.btnCancel.DialogResult = DialogResult.None;

after the call to InitializeComponent in the form's constructor.  Unfortunately, you 
can't do this at design-time, and placing it in the .CancelButton's click event is too 
late since the cancel will go ahead.

Although this doesn't answer your question as to why you can't use an event to halt 
the Cancel process, it is a way that prevents this behaviour and lets you decide 
whether you want to close or not, in the button's OnClick event.  I'm sure there's a 
better way though....

Personally, I'd prefer it if the form didn't change the DialogResult  to something 
other than what I set at design time.

mw

> -----Original Message-----
> From: Chris Sells [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, 11 June 2002 11:29 AM
> To: [EMAIL PROTECTED]
> Subject: Re: [DOTNET] Why is CancelButton special?
> 
> 
> Yes.
> 
> Chris Sells
> http://www.sellsbrothers.com/
> 
> > -----Original Message-----
> > From: The DOTNET list will be retired 7/1/02
> > [mailto:[EMAIL PROTECTED]] On Behalf Of Michael Weinhardt
> > Sent: Monday, June 10, 2002 5:12 PM
> > To: [EMAIL PROTECTED]
> > Subject: Re: [DOTNET] Why is CancelButton special?
> >
> > Setting up the situation you described:
> >
> > AcceptButton's DialogResult = none
> > CancelButton's DialogResult = none
> >
> > I also got the same effect.  I trapped the dialog's Closing 
> event and
> checked out the Sender
> > object (the Winform) whose CancelButton's DialogResult was actually
> set to Cancel, even
> > though the design-time property value is "none".  The AcceptButton's
> DialogResult was
> > "none" however.
> >
> > Also, if I shut the project and VS.NET down, then reopen both, the
> CancelButton's
> > DialogResult is now set to "Cancel", even though I checked it before
> shutting down
> > VS.NET where it had the expected value of "none".
> >
> > I couldn't determine where the DialogResult property get's changed
> between design and
> > runtime.  Do you get the same behaviour?
> >
> >
> > mw
> >
> > > -----Original Message-----
> > > From: Chris Sells [mailto:[EMAIL PROTECTED]]
> > > Sent: Tuesday, 11 June 2002 9:48 AM
> > > To: [EMAIL PROTECTED]
> > > Subject: [DOTNET] Why is CancelButton special?
> > >
> > >
> > > If I set the AcceptButton to a button whose DialogResult is
> > > set to None,
> > > pressing that button does not automatically close the form.
> > >
> > > However, if I set the CancelButton to a button whose
> > > DialogResult is set
> > > to None, pressing the button *does* automatically close 
> the form and
> I
> > > don't see any way to halt this process, even if I've got an event
> > > handler for that button. Why is that?
> > >
> > > Chris Sells
> > > http://www.sellsbrothers.com/
> 
> You can read messages from the DOTNET archive, unsubscribe 
> from DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.
> 

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to