1

I'm trying to create a custom control which fires an even on click. My control is just a panel with a couple of labels and a picturebox inside.

The click works perfectly, the only issue is that I have to click the background of the control and if I press on the picturebox, is not working.

I've added the on click event to the control, but I would like to press in every place of it to trigger the event, not just the background of the panel.

I thought about adding a transparent object that covers entirely the control. I actually don't like this idea, however, I've tried with a picturebox, but i cannot see through it. It's not transparent. I can just see the panel background but It covers the labels and the image.

Thanks for the support.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Dario Emerson
  • 127
  • 4
  • 13

2 Answers2

3

If you just have a couple of objects in your panel, you can hook the Click event of all objects it contains to the same event handler, there is nothing wrong doing this.

public class MyUserControl : UserControl
{
    public event Action<MyUserControl> MyControlClick

    public string ID {get; set;}

    public MyUserControl()
    {
        InitializeComponents();

        // The same event handler code will be used for the three controls
        myPictureBox.Click += global_Click;
        myLabel1.Click += global_Click;
        myLabel2.Click += global_Click;
        this.Click += global_Click;
    }

    void global_Click(object sender, EventArgs e)
    {
        if (MyControlClick != null)
            MyControlClick(this);
    }
}

If you have a more important amount of objects, you can rely on this answer to create a truly transparent panel that handles clicks. The drawback is that you will have to detect which object has been clicked by using HitTest based on the mouse location.

On the form side :

aControl.MyControlClick += aControl_MyControlClick;

// ...

// This code is triggered when a MyUserControl is clicked
void aControl_MyControlClick(MyUserControl ctl)
{
    MessageBox.Show(ctl.ID);
}
Community
  • 1
  • 1
Larry
  • 17,605
  • 9
  • 77
  • 106
  • Actually I prefere to hook the same click event, It's easier. Is there a way to inherit the event on child controls? – Dario Emerson Nov 02 '14 at 22:32
  • I agree :) Using the transparent panel is worth with many controls. For example, I am using this is a project to enable the user to move and to resize controls that are created dynamically. – Larry Nov 02 '14 at 22:34
  • I'm creating a server list for a game, and every server got server name, motd and logo/image. so this custom control contains this three elements and you could click wherever you like to trigger the "select server" action. I would love if there's something to automate the click hook assignment. something like telling the childs to inherit from the parent... – Dario Emerson Nov 02 '14 at 22:37
  • Well it can be quickly done using code instead of the designer (I edited my answer). The inherit thing would be interresting if you plan to add or remove controls to your panel dynamically. – Larry Nov 02 '14 at 22:46
  • Yeah, I've just done that.. but I'm now facing another issue: the global_Click method contains the sender, which is not anymore the Control itself. I used the sender parameter to get a property from the sender, which actually was the server id of the clicked server in the list.. – Dario Emerson Nov 02 '14 at 22:47
  • Using an Action delegate (which basically an eventhandler is) is perfectly valid and very handy. Have a look on the edit: this enables you to pass a property value directly. By this way you wont have to get the sender and cast it in the parent screen. – Larry Nov 02 '14 at 22:52
  • Actually I've not understand the use of the Action. I'm still missing the click on the control with that code.. Where and how can i retrive the value of the ID? – Dario Emerson Nov 02 '14 at 23:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64154/discussion-between-larry-and-dario-emerson). – Larry Nov 03 '14 at 07:56
0

Actually! You cannot raise any event to the element in the Usercontrol unless you have to apply own method to your usercontrol or you can disable the element in the usercontrol but it will change the color of that element but It will raise the click event when you click your control.

borrom
  • 441
  • 1
  • 5
  • 16