vb.net 动态按钮单击事件处理程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7375061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:41:15  来源:igfitidea点击:

Dynamic button click event handler

vb.net

提问by Debasish Sahu

I've 100 buttons created dynamically in a form. How can I an add event handler to them?

我在一个表单中动态创建了 100 个按钮。如何向它们添加事件处理程序?

回答by Reed Copsey

You can use AddHandlerto add a handler for any event.

您可以使用AddHandler为任何事件添加处理程序。

For example, this might be:

例如,这可能是:

AddHandler theButton.Click, AddressOf Me.theButton_Click

回答by Jason S

Just to round out Reed's answer, you can either get the Buttonobjects from the Formor other container and add the handler, or you could create the Buttonobjects programmatically.
If you get the Buttonobjects from the Formor other container, then you can iterate over the Controlscollection of the Formor other container control, such as Panelor FlowLayoutPaneland so on. You can then just add the click handler with
AddHandler ctrl.Click, AddressOf Me.Button_Click(variables as in the code below),
but I prefer to check the type of the Controland cast to a Buttonso as I'm not adding click handlers for any other controls in the container (such as Labels). Remember that you can add handlers for any event of the Buttonat this point using AddHandler.
Alternatively, you can create the Buttonobjects programmatically, as in the second block of code below.
Then, of course, you have to write the handler method, as in the third code block below.

只是为了完善 Reed 的答案,您可以ButtonForm容器或其他容器中获取对象并添加处理程序,或者您可以通过Button编程方式创建对象。
如果您Button从该Form或其他容器中获取对象,则可以迭代ControlsForm或其他容器控件的集合,例如PanelorFlowLayoutPanel等。然后,您可以使用
AddHandler ctrl.Click, AddressOf Me.Button_Click(如下代码中的变量)添加点击处理程序,
但我更喜欢检查 的类型Control并将其强制转换为 a Button,因为我不会为容器中的任何其他控件添加点击处理程序(例如标签)。请记住,您可以的任何事件添加处理程序Button,在这一点上使用AddHandler
或者,您可以以Button编程方式创建对象,如下面的第二个代码块所示。
然后,当然,您必须编写处理程序方法,如下面的第三个代码块所示。

Here is an example using Formas the container, but you're probably better off using a Panelor some other container control.

这是一个Form用作容器的示例,但您最好使用 aPanel或其他容器控件。

Dim btn as Button = Nothing
For Each ctrl As Control in myForm.Controls
    If TypeOf ctrl Is Button Then
        btn = DirectCast(ctrl, Button)
        AddHandler btn.Click, AddressOf Me.Button_Click   ' From answer by Reed.
    End If
 Next

Alternatively creating the Buttons programmatically, this time adding to a Panelcontainer.

或者以Button编程方式创建s,这次添加到Panel容器中。

Dim Panel1 As new Panel()
For i As Integer = 1 to 100
    btn = New Button()
    ' Set Button properties or call a method to do so.
    Panel1.Controls.Add(btn)  ' Add Button to the container.
    AddHandler btn.Click, AddressOf Me.Button_Click   ' Again from the answer by Reed.
Next

Then your handler will look something like this

然后你的处理程序看起来像这样

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' Handle your Button clicks here
End Sub

回答by monami

@Debasish Sahu, your answer is an answer to another question, namely: how to know which button (or any other control) was clicked when there is a common handler for a couple of controls? So I'm giving an answer to this question how I usually do it, almost the same as yours, but note that also works without type conversion when it handles the same type of Controls:

@Debasish Sahu,您的回答是对另一个问题的回答,即:当有几个控件的通用处理程序时,如何知道单击了哪个按钮(或任何其他控件)?所以我要回答这个问题我通常是怎么做的,几乎和你的一样,但请注意,当它处理相同类型的控件时,它也可以在没有类型转换的情况下工作:

Private Sub btn_done_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim selectedBtn As Button = sender
    MsgBox("you have clicked button " & selectedBtn.Name)
End Sub

回答by Debasish Sahu

I needed a common event handler in which I can show from which button it is called without using switch case... and done like this..

我需要一个通用的事件处理程序,我可以在其中显示从哪个按钮调用它而无需使用 switch case ......并像这样完成......

  Private Sub btn_done_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)

    MsgBox.Show("you have clicked button " & CType(CType(sender,  _
    System.Windows.Forms.Button).Tag, String))

  End Sub

回答by Kristian

Some code for a variation on this problem. Using the above code got me my click events as needed, but I was then stuck trying to work out which button had been clicked. My scenario is I have a dynamic amount of tab pages. On each tab page are (all dynamically created) 2 charts, 2 DGVs and a pair of radio buttons. Each control has a unique name relative to the tab, but there could be 20 radio buttons with the same name if I had 20 tab pages. The radio buttons switch between which of the 2 graphs and DGVs you get to see. Here is the code for when one of the radio buttons gets checked (There's a nearly identical block that swaps the charts and DGVs back):

此问题的一些变体代码。使用上面的代码根据需要为我提供了我的点击事件,但后来我被困在试图找出哪个按钮被点击了。我的情况是我有一个动态数量的标签页。在每个标签页上(都是动态创建的)2 个图表、2 个 DGV 和一对单选按钮。每个控件都有一个相对于选项卡的唯一名称,但如果我有 20 个选项卡页,可能会有 20 个同名的单选按钮。单选按钮可在您看到的 2 个图形和 DGV 之间切换。以下是选中一个单选按钮时的代码(有一个几乎相同的块将图表和 DGV 交换回来):

   Private Sub radioFit_Components_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    If sender.name = "radioFit_Components" And sender.visible Then
        If sender.checked Then
            For Each ctrl As Control In TabControl1.SelectedTab.Controls
                Select Case ctrl.Name
                    Case "embChartSSE_Components"
                        ctrl.BringToFront()
                    Case "embChartSSE_Fit_Curve"
                        ctrl.SendToBack()
                    Case "dgvFit_Components"
                        ctrl.BringToFront()
                End Select
            Next

        End If
    End If

End Sub

This code will fire for any of the tab pages and swap the charts and DGVs over on any of the tab pages. The sender.visible check is to stop the code firing when the form is being created.

此代码将为任何标签页触发,并在任何标签页上交换图表和 DGV。sender.visible 检查是在创建表单时停止代码触发。