OnClick  
Author Message
Rocinante8





PostPosted: Windows Forms General, OnClick Top

Hello,

I'm having a problem with a custom control derived from usercontrol. After I put up (and close) a modeless dialog box and then switch parent forms the control no longer generates or receives OnClick events. Other events like MouseDown MouseUp still work.

I'm wondering how the OnClick event is generated (I don't think it is a standard windows message derived event) and if anyone has any ideas how it could be blocked.

Thanks,

Jeff

(Moderator: Thread moved to this forum for better responses)


Windows Forms13  
 
 
Bhanu Prakash Nunna - MSFT





PostPosted: Windows Forms General, OnClick Top

hi,

Do you wish to block the Onclick event or would you like to raise the event could you please be more clear

thank you,
bhanu.



 
 
Rocinante8





PostPosted: Windows Forms General, OnClick Top

Hello,

I want the event to continue to be raised. When my progam starts the OnClick is raised correctly, only after I open/close a modeless dialog and switch forms (the switch is triggered by a OnClick event) does the On Click event stop being raised. I don't understand how/where the onclick event is raised internally so it is hard for me to track this down.

Thanks,

Jeff


 
 
Rocinante8





PostPosted: Windows Forms General, OnClick Top

One additional thing I've noticed is that if I click on the parent form after the modeless dialog is closed, everything works fine. I tried setting the focus and activating the parent form in a Form.Closed event handler but that doesn't work.

Jeff


 
 
nobugz





PostPosted: Windows Forms General, OnClick Top

The OnClick() method for most controls (not all!) is called by the MouseUp message handler. There are a couple of internal control state flags that affect the outcome. One generates the DoubleClick event instead of the Click event. Another checks if the OnValidating event for another control cancelled the focus change. Another checks if the control was disposed. Yet another checks a state flag that I haven't reverse-engineered yet (0x8000000). The most interesting one in your case however is that it calls the WindowFromPoint() API function to map the X/Y mouse coordinates back to a window handle and generates the Click event only if that maps to the same control.

Yet another potentional source of this problem is that a control has captured the mouse. The Button control does that for example. It captures the mouse on the MouseDown event so it can be guaranteed to see the MouseUp event. You'll get in trouble if you implement a MouseDown event handler that will make the window disappear. The work-arounds in that case are to implement the MouseUp event instead. Or set the control's Capture property to false. Or call the ReleaseCapture() API function. If that is the source of your problem, take a good hard look at your code and make sure you properly dispose your controls; you may be leaking windows handles...


 
 
Rocinante8





PostPosted: Windows Forms General, OnClick Top

Hello Hans,

Thanks for the informative reply on how OnClick works. I looked using Reflector but I guess not in the right place. I wish MS would just give out the source code like they do for MFC and ATL...

I'm still not certain of the root cause but using spy I was able to determine that my first click sent a WM_MOUSEACTIVATE message with a LButtonDown as part of one of the parameters. By activating my form after the modeless dialog closes I was able to fix the problem, which may be related to a WindowFromPoint problem.

Another issue was that the underlying control I clicked to initiate the modeless dialog was an ActiveX control. Not sure if this is relevant or not.

Thanks again,

Jeff

P.S. you should put the gist of your reply in the MSDN Wiki