Capturing the mousedown event for a groupbox--possible?
Is there a way to capture the mousedown event for a groupbox? According to MSDN documentation the mousedown event for the groupbox "is not intended to be used directly from your code." For various reasons, I need to be able to capture the mousedown event from anywhere on my form, and the groupbox seems to be the only control on my form that does not capture this event.
The quick solution I am using is dropping a panel into the groupbox and set the dock to fill and set the background to transparent. However, the mousedown is still not captured when occurring on the groupbox's border or header. I am wondering if there is a better way to this bandaid approach.
Charles
Hi,
Try overriding the WndProc for that groupbox.
Catch the WM_LBUTTONDOWN message and add your code there.
I don't know exactly where is the client and non-client area (and is there a non-client area) in a groupbox but to be more explicit you may catch the WM_NCLBUTTONDOWN message and check the return value of WPARAM(it contains the WM_NCHITTEST value)
for instanse:
...................
case 0x00A1://WM_NCLBUTTONDOWN
if(m.WParam.ToInt32() == 1//HTCLIENT)
{
//your code goes here
}
else//you have non-client click
{
//do something
}
....................
Hope this helps!
Gogou
Gogou's approach was my first inclination, too. However, it turns out this is considerably easier than this. The MouseDown event is actually on GroupBox, and it fires fine. However, because it has the EditorBrowsableState.Advanced attribute, it's harder to find. You can just add this code to your form, and it will work:
<B>VB:</b>
Private Sub GroupBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GroupBox1.MouseDown
MsgBox(CStr(e.X) + ", " + CStr(e.Y))
End Sub
<B>C#:</b>
this.groupBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.GroupBox1_MouseDown);
private void GroupBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
MessageBox.Show(e.X.ToString() + ", " + e.Y.ToString());
}
Cheers,
Bri