简体   繁体   English

vb.net中的动态按钮使用for循环

[英]Dynamic buttons in vb.net using for loop

I want to deal with the arrangement of the buttons. 我想处理按钮的布置。 for example i have 10 buttons, i want that after 5 buttons the next 5 buttons will go to the nextline. 例如我有10个按钮,我希望在5个按钮之后,下5个按钮将转到下一行。 Here is the code that i have used: 这是我使用的代码:

For i = 1 To 10

        Dim btn As New Button

        btn.Width = 40
        btn.Height = 30
        btn.TextAlign = ContentAlignment.MiddleCenter
        If i.ToString.Length = 1 Then
            btn.Text = "B" & "0" & i
        Else
            btn.Text = "B" & i
        End If
        btn.Visible = True
        btn.Tag = "Button" & i
        Panel1.Controls.Add(btn)
        If i <= 5 Then
            btn.Location = New Point(10 * 1 + ((i - 1) * btn.Width), 10)
        Else
            btn.Location = New Point(10 * 1 + ((i - 1) * btn.Width), 10 * 1 + ((i - 1) * btn.Height))
        End If

i get the wrong positioning of the buttons. 我弄错了按钮的位置。 kindly help me on this. 请帮助我。 i always get this kind of position. 我总是得到这样的职位。 ex: 例如:

* * * * *
         *
          *
           *
            *
             *

What i want is this: 我想要的是:

* * * * *
* * * * *

additional: How can i do it with backgroundworker...? 另外:我该如何使用backgroundworker ...?

Assuming you are using winforms, instead of trying to position your buttons by hand, I would suggest you use a FlowLayoutPanel control instead of a straight up panel. 假设您使用的是winforms,而不是尝试手动定位按钮,我建议您使用FlowLayoutPanel控件而不是垂直面板。 Then you can just add them and let the panel manage their positions. 然后,您可以添加它们并让面板管理它们的位置。

For i = 1 to 10


    Dim btn As New Button

    btn.Width = 40
    btn.Height = 30
    btn.TextAlign = ContentAlignment.MiddleCenter
    If i.ToString.Length = 1 Then
        btn.Text = "B" & "0" & i
    Else
        btn.Text = "B" & i
    End If
    btn.Visible = True
    btn.Tag = "Button" & i

    FlowLayoutPanel1.Controls.Add(btn)
Next

If you must have 5 per line (assuming your panel is wide enough), you can use SetFlowBreak : 如果每行必须有5个(假设面板足够宽),则可以使用SetFlowBreak

For i = 1 to 10

    '.....

    FlowLayoutPanel1.Controls.Add(btn)

    'Use this line if you must have only 5 buttons per line.
    if i Mod 5 = 0 Then FlowLayoutPanel1.SetFlowBreak(btn, true)
Next

Try this: 尝试这个:

If i <= 5 Then
    btn.Location = New Point(10 * 1 + ((i - 1) * btn.Width), 10)
Else
    btn.Location = New Point(10 * 1 + ((i - 6) * btn.Width), 10 + btn.Height)
End If

EDIT: 编辑:

If you wanted to change the loop so that you wanted to multiple lines of buttons then look at this: 如果要更改循环,以便要多行按钮,请查看以下内容:

Dim noOfButtonsPerLine As Integer = 5
Dim buttonIndex As Integer = 0
Dim y As Integer = 10

For i = 1 To 15

    Dim btn As New Button With {.Height = 40, .Width = 30}

    If buttonIndex = noOfButtonsPerLine Then
        buttonIndex = 1
        y += btn.Height
    Else
        buttonIndex += 1
    End If

    btn.TextAlign = ContentAlignment.MiddleCenter
    If i.ToString.Length = 1 Then
        btn.Text = "B" & "0" & i
    Else
        btn.Text = "B" & i
    End If
    btn.Visible = True
    btn.Tag = "Button" & i
    Panel1.Controls.Add(btn)

    btn.Location = New Point(10 * 1 + ((buttonIndex - 1) * btn.Width), y)

Next

Change the variable noofButtonsPerLine to what suits you. 将变量noofButtonsPerLine更改为适合您的变量。 I've gone for 5 as per the question but you can change it and it should adapt. 我已经按照问题回答了5条 ,但是您可以更改它,它应该可以适应。

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

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