0

I made a user control with the combination of a panel and two label. I added mouseenter and mouseleave event on the user control. I placed the user control on form. I want to open a new form when the control is clicked. But default click or mouseclick event don't works. How can I solve This?

Here is user control code:

public partial class group_control : UserControl
{

    public group_control()
    {
        InitializeComponent(); 
    }

    public void setname(string s)
    {
        namelbl.Text = s;
    }

    public void setno(int n)
    {
        no_acc.Text = n.ToString();
    }

    private void logo_MouseEnter(object sender, EventArgs e)
    {
        logo.BackColor = System.Drawing.Color.Yellow;
        no_acc.Visible = true;
        label1.Visible = true;
        namelbl.Visible = false;
    }

    private void logo_MouseLeave(object sender, EventArgs e)
    {
        logo.BackColor = System.Drawing.Color.Gray;
        no_acc.Visible = false;
        label1.Visible = false;
        namelbl.Visible = true;
    }

    private void logo_Click(object sender, EventArgs e)
    {
        Program.cur_grp = namelbl.Text;
    }

    private void no_acc_Click(object sender, EventArgs e)
    {
        logo_Click(sender, e);
    }

    private void label1_Click(object sender, EventArgs e)
    {
        logo_Click(sender, e);
    }

    private void namelbl_Click(object sender, EventArgs e)
    {
        logo_Click(sender, e);
    }

    private void no_acc_MouseEnter(object sender, EventArgs e)
    {
        logo_MouseEnter(sender, e);
    }

    private void namelbl_MouseEnter(object sender, EventArgs e)
    {
        logo_MouseEnter(sender, e);
    }

    private void label1_MouseEnter(object sender, EventArgs e)
    {
        logo_MouseEnter(sender, e);
    }

    private void no_acc_MouseLeave(object sender, EventArgs e)
    {
        logo_MouseLeave(sender, e);
    }

    private void label1_MouseLeave(object sender, EventArgs e)
    {
        logo_MouseLeave(sender, e);
    }

    private void namelbl_MouseLeave(object sender, EventArgs e)
    {
        logo_MouseLeave(sender, e);
    }
}

here is user control click event code:

private void group_control1_Click_1(object sender, EventArgs e)
    {
        new view_record().Show();
    }
  • Trim the code. Are all those MouseLeave events necessary for us to see the issue? Put a debugger stop on your click line, see if it fires. – LarsTech Aug 12 '16 at 13:58
  • Are you clicking on panel or labels that were added in your usercontrol? – Kamalesh Wankhede Aug 12 '16 at 14:00
  • @user12572 I am clicking in panel. – Md. Rasedur Rahman Roxy Aug 12 '16 at 14:03
  • Find usages of your xxxx_Click event handler. (In Visual Studio - right click on event hadler name -> FindAllReferences) If found one reference only, you need to add your event hadler to your controls. – Alexander Kiselev Aug 12 '16 at 14:04
  • It looks like you've defined the handlers, but do you have anything actually listening for the events? Are the event assignments handled in the IDE? If the event isn't firing, it's likely that it's not even wired up. – Joel Etherton Aug 12 '16 at 14:14

1 Answers1

0

If you are clicking on panel then panel will get mouse click event. If you want that event on your usercontrol then your panel should be transparent to mouse clicks. This is how you do it;

public class ClickTransparentPanel : Panel
{
    protected override void WndProc(ref Message m)
    {
        // To make panel "transparent" 
        // to mouse clicks i.e. Mouse clicks pass through this window
        switch (m.Msg)
        {
            case (int) Win32Constants.WM_NCHITTEST:
                m.Result = new IntPtr((int) Win32Constants.HTTRANSPARENT);
                return;
        }

        base.WndProc(ref m);
    }
}

where

WM_NCHITTEST = 0x0084,

HTTRANSPARENT = -1
Kamalesh Wankhede
  • 1,475
  • 16
  • 37
  • I didn't understand. Would you please elaborate? I want to use this control in an window form to open another form. – Md. Rasedur Rahman Roxy Aug 12 '16 at 14:11
  • If you use this ClickTransparentPanel as panel you are adding in your usercontrol, your usercontrol will get all the click events. In your click event of usercontrol, you can call AnotherFormObj.Show(). – Kamalesh Wankhede Aug 12 '16 at 14:17
  • You have already added usercontrol's click event as "group_control1_Click_1". That event will work as you want if you use above mentioned panel on your usercontrol instead of using system's Panel class. – Kamalesh Wankhede Aug 12 '16 at 14:20