Vb.net 各种面板上的按钮

Vb.net 各种面板上的按钮,vb.net,Vb.net,我有以下头痛,我不能传递按钮到其他面板,我的想法是,与安排,我已经有了按钮。将这些创建的按钮传递给我将要执行的其他面板,请有人帮助我 Dim botones(1, 3) As Button Dim B As Button Public Sub crearBotones() For filas As Integer = 0 To 1 For columnas As Integer = 0 To 3 B = New Button

我有以下头痛,我不能传递按钮到其他面板,我的想法是,与安排,我已经有了按钮。将这些创建的按钮传递给我将要执行的其他面板,请有人帮助我

Dim botones(1, 3) As Button

Dim B As Button
Public Sub crearBotones()
    For filas As Integer = 0 To 1
        For columnas As Integer = 0 To 3
            B = New Button
            botones(filas, columnas) = B
            With B
                .Name = "" & filas.ToString & filas.ToString
                .Text = "" & filas.ToString & columnas.ToString
                .Left = 100
                .Location = New Point(40 + columnas * 70, 5 + filas * 70)
                .Height = 25
                .Width = 50
                Me.Panel1.Controls.Add(botones(filas, columnas))

            End With

        Next

    Next
End Sub
它显示一个面板上的按钮,我正在寻找它显示两个面板上的按钮。


我的意思是,阵列已经为我制作了按钮,但我想把 不同面板上的按钮,它只是让我在一个面板上使用它。 在for中的面板中

稍微重构代码,以便
crearBotones()
方法接收一个面板作为参数,并且仅在尚未创建按钮时创建按钮:

Public Class Form1

    Private botones(1, 3) As Button

    Public Sub crearBotones(ByVal pnl As Panel)
        For filas As Integer = 0 To 1
            For columnas As Integer = 0 To 3
                If IsNothing(botones(filas, columnas)) Then
                    Dim B As New Button
                    With B
                        .Name = "" & filas.ToString & filas.ToString
                        .Text = "" & filas.ToString & columnas.ToString
                        .Left = 100
                        .Location = New Point(40 + columnas * 70, 5 + filas * 70)
                        .Height = 25
                        .Width = 50
                    End With
                    botones(filas, columnas) = B
                End If
                pnl.Controls.Add(botones(filas, columnas))
            Next
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        crearBotones(Panel1)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        crearBotones(Panel2)
    End Sub

End Class

“将它们传递给其他面板”是什么意思?一个控件只能有一个父控件,因此如果将它们添加到其他面板,它们将自动从当前面板中删除。如果所有面板都是同一表单,则只需确保您的声明
Dim botone(1,3)As Button
处于表单级别,而不是在方法内部。然后它们应该可以从表单代码的任何地方访问。我的意思是,数组已经为我制作了按钮,但我想把按钮放在不同的面板上,它只是让我在一个面板上使用它。在for中的面板中,您需要在
结束于
添加
botone(filas,columnas)=B之后,使用新按钮分配数组