如何在vb.net中动态生成每个按钮的属性后访问它们?

时间:2012-06-10 17:02:51

标签: vb.net

我动态生成了一些按钮。现在我需要以这样的方式操纵它们,即单击一个按钮应该在另一个按钮上执行某些操作。怎么做?我希望代码尽可能简单,因为我正在辅导业余爱好者。

    Private Sub NewButton(ByVal ButtonNumber As Integer)
    ' set the button properties 

    btn.Name = "Button" & ButtonNumber
    btn.Enabled = True
    btn.Size = New Size(25, 25)
    btn.Visible = True
    'use tag to store number of button
    btn.Tag = ButtonNumber
    ' Add button click handler
    AddHandler btn.Click, AddressOf btnclick
    ' Add to this forms controls collection
    Me.Controls.Add(btn)
    arr1(count) = btn.Name
    count = count + 1
End Sub          


Private Sub NewButton(ByVal ButtonNumber As Integer)
    ' set the button properties 

    btn.Name = "Button" & ButtonNumber
    btn.Enabled = True
    btn.Size = New Size(25, 25)
    btn.Visible = True
    'use tag to store number of button
    btn.Tag = ButtonNumber
    ' Add button click handler
    AddHandler btn.Click, AddressOf btnclick
    ' Add to this forms controls collection
    Me.Controls.Add(btn)
    arr1(count) = btn.Name
    count = count + 1
End Sub

1 个答案:

答案 0 :(得分:0)

将事件与动态创建按钮相关联:

AddHandler btn.Click, AddressOf clickevent 'clickeven sub to handle the event

sub clickevent()
    For Each cn As Control In Me.Controls
      Select Case cn.GetType.ToString.Substring(cn.GetType. _
                                      ToString.LastIndexOf(".") +1).ToLower
            Case "button"
                'code goes here
        End Select
    Next
end sub

希望如果最适合你,问候。