动态布局按钮.net



大家好

我有一个过程,把按钮使用vb.net的。net windows窗体。它的工作原理很好,但因为我不知道按钮的数量,我将编程,因为他们来自数据库,我想一种方式来布置他们的行10。我已经编程到50,其中ios 5行,但我使用的方法将不工作,如果有超过50。有没有办法做到这一点。我试过使用mod的盒子数量,它不工作。

代码如下:

 Private Sub AddButtons()
        Dim xPos As Integer = 0
        Dim yPos As Integer = 0
        Dim n As Integer = 1
        Dim numberOfBoxes As Integer
        numberOfBoxes = txtNumberOfBoxes.Text
        numberOfBoxes = numberOfBoxes + 1
        ' Declare and Initialize one variable
        Dim btnArray(numberOfBoxes) As System.Windows.Forms.Button
        For i As Integer = 0 To numberOfBoxes
            btnArray(i) = New System.Windows.Forms.Button
        Next i
        While (n < numberOfBoxes)
            With (btnArray(n))
                .Tag = n + 1 ' Tag of button
                .Width = 100 ' Width of button
                .Height = 100 ' Height of button
                If (n = 11) Then ' Location of second line of buttons:
                    xPos = 0
                    yPos = 120
                ElseIf (n = 21) Then
                    xPos = 0
                    yPos = 240
                ElseIf (n = 31) Then
                    xPos = 0
                    yPos = 360
                ElseIf (n = 41) Then
                    xPos = 0
                    yPos = 480
                ElseIf (n = 51) Then
                    xPos = 0
                    yPos = 600
                End If
                'If n Mod 10 = 0 Then
                '    xPos = xPos
                '    yPos = yPos + 50
                'End If
                ' Location of button:
                .Left = xPos
                .Top = yPos
                ' Add buttons to a Panel:
                pnlButtons.Controls.Add(btnArray(n)) ' Let panel hold the Buttons
                xPos = xPos + .Width ' Left of next button
                .Text = (n)
                ' for Event of click Button
                AddHandler .Click, AddressOf Me.ClickButton
                n += 1
            End With
        End While
        btnAddButton.Enabled = False ' not need now to this button now
        Label1.Visible = True
    End Sub

我建议使用FlowLayoutPanel,或者更有可能使用TableLayoutPanel。您配置它以所需的方式增长,然后您在代码中所要做的就是将AddButton添加到Controls集合。它处理所有的布局,所以不需要计算。

Private Sub AddButtons()
    Dim numberOfBoxes As Integer = txtNumberOfBoxes.Text
    For i As Integer = 0 To numberOfBoxes - 1
        'since row is an integer it won't have decimal places
        'also  divides without decimales-
        Dim row As Integer = i  10
        'The modulo operator is your friend. it gives you 
        'the rest of the devision.
        '   http://en.wikipedia.org/wiki/Modulo_operation
        Dim col As Integer = i Mod 10
        Dim newButton = New System.Windows.Forms.Button
        With newButton
            .Tag = i + 1 ' Tag of button
            .Width = 100 ' Width of button
            .Height = 100 ' Height of button
            .Left = col * .Width
            .Top = row * (.Height + 20)
            .Text = i + 1
            AddHandler .Click, AddressOf Me.ClickButton
        End With
        pnlButtons.Controls.Add(newButton)
    Next i
    btnAddButton.Enabled = False ' not need now to this button now
    Label1.Visible = True
End Sub

最新更新