Odd Button Behaviour

J

Jason Allred

I have this form in a VB.NET windows application that has a simple
"Cancel" button on it that is simply supposed to close a form. There is an
event handler for the "Click" event, and everything appears to be correct in
the code. The form does not have anything assigned for the CancelButton or
AcceptButton properties. However, when I run the application and an
instance of this window is created, this and two other buttons that are on
the main form don't do anything. It's as if the event handlers were never
set up. Has anyone ever run into this problem? If so, how do you fix it?

To complicate matters, on the same form are several tabpages, and any
buttons in these work fine.

Regards,

Jason.
 
L

Linda Liu [MSFT]

Hi Jason,

Based on my understanding, you have a Button on a form in a WinForm
application project. When you run the application and an instance of this
form is created and you click the button on the form, the form isn't
closed. If I'm off base, please feel free to let me know.

There're several possible reasons that may cause this problem.

1. The event handler isn't hooked up to the corresponding event properly.

You may also check whether the event handler is hooked up to the Click
event of the Button properly. To do this, open the code view of the form
and navigate to the Button's Click event handler. You should see "Handles
Button1.Click" at the end of the Sub procedure declaration. Or if you hook
up the event handler at run time, you should have the following line of
code in the form's code, e.g. the form's New Sub procedure or Load event
handler:

AddHandler Me.Button1.Click, AddressOf Button1_Click

2. If you show the form modeless, i.e. call the Show method to show the
form, and in the Button's Click event handler you just set the form's
DialogResult property to e.g. Cancel, the form wouldn't closed when you
click the Button. In this case, you should call the Close method to close
the form.

3. Perhaps you cancel the form's closing operation in the form's
FormClosing event handler by setting the Cancel property of the
FormClosingEventArgs to True.

Hope this helps.


Sincerely,
Linda Liu
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.
 
J

Jason Allred

Here's the code:

Private Sub btnCancelSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCancelSend.Click

Me.DialogResult = Windows.Forms.DialogResult.Cancel

Me.Close()

End Sub

Irritatingly, it isn't a behavior specific to closing the form. The
following doesn't work either:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

MessageBox.Show("Test", "test", MessageBoxButtons.OK)

End Sub

The problem lies in that the event handlers are never reached. You can
click on the buttons to your heart's content, but the code in the handlers
will never execute. Oddly, there is a TabControl on the same form, and the
buttons on it function correctly.

Thanks for your help.

Jason.
 
J

Jason Allred

It seems the TabControl on the form is causing the buttons not to function.
I removed the tab control, and they work fine. I'm at a loss as to why this
would happen however. Nor can I just remove the TabControl. How do I
rectify this so everything works?
 
J

Jason Allred

An update: When I put the buttons in a container (GroupBox or Panel), they
work fine, but the buttons still on the main form do not.
 
L

Linda Liu [MSFT]

Hi Jason,

Thank you for your prompt response and detailed feedback.

Could you please tell me what controls you place on the TabControl? When
you click the button on the form, will your application do something else,
e.g. poping up a MessageBox?

If an event handler has been hooked up to an event properly, the only
possible reason that the event handler is not executed is that the event is
not fired at all.

As for the Click event of a Button, only when the Button receives the
WM_LBUTTONDOWN windows message, the Click event is raised. If a MessageBox
pops up when you click the Button, the Button won't receive WM_LBUTTONDOWN
message so that the Click event won't be raised. You can use Spy++ tool to
monitor the windows messages that a window receives.

If the problem is still not solved, you may create a simple project that
could just reproduce the problem and send it to me. Remove 'online' from my
displayed email address to get my acutal email address.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
J

Jason Allred

From Spy++, everything looks ok, but still the event handler code is never
executed. I'll see if I can recreate it as a project and send it to you as
you suggest. For your information, here is the log from Spy++:

