Got a UserControl designed with several Labels and PictureBoxes.
Now I do integrate this UserControl via Controls.Add
to a Panel where they get displayed the main Form1.
Now I like to raise an event once a UserControll is clicked or hovered.
If I do it via AddHandler taskItem.MouseClick, AddressOf meClick
but its only triggered when I click the empty space of this Usercontrol and not on a label or PictureBox.
The goal is to use the Event to remove the clicked UserControl from the Label.
EDIT:
This is how my UserControl looks like:
Public Class Taks
Private Sub Taks_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each childControl As Control In Controls
AddHandler childControl.MouseClick, AddressOf Form1.meClick
Next
End Sub
Public Sub AddTasks(ByVal task_label As TaskLabels, ByVal show_seperator As Boolean, ByVal task_subject As String, ByVal task_message As String)
taskTitel.Text = task_subject
taskDesc.Text = task_message
If task_label = TaskLabels.General Then
taskImage.Image = My.Resources.Information_50px
End If
If task_label = TaskLabels.Important Then
taskImage.Image = My.Resources.Error_48px
End If
If task_label = TaskLabels.Critical Then
taskImage.Image = My.Resources.Box_Important_50px
End If
BunifuImageButton1.Hide()
End Sub
Enum TaskLabels
General = 0
Important = 1
Critical = 2
End Enum
End Class
And here I integrate the UserControl into a panel
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddTasks(Taks.TaskLabels.Critical, False, "Batterie Spannung", "low power")
AddTasks(Taks.TaskLabels.Important, False, "Wetter", "Das Wetter hat sich verschlechtert, Wind > 15km/h")
AddTasks(Taks.TaskLabels.General, False, "Server", "Auslastung liegt bei 12%")
AddTasks(Taks.TaskLabels.Important, False, "Temperature", "Temperatur der Proben im Kritischen Bereich")
End Sub
Sub AddTasks(ByVal taksLabels As Taks.TaskLabels, ByVal task_sep As Boolean, ByVal taskTitle As String, ByVal TaskDesc As String, Optional ByVal toFront As Boolean = False)
Dim taskItem As New Taks
taskItem.AddTasks(taksLabels, task_sep, taskTitle, TaskDesc)
taskItem.Dock = DockStyle.Top
AddHandler taskItem.MouseClick, AddressOf meClick
taskItem.Cursor = Cursors.Hand
Panel1.Controls.Add(taskItem)
End Sub
Public Sub meClick(sender As Object, e As MouseEventArgs)
If MessageBox.Show("delete Event?", "Question", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
Panel1.Controls.Remove(sender)
End If
BunifuVScrollBar1.Maximum = Panel1.Height
End Sub
End Class
That's how events work. It's just like how a form doesn't raise a Click
event when you click a Button
on it. If you want the UC to raise a MouseClick
event when a child control is clicked on then you need to handle the event of the child control internally and then raise the UC's event yourself. E.g.
Public Class UserControl1
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each childControl As Control In Controls
'Filter specific control types if desired.
If TypeOf childControl Is Label OrElse TypeOf childControl Is Panel Then
AddHandler childControl.MouseClick, AddressOf ChildControls_MouseClick
End If
Next
End Sub
Private Sub ChildControls_MouseClick(sender As Object, e As MouseEventArgs)
Dim childControl = DirectCast(sender, Control)
'Translate location on child control to location on user control.
Dim translatedLocation = PointToClient(childControl.PointToScreen(e.Location))
Dim args As New MouseEventArgs(e.Button,
e.Clicks,
translatedLocation.X,
translatedLocation.Y,
e.Delta)
'Raise the event of the user control.
OnMouseClick(args)
End Sub
End Class