在面板vb.net中安排按钮

时间:2016-04-01 06:06:32

标签: vb.net

我试图将25个按钮放在VB.Net 2015经典表格中的面板中,就像图片一样,但它不起作用。你能帮忙吗......瞧不起我的代码

    Dim i As Integer

    For i = 1 To 25
        newButton = New Windows.Forms.Button
        newButton.AutoSize = True
        newButton.Name = "btnButton" & i
        newButton.Text = "Button " & i
        newButton.Top = i * 5
        newButton.Left = i * 25

        newButton.Size = New Size(95, 70)

        AddHandler newButton.Click, AddressOf ButtonClicked

        Panel1.Controls.Add(newButton)

    Next

enter image description here

1 个答案:

答案 0 :(得分:1)

您的代码正在创建按钮,问题是它们没有正确排列,因此您需要做的是按行和列排列它们。在这里,我帮助你做到这一点;

此片段将告诉您如何在5列和n行中排列它们:

Dim x As Integer = 5 ' x co-ordinate of the point
Dim y As Integer = 5 ' y co-ordinate of the point
For i = 1 To 25
    If i Mod 5 = 0 Then ' For starting next row after column
       y += 100 ' 100 is not mandatory change as per size of button
       x = 0
    Else
       x += 100 ' 100 is not mandatory change as per size of button
    End If
    Dim p As Point = New Point(x, y)
    Dim newButton = New Windows.Forms.Button
    newButton.Location = p
    //do the rest of formatting here
    Panel1.Controls.Add(newButton)
Next