UserControl Click Event Problem

KG Gamer Forever 21 Reputation points
2021-02-14T09:30:47.263+00:00

Hello,
I have a problem with a custom button that I am trying to make in visual basic using a usercontrol. It consists of a picturebox and a label. I want to call the click event from my main form, but the picturebox and the label don't do anything when I click them. Only the background area of the usercontrol does what I want it to do. Any ideas?

Thanks in advance!

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,714 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 84,471 Reputation points
    2021-02-14T15:01:05.233+00:00

    I did a quick test with a Delegate and it works normally, I receive the click from the PictureBox =>
    (MyUserControl1 added from Designer)

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub PicClick() Handles MyUserControl1.PictureBoxClick
            Console.Beep(8000, 10)
        End Sub
    
    End Class
    
    
    Public Class MyUserControl
        Inherits System.Windows.Forms.UserControl
    
        Private label1 As System.Windows.Forms.Label
        Private WithEvents pictureBox1 As System.Windows.Forms.PictureBox
    
        Private components As System.ComponentModel.IContainer
    
        Public Delegate Sub PictureBoxClickHandler()
        Public Event PictureBoxClick As PictureBoxClickHandler
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Public Sub InitializeComponent()
            components = New System.ComponentModel.Container()
            label1 = New System.Windows.Forms.Label()
            pictureBox1 = New System.Windows.Forms.PictureBox()
    
            pictureBox1.Name = "PictureBox1"
            pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
            pictureBox1.Location = New System.Drawing.Point(10, 10)
            pictureBox1.Size = New System.Drawing.Size(90, 75)
    
            label1.Text = "Label"
            label1.Location = New System.Drawing.Point(10, 95)
            label1.Size = New System.Drawing.Size(200, 20)
    
            Controls.AddRange(New System.Windows.Forms.Control() {label1, pictureBox1})
    
            Size = New System.Drawing.Size(300, 250)
        End Sub
    
        Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles pictureBox1.Click
            OnPictureBoxClick()
        End Sub
    
        Protected Overridable Sub OnPictureBoxClick()
            RaiseEvent PictureBoxClick()
        End Sub
    End Class
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,401 Reputation points
    2021-02-14T11:47:33.433+00:00

    Hello,

    The easy way is to set the modifier property of the button in the user control from friend to public then call PerformClick on the button. Otherwise create a public procedure that does what the button would do then call the sub.

    Base example (I realize there is a picture box but for this I use a label, the concept is how to access a click event)

    Public Class UserControl1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ChangeLabel()
        End Sub
        Public Sub ChangeLabel()
            Label1.Text = Now.ToShortTimeString()
        End Sub
    End Class
    

    In the above code if the modifier for the button is friend than call ChangeLabel e.g. as shown in Button1_Click while if the button modifier is button is public call PerformClick.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            MyUserControl.ChangeLabel()
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            MyUserControl.Button1.PerformClick()
        End Sub
    End Class
    

    Full source

    https://github.com/karenpayneoregon/forum-questions/tree/master/UserControlDemo


  2. Viorel 116.6K Reputation points
    2021-02-14T15:13:34.663+00:00

    If you really want to allow clicking the free area of User Control, and also accept clicking on picturebox and label, then try adding this
    code to User Control:

    Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
       AddHandler PictureBox1.Click, Sub(s, a)
          OnClick(Nothing)
       End Sub
    
       AddHandler Label1.Click, Sub(s, a)
          OnClick(Nothing)
       End Sub
    
    End Sub
    

    (Adjust the names according to your controls).

    If only picturebox and label should be clickable, then the solution is different.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.