0

I've a user control, pretty simple just a label and a picturebox, they are docked that you can't even see the underlying most of the events never occurs (click, hover and others)

i think (i could be so wrong), it's because the events won't be triggered as they are being clicked either on the label or the picturebox and not on the background

i need somehow to redirect these events (i.e create that events in the usercontrol and somehow redirect them to my code and with my code i mean the form that uses them) or any other way to capture those events

i've seen like 5 topics about the same problem and none were solved, any workaround ?

  • 1
    Workaround could be to forward events. Add new event to your control and rise it in controls event handlers. – Renatas M. Aug 21 '14 at 13:35
  • i know and that should work but now i need to subscribe to both events, my custom and other native events, also i would have to create a custom event for each native event, i will if i have to, but lets see if there is more magical elegant solution :) –  Aug 21 '14 at 13:41
  • 1
    I see. Look what I found [here](http://stackoverflow.com/questions/547172/pass-through-mouse-events-to-parent-control?answertab=votes#tab-top). Looks like "transparent" control might fit your needs and its more magical :) – Renatas M. Aug 21 '14 at 13:48

3 Answers3

2

You need to redirect your Click event of your label and pictureBox to the OnClick event of your user control using this code:

public UserControl1()
{
   InitializeComponent();

   this.label1.Click += myClickEvent;
   this.pictureBox1.Click += myClickEvent;
}

private void myClickEvent(object sender, EventArgs e)
{
   this.OnClick(e);
}
Matin Lotfaliee
  • 1,745
  • 2
  • 21
  • 43
  • + This is nice solution. You don't need any custom events. – Renatas M. Aug 21 '14 at 14:00
  • okay sorry I've removed my old comment (my bad I've got confused, had to delete it, didn't knew you posted by then) your answer is the best, easiest, most elegant :) thank you –  Aug 21 '14 at 14:00
1

If you can Inherit the Label and PictureBox, Inherit them and override WndProc and do the following.

protected override void WndProc(ref Message m)
{
    if (m.Msg == (int)WindowsMessage.WM_NCHITTEST)
    {
        m.Result = (IntPtr)(-1);//Transparent
        return;
    }
    base.WndProc(ref m);
}

If not, Use this

public partial class MyUserControl : UserControl
{
    private TransparentWindow label;
    private TransparentWindow pic;

    public MyUserControl()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        label = new TransparentWindow(label1);
        pic = new TransparentWindow(pictureBox1);
    }
}

class TransparentWindow : NativeWindow
{
    public TransparentWindow(Control control)
    {
        this.AssignHandle(control.Handle);
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == (int)WindowsMessage.WM_NCHITTEST)
        {
            m.Result = (IntPtr)(-1);//Transparent
            return;
        }
        base.WndProc(ref m);
    }
}

WindowMessage enumeration can be found here

Related question

Community
  • 1
  • 1
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • @MatinLotfaliee [I remember you](http://stackoverflow.com/questions/24943088/textbox-not-clickable-but-editable#comment38764467_24943375) Still you have revenge? – Sriram Sakthivel Aug 21 '14 at 14:09
  • Just wanted to say hello my friend. I know I am not as knowledgeable as you, but your comment was not fair that time. – Matin Lotfaliee Aug 21 '14 at 14:16
  • @MatinLotfaliee Forget about it dude. If you can teach me something I'm happy to learn. Sameway if I can teach you something also am happy.. That day I thought it was ugly and said that. If you provide good answer I'll upvote that. Nothing to take it personal, I hope you'll one day realize it is really not elegant, that time you'll remember me. And... Hello my friend.. :) – Sriram Sakthivel Aug 21 '14 at 14:20
  • Well, I mean your rudeness shocked me that time as a newcomer. I realized my answer was ugly just after reading your reason and testing it myself. BTW, as an expert in WindowMessages can you help me with [this one](http://stackoverflow.com/questions/24641829/formborderstyle-none-removes-the-native-open-effect-of-windows-8/24707049#24707049)? – Matin Lotfaliee Aug 21 '14 at 15:08
1

The answer from Matin Lotfaliee is definetly correct, but in my case, where i want to forward an event from one control to 'OtherControl', you may prefer one-liners:

this.OtherControl.Click += (s, e) => { this.OnClick(e); };
jaufer.k
  • 90
  • 1
  • 7