none
How to get all mouse clicks in child controls RRS feed

  • Question

  • I have a parent user control. I want to capture all mouse clicks within the control, even if the mouse click is on a control within my parent control. How do I do this?
    Tuesday, June 13, 2006 8:25 PM

Answers

  •  

    Add this code to your usercontrol(vb)

        Protected Overrides Sub OnControlAdded(ByVal e As System.Windows.Forms.ControlEventArgs)

            AddHandler e.Control.Click, AddressOf doclick

        End Sub

     

        Private Sub doclick(ByVal sender As Object, ByVal e As EventArgs)

            Me.OnClick(e)

        End Sub

     

        Protected Overrides Sub OnControlRemoved(ByVal e As System.Windows.Forms.ControlEventArgs)

            removeHandler e.Control.Click, AddressOf doclick

        End Sub

    Remco

     

    Wednesday, June 14, 2006 12:56 PM

All replies

  • You can try and look for WM_PARENTNOTIFY and WM_MOUSEACTIVATE event.

    What I suggest you do is,create a WinForms Application and then change the property of the project to Console App,so that you can see the Console Window.

     

    In your user control,

    protected override void WndProc(ref Message m)

    {

    Console.WriteLine(m);

    base.WndProc(ref m);

    }

     

    you will see all the messages coming up in the Console.When you click on an underlying control,you will find that these two messages are sent to the userControl.When WM_PARENTNOTIFY Occurs,notice WM_LBUTTONDOWN which tells you it was a mouseclick on the child control.

     

    Hope this helps

    Wednesday, June 14, 2006 3:59 AM
  •  BioGeek wrote:
    I have a parent user control. I want to capture all mouse clicks within the control, even if the mouse click is on a control within my parent control. How do I do this?

    do you mean al clicks get the same eventhandler

    Remco

    Wednesday, June 14, 2006 12:16 PM
  • I am thinking about the propagation step, not the consumption. So lets say I have a custom UserControl that contains 20 controls. I want to raise the UserControl's Click event whenever the mouse is clicked anywhere within the control. The problem, is that if the mouse is clicked within any on the controls in my parent control, the parent's click event is not raised.

    I could have an eventhandler in the parent that raises the click event and attach this handler to the click event of every sub-control, but I would have thought there would be an easier way.

    How do I get any mouse click that occurs within a control (even if the click occurred in a child control) to raise the control's click event?

    Wednesday, June 14, 2006 12:28 PM
  •  

    Add this code to your usercontrol(vb)

        Protected Overrides Sub OnControlAdded(ByVal e As System.Windows.Forms.ControlEventArgs)

            AddHandler e.Control.Click, AddressOf doclick

        End Sub

     

        Private Sub doclick(ByVal sender As Object, ByVal e As EventArgs)

            Me.OnClick(e)

        End Sub

     

        Protected Overrides Sub OnControlRemoved(ByVal e As System.Windows.Forms.ControlEventArgs)

            removeHandler e.Control.Click, AddressOf doclick

        End Sub

    Remco

     

    Wednesday, June 14, 2006 12:56 PM
  • In your userControl,put in this bit of code-

    protected override void WndProc(ref Message m)

    {

    //0x210 is WM_PARENTNOTIFY

    if (m.Msg == 0x210 && m.WParam.ToInt32() == 513) //513 is WM_LBUTTONCLICK

    {

    Console.WriteLine(m); //You have a mouseclick(left)on the underlying user control

    }

    base.WndProc(ref m);

    }

    Wednesday, June 14, 2006 1:19 PM
  • I will give it a try.
    Wednesday, June 14, 2006 1:27 PM
  • Curious,did this help you?
    Thursday, June 15, 2006 2:05 PM
  • The best solution is to override the WndProc as stated here. Adding click event handlers to your child conrols may not work because the child controls may have their own child controls (which will not fire the click event)..
    Monday, October 9, 2006 10:21 PM
  • Hi,

     

    If anyone is still here, I'd appreciate if you can comment my solution (or just use it). It almost works with all but one of the controls that I tried. Selecting an item from a combobox won't show any click. so I guess I should add a SelectedIndexChanged event to comboboxes or so.

     

    Code Snippet

    public class ControlItem

    {

    public Control c;

    public int l;

    }

     

     

    Code Snippet

    private void Form1_Load(object sender, EventArgs e)

    {

    //comboBox1.Items.AddRange(new string[] { "qwe", "asd", "zxc" });

    List<ControlItem> lc = new List<ControlItem>();

    ControlItem f = new ControlItem();

    f.c = this;

    f.l = 0;

    lc.Add(f);

    int cc = lc.Count;

    while (cc > 0)

    {

    ControlItem ci = lc[0];

    Console.WriteLine(ci.c.Name + " * " + ci.l.ToString());

    ci.c.MouseClick += new MouseEventHandler(c_MouseClick);

    ci.c.MouseWheel += new MouseEventHandler(c_MouseWheel);

    ci.c.KeyUp += new KeyEventHandler(c_KeyUp);

    lc.Remove(ci);

    if (ci.c.Controls.Count > 0)

    {

    foreach (Control c2 in ci.c.Controls)

    {

    ControlItem ci2 = new ControlItem();

    ci2.c = c2;

    ci2.l = ci.l + 1;

    lc.Add(ci2);

    }

    }

    cc = lc.Count;

    }

    }

     

     

    Code Snippet

    void c_KeyUp(object sender, KeyEventArgs e)

    {

    Console.WriteLine("Key pressed: " + (sender as Control).Name + " * " + e.KeyCode.ToString());

    }

    void c_MouseWheel(object sender, MouseEventArgs e)

    {

    Console.WriteLine("Mouse wheel: " + (sender as Control).Name + " * " + e.Location.ToString());

    }

    void c_MouseClick(object sender, MouseEventArgs e)

    {

    Console.WriteLine("Mouse click: " + (sender as Control).Name + " * " + e.Location.ToString());

    }

     

     

    Thanks,

     

    Marcelo

    Wednesday, June 18, 2008 9:43 AM