0

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. 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...?

2
  • which positions do you get? Commented Oct 27, 2016 at 8:38
  • can anyone teach me how to do this with backgroundworker Commented Oct 28, 2016 at 10:55

2 Answers 2

2

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. 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:

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
Sign up to request clarification or add additional context in comments.

2 Comments

@redbull I would suggest that that should be a different question in its own right, rather than an addendum to this question.
yes. i already made a seperate question for this one. you can view it here: stackoverflow.com/questions/40304088/…
1

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. I've gone for 5 as per the question but you can change it and it should adapt.

2 Comments

yes this is correct but is their another way of doing this without using the if statement so that even when i change the number of buttons to loop it, it can still show 5 buttons per line.
@redbull Updated answer to what I think will suit you better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.