Button Event Handler

G

Guest

I’ve come across an unusual situation tonight, so I’m going to see if anyone
can give me an explanation for what could be going on here.

I created a new form based on instructions from a Microsoft Press book.
Rather than create the entire form (visually), I decided to copy and paste
all the controls from the training sample to my sample. Then, after words, I
entered all of the form’s code. Now, there are two buttons on the form. In
the form’s code module I found an event for one of the buttons. Anyway, I ran
the project anyway without adding any code to the btnClose button. But, when
I clicked on the close button the form closed. Now, this puzzles the heck out
of me. How can my form close when the form’s event modules do not include a
click event or any other event associated with the close button? If fact,
there’s no CLOSE command anywhere in the form’s code at all.

Does anyone have an explanation why something like this could happen?

I know this you might think this can’t be, but its happening and it makes no
sense to me. I’m assuming there must be a way to embed code in a control that
is not visible in the code editor?

I also set a breakpoint on every single line of code in the form and still,
the close button closes the form. Plus, I added an event handler to the click
event to display a message and it worked, but still closed the form. This
makes no sense to me and how can this be possible.
 
T

Tom Shelton

I've come across an unusual situation tonight, so I'm going to see if anyone
can give me an explanation for what could be going on here.

I created a new form based on instructions from a Microsoft Press book.
Rather than create the entire form (visually), I decided to copy and paste
all the controls from the training sample to my sample. Then, after words, I
entered all of the form's code. Now, there are two buttons on the form. In
the form's code module I found an event for one of the buttons. Anyway, I ran
the project anyway without adding any code to the btnClose button. But, when
I clicked on the close button the form closed. Now, this puzzles the heck out
of me. How can my form close when the form's event modules do not include a
click event or any other event associated with the close button? If fact,
there's no CLOSE command anywhere in the form's code at all.

Does anyone have an explanation why something like this could happen?

I know this you might think this can't be, but its happening and it makes no
sense to me. I'm assuming there must be a way to embed code in a control that
is not visible in the code editor?

I also set a breakpoint on every single line of code in the form and still,
the close button closes the form. Plus, I added an event handler to the click
event to display a message and it worked, but still closed the form. This
makes no sense to me and how can this be possible.

Hmmm... My guess is that the from is shown using form.showdialog, and
the close buttons DialogResult property is set?

Am I close?
 
M

Michel Posseth [MCP]

default button and dialog result property`s i use this handy feature to
let my user close a dialog with a enter strike

hth

Michel
 
H

Herfried K. Wagner [MVP]

Greg said:
I created a new form based on instructions from a Microsoft Press book.
Rather than create the entire form (visually), I decided to copy and paste
all the controls from the training sample to my sample. Then, after words,
I
entered all of the form’s code. Now, there are two buttons on the form. In
the form’s code module I found an event for one of the buttons. Anyway, I
ran
the project anyway without adding any code to the btnClose button. But,
when
I clicked on the close button the form closed.

Make sure there is no 'DialogResult' assigned to the button's 'DialogResult'
property. You can check this in the button's property window.
 
G

Guest

Ok, now that makes a little sense then. I can also see now that there is a
DialogResult property associated with a buttonc control. It seems that if I
set this property to Cancel, it will always close the form, regardless of how
it was opened. Plus, I can just use this property instead of creating a Click
event with me.close in it. Acutally, it seems that I can use any DialogResult
option to close the form.

I can see were this will be a very useful tool.

Thankis everyone.

Greg
 
J

Jeffrey Tan[MSFT]

Hi Greg,

Thanks for your feedback.

Yes, once you have set the Button.DialogResult to anything other than None
and if the parent form was displayed through the ShowDialog method,
clicking the button closes the parent form without your having to hook up
any events. This behavior is by design and is documented in the "Remarks"
section of the link below:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.button.dialogr
esult(VS.80).aspx

If you still have anything unclear, please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

This certainly is very useful information. I can see alot of potential by
using it. Thanks for the insite.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top