5

This is probably a very simple question, but for some reason, even the right way to web search for the answer eludes me...

I'm trying to create a user control that consists of a few labels and progress bars. However, I want the entire control to have a "Click" event that is raised no matter what item inside the control is clicked on. I've created a "HandleClick" procedure that is assigned to each control:

    private void HandleClick(object sender, EventArgs e)
    {
        // Call the callback function, if we were asked to
        if (OnClick != null)
        {
            EventArgs ee = new EventArgs();
            OnClick(this, ee);
        }
        else
        {
            MessageBox.Show("OnClick was null!");
        }
    }

OnClick in this instance is a variable defined at control level:

    public new event EventHandler OnClick;

Now, this only works properly on the form. On one label it shows the MessageBox, and then calls the event on the enclosing form. All the rest show the message box.

I get the feeling that this should be obvious, but an afternoon of frustration has left me feeling I'm missing something that should be self-evident, but when I see it I am going to feel like a complete buffoon... can anyone stop giggling at my daftness long enough to enlighten me where I've gone wrong?

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • You have to register the same click event for each of the controls. You also then call that event when the user control is clicked. – Security Hound Apr 28 '11 at 15:19
  • In case you're interested, this post helped solve it: http://stackoverflow.com/questions/1071579/user-control-click-windows-forms Basically, remove HandleClick, and the property and substitute this one instead: public new event EventHandler Click { add { base.Click += value; foreach (Control control in Controls) { control.Click += value; } } remove { base.Click -= value; foreach (Control control in Controls) { control.Click -= value; } } } –  Apr 28 '11 at 15:20

2 Answers2

10

Sorry about this - just putting an answer on in case someone googles it...

In case you're interested, this post helped solve it: User Control Click - Windows Forms… Basically, remove HandleClick, and the property and substitute this one instead:

public new event EventHandler Click
{
    add
    {
        base.Click += value;
        foreach (Control control in Controls)
        {
            control.Click += value;
        }
    }
    remove
    {
        base.Click -= value;
        foreach (Control control in Controls)
        { 
            control.Click -= value;
        }
    }
}
Community
  • 1
  • 1
  • Thanks alot, exactly what I#m looking for. – Kingpin Nov 23 '11 at 13:26
  • 1
    You should accept this answer to put some closure to this question – Mark Hall Mar 13 '12 at 02:59
  • 1
    Thank you!!! Please note that you should include a recursive add/remove for Controls that are inside Controls (For example Controls inside a Panel) – Gerhard Powell May 01 '12 at 14:47
  • @GerhardPowell Excellent point - in my case there weren't, so I didn't consider it but you will need to for flow layour panels, etc... –  Jun 11 '12 at 12:57
-1

I've never tried this with Windows Forms, but in other GUIs I've placed a transparent panel that covers the entire form so that when you click "on the form," the event actually goes to the panel control.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • I'm liking this idea, but I'd still have the problem that it only works when the underlying usercontrol is clicked, and not the controls on it are clicked, if you see what I mean - so by adding the panel I'd probably just have one big, non-clickable control... –  Apr 28 '11 at 15:09