【问题标题】:dynamically layout buttons .net动态布局按钮.net
【发布时间】:2014-10-01 16:09:17
【问题描述】:

大家好

我有一个使用 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

【问题讨论】:

    标签: vb.net button layout dynamic


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      相关资源
      最近更新 更多