我现在动态创建了几个面板和按钮,我需要在按钮点击事件上访问面板。
这就是我创建动态按钮和Pannel的方式
While rst.Read()
Dim panel As New Panel
Dim panel1 As New Panel
Dim mylink As New LinkLabel
Dim btn As New Button
Dim Tooltip As New ToolTip()
Tooltip.SetToolTip(mylink, rst!form_name)
mylink.Name = rst!form_name
AddHandler mylink.Click, AddressOf HandleDynamicLinkLabelClick
AddHandler btn.Click, AddressOf HandleDynamicButtonClick
mylink.Dock = DockStyle.Left
btn.Name = rst!form_name
btn.Tag = rst!form_name
panel1.Tag = rst!form_name
btn.Text = "<"
panel.Controls.Add(mylink)
panel.Controls.Add(btn)
panel1.Name = rst!form_name
panel1.Controls.Add(lbl)
Me.FlowLayoutPanel1.Controls.Add(panel)
Me.FlowLayoutPanel1.Controls.Add(panel1)
End While
rst.Close()
这是动态按钮点击事件的代码,我需要从上面的过程
访问pannel1 Private Sub HandleDynamicButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
End Sub
答案 0 :(得分:0)
您需要能够积极地了解&#39;你动态创建的控件的名称是什么,所以尝试这样的事情:
Private Name1 As String
Private Name2 As String
while rst.read
Dim panel As New Panel
Dim panel1 As New Panel
Dim mylink As New LinkLabel
Dim btn As New Button
Dim Tooltip As New ToolTip()
Tooltip.SetToolTip(mylink, rst!form_name)
mylink.Name = rst!form_name
AddHandler mylink.Click, AddressOf HandleDynamicLinkLabelClick
AddHandler btn.Click, AddressOf HandleDynamicButtonClick
mylink.Dock = DockStyle.Left
Name1 = cstr(rst!form_name)
btn.Name = Name1
btn.Tag = rst!form_name
panel1.Tag = rst!form_name
btn.Text = "<"
panel.Controls.Add(mylink)
panel.Controls.Add(btn)
Name2 = Cstr(rst!form_name)
panel1.Name = Name2
panel1.Controls.Add(lbl)
Me.FlowLayoutPanel1.Controls.Add(panel)
Me.FlowLayoutPanel1.Controls.Add(panel1)
end while
Private Sub HandleDynamicButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
Select Case btn.Name
Case Name1
'do something here
End Select
End Sub
答案 1 :(得分:0)
您已经在按钮名称中存储了“rst!form_name”:
btn.Name = rst!form_name
那么为什么不将Panel存储在Tag中呢?
btn.Tag = panel1
然后你可以在你的处理程序中检索它:
Private Sub HandleDynamicButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
Dim panel1 As Panel = DirectCast(btn.Tag, Panel)
' ... use "panel1" somehow ...
End Sub