将自动生成的按钮数组链接到VB.NET 2010中的公共事件处理程序

时间:2014-01-29 03:01:22

标签: vb.net

我正在使用VB.net做一个项目,在那里我生成一个大约300个按钮的数组(根据可用的X和Y方向限制确定的按钮数量。),点击一个名为START按钮的按钮。我定义了一个子程序,使用ADD Button生成按钮并成功完成。

现在我想自动将所有这些按钮链接到相同的点击处理程序。我无法选择按钮并将它们链接到相同的单击事件,因为所有这些按钮仅在代码执行时生成。

我在下面附上我的代码,有人建议我解决这个问题会很棒。

Public Class Form1

    'Variable that holds the value of button size 
    Dim buttonwidth As Integer = 40
    Dim buttonheight As Integer = 40

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)     Handles MyBase.Load



    Private Sub AddButton(ByVal xp As Integer, ByVal yp As Integer)

        'This routine generate button automatically
        'This routine need an inputs of its location

        'defining b as a new BUTTON control "b = New Button"
        Dim b As New Button

        'defining location as a POINT variable, which have X & Y. Taking them from the argument
        Dim location As New Point(xp, yp)

        'Assigning that location for the button
        b.Location = location

        'Assiging the size of the button
        b.Size = New Size(40, 40)

        'Giving width value to button width variable
        'buttonwidth = 40 'b.Size.Width
        'Giving height value to buttonheight variable
        'buttonheight = 40 ' b.Size.Height

        'Assigning Random label for the button
        Randomize()                                         'Initializing the random generator
        Dim label As Integer = CInt(Int((9 * Rnd() + 0)))   'Create a random number for Label
        b.Text = label                                      'Assign that number to label

        'Assigning font for the button
        b.Font = New Font(b.Font.FontFamily, 15)
        'b.Handle = New (b.Handle, Button1_Click )

        'Finally ADD the button to the form
        Me.Controls.Add(b)

    End Sub

    Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
        'This part of algorithm handles the generation and placement of buttons
        'Here x & y are two indigers responsible for the x , y cordinates of buttons

        'defining y value and it starts from cordinate 60. This can be changes to increase the spacing
        'between top border of the form and buttons

        'Here set the limits for the buttons
        Dim x_min_limit, y_min_limit, x_max_limit, y_max_limit As Integer
        x_min_limit = 60
        x_max_limit = 1300
        y_min_limit = 150
        y_max_limit = 600

        Dim y As Integer = y_min_limit         'assigning minimum limit value to y

        'This loop is responsible for generating buttons in between the limits of x&y 
        'Here the for loop executes untill the x&y reaches up to its maximum limits
        ' here x is the integer looping between the limits with a step of button width (when ever button width changes , the step also will change)
        '1.4 value deducted from step because we dont wanted to overlap 2 buttons in x axis
        For x As Integer = x_min_limit To x_max_limit Step buttonwidth - 1.4

            If x > x_max_limit - 50 Then            'This if loop makes sure that buttons are populated on y direction
                x = x_min_limit                     'If x direction population exceeds the xiven x limit, it increases the y value and restores x value to initial value        
                y = y + buttonheight - 1.4          'Y value increased with a step of button height and correction value is also given
                If y > y_max_limit Then             'whenever the Y value reaches its maximum limit, 
                    Exit For                        'terminate the whole for loop
                End If
            End If

            'Whenever the x and y coordinates get finalized ADD that button to form
            'Call the function and pass the x&y value in to it
            Call AddButton(x, y)
        Next
        Randomize()                                         'Initializing the random generator
        Dim label As Integer = CInt(Int((9 * Rnd() + 0)))   'Create a random number for Label
        Label1.Text = label


    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttons_click



    End Sub


End Class

2 个答案:

答案 0 :(得分:0)

我相信您正在寻找AddHandler方法:http://msdn.microsoft.com/en-us/library/ms598898(v=vs.110).aspx

答案 1 :(得分:0)

是的,你必须创建一个没有Buttons_Click关键字的Handles Sub,就像关注

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

End Sub

并且,您必须在创建按钮对象后添加事件处理程序

'defining b as a new BUTTON control "b = New Button"
Dim b As New Button
AddHandler b.Click, AddressOf Buttons_Click

就是这样。

有关更多参考资料,请参阅评论和其他答案 请去 http://msdn.microsoft.com/en-us/library/7taxzxka.aspx
http://msdn.microsoft.com/en-us/library/ms598898%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1