If you want to pass the event from the UserControl to a parent container, like a Form, then you need to:
1. expose either a Method to a delegate (a method or function to call from within the UserControl) or
2. add a public Event property to the UserControl that the form can Listen to and raise it from within the UserControl when you want to notify the parent container. This is called event bubbling. ref:
Declaring and Raising Events - Visual Basic | Microsoft Learn[
^]
As you are a beginner, I would suggest option 2.
1. USerControl Designer
Partial Class UserControl1
Inherits System.Windows.Forms.UserControl
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
Private components As System.ComponentModel.IContainer
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
PictureBox1 = New PictureBox()
CType(PictureBox1, ComponentModel.ISupportInitialize).BeginInit()
SuspendLayout()
PictureBox1.BackColor = Color.Red
PictureBox1.Location = New Point(205, 104)
PictureBox1.Name = "PictureBox1"
PictureBox1.Size = New Size(246, 179)
PictureBox1.TabIndex = 0
PictureBox1.TabStop = False
AutoScaleDimensions = New SizeF(10F, 25F)
AutoScaleMode = AutoScaleMode.Font
Controls.Add(PictureBox1)
Name = "UserControl1"
Size = New Size(684, 432)
CType(PictureBox1, ComponentModel.ISupportInitialize).EndInit()
ResumeLayout(False)
End Sub
Friend WithEvents PictureBox1 As PictureBox
End Class
Adding the custom event to the UserControl:
Public Class UserControl1
Private Sub PictureBox1_MouseDown(
sender As Object,
e As MouseEventArgs)
Handles PictureBox1.MouseDown
RaiseEvent ShapeMouseDown(sender, e)
End Sub
Public Event ShapeMouseDown(sender As Object, e As MouseEventArgs)
End Class
Now when the Shape (PictureBox) event is raised, we bubble it up to the Form (Parent Container)
Now to handle the event. When you compile the project, then add the USerControl, you will see the custom event named
ShapeMouseDown
. We can double-tap and create an event handler in the Form. Here is the form designer snippet wiring up the event:
Private Sub InitializeComponent()
UserControl11 = New UserControl1()
SuspendLayout()
UserControl11.Location = New Point(12, 2)
UserControl11.Name = "UserControl11"
UserControl11.Size = New Size(776, 436)
UserControl11.TabIndex = 0
AutoScaleDimensions = New SizeF(10F, 25F)
AutoScaleMode = AutoScaleMode.Font
ClientSize = New Size(800, 450)
Controls.Add(UserControl11)
Name = "Form1"
Text = "Form1"
ResumeLayout(False)
End Sub
Friend WithEvents UserControl11 As UserControl1
And now the Form code-behind:
Public Class Form1
Private Sub UserControl11_ShapeMouseDown(
sender As Object,
e As MouseEventArgs)
Handles UserControl11.ShapeMouseDown
MsgBox("Shape Mouse Down Event")
End Sub
End Class
Now when you Mouse-Down on the Shape (PictureBox), the
ShapeMouseDown
event is raised by the USerControl, captured by the Form, and a Message Box is displayed.
Hope this is clear enough.
UPDATE
I am not sure why you are having so much difficulty. Here are the complete file dumps:
1.
Form1.Designer.vb
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class Form1
Inherits System.Windows.Forms.Form
<System.Diagnostics.DebuggerNonUserCode()>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
Private components As System.ComponentModel.IContainer
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
UserControl11 = New UserControl1()
SuspendLayout()
UserControl11.Location = New Point(12, 2)
UserControl11.Name = "UserControl11"
UserControl11.Size = New Size(776, 436)
UserControl11.TabIndex = 0
AutoScaleDimensions = New SizeF(10.0F, 25.0F)
AutoScaleMode = AutoScaleMode.Font
ClientSize = New Size(800, 450)
Controls.Add(UserControl11)
Name = "Form1"
Text = "Form1"
ResumeLayout(False)
End Sub
Friend WithEvents UserControl11 As UserControl1
End Class
2.
Form1.vb
Public Class Form1
Private Sub UserControl11_ShapeMouseDown(sender As Object, e As MouseEventArgs) Handles UserControl11.ShapeMouseDown
MsgBox("Shape Mouse Down Event")
End Sub
End Class
3.
UserControl1.Designer.vb
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class UserControl1
Inherits System.Windows.Forms.UserControl
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
Private components As System.ComponentModel.IContainer
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
PictureBox1 = New PictureBox()
CType(PictureBox1, ComponentModel.ISupportInitialize).BeginInit()
SuspendLayout()
PictureBox1.BackColor = Color.Red
PictureBox1.Location = New Point(205, 104)
PictureBox1.Name = "PictureBox1"
PictureBox1.Size = New Size(246, 179)
PictureBox1.TabIndex = 0
PictureBox1.TabStop = False
AutoScaleDimensions = New SizeF(10F, 25F)
AutoScaleMode = AutoScaleMode.Font
Controls.Add(PictureBox1)
Name = "UserControl1"
Size = New Size(684, 432)
CType(PictureBox1, ComponentModel.ISupportInitialize).EndInit()
ResumeLayout(False)
End Sub
Friend WithEvents PictureBox1 As PictureBox
End Class
4.
UserControl1.vb
Public Class UserControl1
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
RaiseEvent ShapeMouseDown(sender, e)
End Sub
Public Event ShapeMouseDown(sender As Object, e As MouseEventArgs)
End Class
That is all of the code. I have done no formatting and just dumped the code. Create a new project and add a
UseerControl1. then copy and paste the code, then run.