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

[688 byte] By [codefund.com] at [2007-12-16]
# 1
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

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 2
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

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 3
Hi, Brian

I haven't tested the OnMouseDown event for groupbox and according to author's post it wasn't reachable through .NET Framework. That's why I suggested WndProc. And I agree with you that just adding an event handler for MouseDown or overriding the OnMouseDown is much more simpler (and intuitive). I am for pure C# (if possible) too :))

Cheers,
Gogou

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...