【发布时间】: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