locked
MouseClick and MouseHover event of User Control! RRS feed

  • Question

  • I created a ImageButton by myself with a PictureBox to display a picture of the button and a Label to display a text of the button.

    Problem is when i use this Control and register MouseClick and MouseHover in any Windows Forms project i have created, these events nerver fired. I event try register two events in the User Control itself, it still not work. 

    Can someone help me, please :D

    Thanks you for reading and helping me!

    Sunday, December 23, 2012 5:05 PM

Answers

  • A System.Windows.Forms Button has a property "Image" for that ;)

    If you still prefere to create a custom control, then a custom event should work for that (I need the code to see why yours doesn't work):

    How I'd probebly do it (This code is not tested, so might contain spelling errors) :

    public class MyCustomButton : Control
    {
            private delegate void MyClickEvent(object sender, EventArgs e); // remembers the functions which have to executed
            public event MyClickEvent MyClick; 
    
            public MyCustomButton()
                    : base()
            {
                    //picturebox and label code
            }
    
    
            public void OnMyClickEvent(object sender, EventArgs e)
            {
                    if (MyClick != null)
                            MyClick(sender, e);//execute event
            }    
    
            private void pb_Click(object sender, EventArgs e)
            {
                     OnMyClickEvent(sender, e);
            }
    
            private void lbl_Click(object sender, EventArgs e)
            {
                     OnMyClickEvent(sender, e);
            }
    }
    
    public partial class Form1
    {
            public Form1()
            {
                    myCustomButton.MyClick += DoThisOnClick;
            }
    
            private void DoThisOnClick(object sender, EventArgs e)
            {
                    //this will be executed on click
            }
    }

    Have fun coding ;)
    • Marked as answer by Thắng Trần Monday, December 24, 2012 7:40 AM
    Monday, December 24, 2012 12:26 AM

All replies

  • Hi,

    do you handle the events for the sub-controls (picturebox and label)? Maybe raise the same event for the Control from these handlers.

    Regards,

      Thorsten

    Sunday, December 23, 2012 7:44 PM
  • A System.Windows.Forms Button has a property "Image" for that ;)

    If you still prefere to create a custom control, then a custom event should work for that (I need the code to see why yours doesn't work):

    How I'd probebly do it (This code is not tested, so might contain spelling errors) :

    public class MyCustomButton : Control
    {
            private delegate void MyClickEvent(object sender, EventArgs e); // remembers the functions which have to executed
            public event MyClickEvent MyClick; 
    
            public MyCustomButton()
                    : base()
            {
                    //picturebox and label code
            }
    
    
            public void OnMyClickEvent(object sender, EventArgs e)
            {
                    if (MyClick != null)
                            MyClick(sender, e);//execute event
            }    
    
            private void pb_Click(object sender, EventArgs e)
            {
                     OnMyClickEvent(sender, e);
            }
    
            private void lbl_Click(object sender, EventArgs e)
            {
                     OnMyClickEvent(sender, e);
            }
    }
    
    public partial class Form1
    {
            public Form1()
            {
                    myCustomButton.MyClick += DoThisOnClick;
            }
    
            private void DoThisOnClick(object sender, EventArgs e)
            {
                    //this will be executed on click
            }
    }

    Have fun coding ;)
    • Marked as answer by Thắng Trần Monday, December 24, 2012 7:40 AM
    Monday, December 24, 2012 12:26 AM
  • Thanks you, your code was very helpful!

    But can i aks your one more question? Is there any better way to implement an event of User Control?
    Monday, December 24, 2012 7:42 AM
  • I think my event example is the most accepted way to do it in C# . Another way to do it, as I assume you'll find more interesting, is defining your PictureBox and Label as public (this option is called Modifiers in the properties list of a control, just set this from private to public).
    You can now access your label and picturebox like this (again this code is not tested):

    public partial class Form1
    {
       MyCustomButton myBtn = new MyCustomButton();
       public Form1()
       {
          this.myBtn.pb.Click += DoThisOnClick; // Add "DoThisOnClick"- function to the Click event of the PictureBox(pb) element
       }
    
       public void DoThisOnClick(object sender, EventArgs e)
       {
          //Code to be executed
       }
    }
    Have fun ;)
    Monday, December 24, 2012 12:20 PM
  • Thanks you very very very much, hehehe!

    I done it :)
    Tuesday, December 25, 2012 3:22 AM