向动态面板添加鼠标处理程序 [英] add mouse handler to dynamic panels
本文介绍了向动态面板添加鼠标处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好我有从db动态创建的面板我想添加一个鼠标处理程序,但下面的代码只识别主窗体而不是面板作为发送者。
Hello I have panels created dynamically from a db I would like to add a mouse handler but the below code only recognize the main form not the panel as sender.
Public Function creation_panel(ID As String, Name As String, Ext As String, Role As String, Status As String, LastActionTime As String) As Panel
Dim newPanel As New Panel
Try
With newPanel
.Name = "Panel_dyna_" + i.ToString
.Size = New Size(150, 35)
If Status = "Ready" Then
.BackColor = Color.DarkGreen
ElseIf Status = "Away" Then
.BackColor = Color.DarkOrange
End If
AddHandler newPanel.MouseDown, AddressOf DynaPanel_MouseDown
.Visible = True
' .Location = New System.Drawing.Point(default_x, default_y + 25)
End With
i += 1
' default_y += 25
Debug.WriteLine(newPanel.Name)
list_panel.Controls.Add(newPanel)
Panel_collec.Add(newPanel)
creation_label_db_content(newPanel, ID, Name, Ext, Role, Status, LastActionTime)
Private Sub DynaPanel_MouseDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseDown
' Handle your Button clicks here
' If e.Button = MouseButtons.Left Then
Debug.Print(sender.name) '>>> shows the sender (only main returned when clicking the mainform and no reaction from the panels when clicking on it)
' End If
End Sub
非常感谢
Many thanks
推荐答案
to完成此操作:
(以其中一个标签为例)
to complete this :
(by the example of one of these Labels)
With newLabel_Name
.Name = "Label_dyna_Name" + ID
.Font = New System.Drawing.Font("Tahoma", 7.25!, System.Drawing.FontStyle.Bold)
.Text = Name.ToUpper
.Dock = DockStyle.Fill
.TextAlign = ContentAlignment.BottomRight
.Size = New System.Drawing.Size(0, 13)
.ForeColor = Color.White
.Parent = panel ' replaces the panel.Controls.Add(newLabel_Name)
.AddHandler .MouseDown, AddressOf DynaPanel_MouseDown
End With
但注意:
处理Panel时,manuel创建的Handler不会自动删除。为此,你必须通过自己的Dispose-Method(或Detach-Method)删除它。
But Attention :
This manuel created Handler is not removed automaticly when disposing the Panel. For this you MUST remove it by your own Dispose-Method (or Detach-Method).
这篇关于向动态面板添加鼠标处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文