如何动态地将带有事件处理程序的按钮添加到表单?

时间:2016-02-29 16:08:30

标签: vb.net

是否可以动态执行此操作?怎么样?

代码:

Public Class Form1

Dim c(40) As Integer
Dim IMG As PictureBox
Dim lbl_n(40) As Label
Dim lbl_pr(40) As Label
Dim lbl_ref(40) As Label
Dim lbl_dim(40) As Label
Dim lbl_col(40) As Label
Dim btn_add(40) As Button
Dim btn_rmv(40) As Button
Dim tb_qt(40) As TextBox


AddHandler btn_add(0).Click, AddressOf btn_add0_Click
AddHandler btn_add(1).Click, AddressOf btn_add1_Click
[...]
AddHandler btn_add(40).Click, AddressOf btn_add40_Click

Public Sub btn_add0_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        c(0) = c(0) + 1
        tb_qt(0).Text = c(0)
    End Sub

    Public Sub btn_add1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        c(1) = c(1) + 1
        tb_qt(1).Text = c(1)
    End Sub

    [...]

    Public Sub btn_add40_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        c(40) = c(40) + 1
        tb_qt(40).Text = c(40)
    End Sub

这些是程序运行的图像(它们被编辑):

Form1

Form2

我想动态,因为我可以使用超过40种产品!我需要做40个addhandler,所以要删除40多个! 我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

是的,你可以动态地做到这一点。

在此示例中,我将buttonstextboxes放入Panel

有评论的例子,哪一行代表什么:

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'dynamically adding buttons (so You don't need create every button separately)
        For x = 1 To 40
            Dim btn As New Button  'create new button
            btn.Name = "btn_add" & x.ToString  'set ID for button (example: btn_add1, btn_add2, ... btn_add40)
            btn.Text = "+"  'set button text
            btn.Tag = x.ToString 'set button NO. for finding corrent textbox whos belong to this button (for example: tb_qt1 belong to buttons btn_add1 and btn_rem1)
            btn.Width = 24 'this is for styling
            btn.Top = (x * btn.Height) + 3 'for styling, too. Top poistion of button
            btn.Left = 0 'for styling, too. Left position of button
            AddHandler btn.Click, AddressOf btnAdd_Click 'creating sub called btnAdd_Click (this sub will handle all, in this case 40, buttons)
            Panel1.Controls.Add(btn) 'for this example I put buttons and textboxes into panel (autoscroll set to True)
            btn = New Button 'same thing just for remove button
            btn.Name = "btn_rem" & x.ToString
            btn.Text = "-"
            btn.Tag = x.ToString
            btn.Width = 24
            btn.Top = (x * btn.Height) + 3
            btn.Left = btn.Width + 3
            AddHandler btn.Click, AddressOf btnRem_Click 'creating sub called btnRem_Click (this sub will handle all, in this case 40, buttons)
            Panel1.Controls.Add(btn)
            Dim txt As New TextBox 'same thing for textboxes
            txt.Name = "tb_qt" & x.ToString
            txt.Text = "0"
            txt.Tag = x.ToString
            txt.Top = (x * btn.Height) + 3
            txt.Left = btn.Left + btn.Width + 3
            txt.TextAlign = HorizontalAlignment.Right
            Panel1.Controls.Add(txt)
        Next
    End Sub

    Public Sub btnAdd_Click(sender As Object, e As EventArgs)
        Dim btn As Button = DirectCast(sender, Button)  'detect which button clicked. You can add line: MsgBox(btn.Name) to see what happening
        Dim txt As TextBox = Panel1.Controls.Find("tb_qt" & btn.Tag.ToString, True)(0) 'find textbox which belong to this button. this is why set .Tag into buttons
        txt.Text = CInt(txt.Text) + 1 'just do math and change value, by adding one(1), in textbox (by this way I avoid using Your Dim c(40) As Integer)
    End Sub
    Public Sub btnRem_Click(sender As Object, e As EventArgs)
        Dim btn As Button = DirectCast(sender, Button) 'same thing like for btnAdd_Click, but we doing subtract for one(1) value in textbox
        Dim txt As TextBox = Panel1.Controls.Find("tb_qt" & btn.Tag.ToString, True)(0)
        txt.Text = CInt(txt.Text) - 1
    End Sub

因为我对所有panelcontainer使用buttons textboxes,所以您必须为AutoScroll=True设置panel

我希望你能理解这是如何开始的。