简体   繁体   English

在面板内排列按钮 vb.net

[英]Arranging Buttons inside a Panel vb.net

I am trying to put 25 Buttons inside a Panel in VB.Net 2015 Classic Form Like the Picture but it is not working .我试图将 25 个按钮放在 VB.Net 2015 经典表单中的面板中,就像图片一样,但它不起作用。 could you please help... bellow my code你能帮帮我吗...下面我的代码

    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

在此处输入图片说明

Your code is creating the buttons, and the problem is that they are not arranged properly so what you need to do is arrange them in rows and columns.您的代码正在创建按钮,问题在于它们没有正确排列,因此您需要做的是将它们按行和列排列。 here i help you to do this;在这里我帮你做这件事;

Here this snippet will tell you how to arrange them in 5 columns and n rows:这里这个片段将告诉你如何将它们排列成 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM