获得了一个设计有多个Label和PictureBox的UserControl。
现在,我确实通过Controls.Add
将此UserControl集成到一个Panel中,在那里他们显示主Form1。
现在,我想在用户控件被点击或悬停后引发一个事件。
如果我通过AddHandler taskItem.MouseClick, AddressOf meClick
进行操作,但是只有当我单击此Usercontrol的空白区域而不是在标签或PictureBox上时,才会触发它。
目标是使用事件从标签中删除单击的UserControl。
编辑:
这是我的UserControl的样子:
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
在这里,我将UserControl集成到面板中
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
答案 0 :(得分:1)
事件就是这样工作的。就像在您单击表单上的Click
时,表单不会引发Button
事件一样。如果您希望UC在单击子控件时引发MouseClick
事件,则需要在内部处理该子控件的事件,然后自己引发UC的事件。例如
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