<00738> 003809F8 S WM_NCHITTEST xPos:1493 yPos:632
<00739> 003809F8 R WM_NCHITTEST nHittest:HTCLIENT
<00740> 003809F8 S WM_NCHITTEST xPos:1493 yPos:632
<00741> 003809F8 R WM_NCHITTEST nHittest:HTCLIENT
<00742> 003809F8 S WM_MOUSEACTIVATE hwndTopLevel:0025098A nHittest:HTCLIENT
uMsg:WM_LBUTTONDOWN
<00743> 003809F8 R WM_MOUSEACTIVATE fuActivate:MA_ACTIVATE
<00744> 003809F8 S WM_SETCURSOR hwnd:003809F8 nHittest:HTCLIENT
wMouseMsg:WM_LBUTTONDOWN
<00745> 003809F8 R WM_SETCURSOR fHaltProcessing:False
<00746> 003809F8 P WM_LBUTTONDOWN fwKeys:MK_LBUTTON xPos:17 yPos:10
<00747> 003809F8 S WM_IME_SETCONTEXT fSet:1 (LONG)iShow:C000000F
<00748> 003809F8 R WM_IME_SETCONTEXT
<00749> 003809F8 S WM_SETFOCUS hwndLoseFocus:001D0A2A
<00750> 003809F8 S WM_GETTEXTLENGTH
<00751> 003809F8 R WM_GETTEXTLENGTH cch:7
<00752> 003809F8 S WM_GETTEXT cchTextMax:16 lpszText:03D0D438
<00753> 003809F8 R WM_GETTEXT cchCopied:7 lpszText:03D0D438 ("B")
<00754> 003809F8 S message:0x202B [User-defined:WM_USER+7211]
wParam:003809F8 lParam:03D0DC1C
<00755> 003809F8 R message:0x202B [User-defined:WM_USER+7211]
lResult:00000000
<00756> 003809F8 R WM_SETFOCUS
<00757> 003809F8 P WM_PAINT hdc:00000000
<00758> 003809F8 S WM_KILLFOCUS hwndGetFocus:001D0A2A
<00759> 003809F8 S WM_CAPTURECHANGED hwndNewCapture:00000000
<00760> 003809F8 R WM_CAPTURECHANGED
<00761> 003809F8 R WM_KILLFOCUS
<00762> 003809F8 S WM_IME_SETCONTEXT fSet:0 (LONG)iShow:C000000F
<00763> 003809F8 R WM_IME_SETCONTEXT
<00764> 003809F8 S WM_NCHITTEST xPos:1493 yPos:632
<00765> 003809F8 R WM_NCHITTEST nHittest:HTCLIENT
<00766> 003809F8 S WM_NCHITTEST xPos:1493 yPos:632
<00767> 003809F8 R WM_NCHITTEST nHittest:HTCLIENT
<00768> 003809F8 S WM_SETCURSOR hwnd:003809F8 nHittest:HTCLIENT
wMouseMsg:WM_MOUSEMOVE
<00769> 003809F8 R WM_SETCURSOR fHaltProcessing:False
<00770> 003809F8 P WM_MOUSEMOVE fwKeys:MK_LBUTTON xPos:17 yPos:10
<00771> 003809F8 P WM_PAINT hdc:00000000
<00772> 003809F8 S WM_NCHITTEST xPos:1493 yPos:632
<00773> 003809F8 R WM_NCHITTEST nHittest:HTCLIENT
<00774> 003809F8 S WM_NCHITTEST xPos:1493 yPos:632
<00775> 003809F8 R WM_NCHITTEST nHittest:HTCLIENT
<00776> 003809F8 S WM_SETCURSOR hwnd:003809F8 nHittest:HTCLIENT
wMouseMsg:WM_MOUSEMOVE
<00777> 003809F8 R WM_SETCURSOR fHaltProcessing:False
<00778> 003809F8 P WM_MOUSEMOVE fwKeys:MK_LBUTTON xPos:17 yPos:10
<00779> 003809F8 S WM_NCHITTEST xPos:1493 yPos:632
<00780> 003809F8 R WM_NCHITTEST nHittest:HTCLIENT
<00781> 003809F8 S WM_NCHITTEST xPos:1493 yPos:632
<00782> 003809F8 R WM_NCHITTEST nHittest:HTCLIENT
<00783> 003809F8 S WM_SETCURSOR hwnd:003809F8 nHittest:HTCLIENT
wMouseMsg:WM_LBUTTONUP
<00784> 003809F8 R WM_SETCURSOR fHaltProcessing:False
<00785> 003809F8 P WM_LBUTTONUP fwKeys:0000 xPos:17 yPos:10
 
J

Jason Allred

I found the source of the problem. There is a System.Windows.Forms.Panel on
one of the TabPages. In the Form's Paint event handler I call the line:
"Me.panCannedMessages.Focus()". When this line is commented out, everything
works just fine. If I had to speculate wildly, I'd say that the Paint event
is raised before the button Click event, and somehow the click event gets
sidetracked or lost. The Spy++ log I posted seems to corroborate this wild
theory...

<00757> 003809F8 P WM_PAINT hdc:00000000
<00758> 003809F8 S WM_KILLFOCUS hwndGetFocus:001D0A2A
<00759> 003809F8 S WM_CAPTURECHANGED hwndNewCapture:00000000
<00760> 003809F8 R WM_CAPTURECHANGED
 
L

Linda Liu [MSFT]

Hi Jason,

Thank you for your detailed feedback.
If I had to speculate wildly, I'd say that the Paint event is raised
before the button Click event, and somehow the click event gets sidetracked
or lost.

I agree with you that the Paint event is raised before the Button's Click
event.

Can you regard the method of commenting out the line
'Me.panCannedMessages.Focus()' as a workaround to your problem?

If not, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu [MSFT]

Hi Jason,

How about the problem now?

If you need our further assistance, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

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