• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 533
  • Last Modified:

vb.net Flow layout

I have the following code below that checks my database and then adds buttons to the FlowLayoutPanel. I want to limit the amount of buttons that fit on the flowlayoutpanel to 30 and then if there are more than 30 in the database add the others to flowpanellayout2.

I also want to add 3 buttons to the flowlayoutpanel at the end of my generated buttons how can I do this?
Dim con As New SqlConnection(My.Settings.Constr)
        con.Open()

        Dim cmd As New SqlCommand("SELECT * FROM tblCostumes WHERE master = '0'", con)
        Dim reader As SqlDataReader = cmd.ExecuteReader()

        While reader.Read()
            Dim btn As New Button
            btn.Name = "Button" & reader("id")
            btn.Width = 116
            btn.Height = 76
            btn.Tag = reader("name")
            btn.Text = reader("name")
            AddHandler btn.Click, AddressOf GetProductDetails
            FlowLayoutPanel1.Controls.Add(btn)
        End While

Open in new window

0
richard_gar
Asked:
richard_gar
  • 3
  • 2
1 Solution
 
utter77Commented:
Do like this:

Dim con As New SqlConnection(My.Settings.Constr)
        con.Open()

        Dim cmd As New SqlCommand("SELECT * FROM tblCostumes WHERE master = '0'", con)
        Dim reader As SqlDataReader = cmd.ExecuteReader()
        Dim counter as Integer = 0

        While reader.Read()
            counter += 1
            Dim btn As New Button
            btn.Name = "Button" & reader("id")
            btn.Width = 116
            btn.Height = 76
            btn.Tag = reader("name")
            btn.Text = reader("name")
            AddHandler btn.Click, AddressOf GetProductDetails
            If counter < 30 Then
                  FlowLayoutPanel1.Controls.Add(btn)
            Else
                  FlowLayoutPanel2.Controls.Add(btn)
            End If
        End While
 
       Dim btn1 as New Button
       btn1. ....
       btn1. ....
       Dim btn2 as New Button
       btn2. ....
       btn2. ....
       Dim btn3 as New Button
       btn3. ....
       btn3. ....

       FlowLayoutPanel1.Controls.Add(btn1)
       FlowLayoutPanel1.Controls.Add(btn2)
       FlowLayoutPanel1.Controls.Add(btn3)



/MAtt
0
 
richard_garAuthor Commented:
My buttons are created dynamically so this solution would not work as I do not know how many fields there would be in my database as the user would add these.

Also my code would go on forever.
0
 
utter77Commented:
the buttons i add after the while-loop are the three buttons you wanted to add after the dynamic buttons.

i have added a counter and an if-statement in your while-loop that adds buttons to a different flowlayout if there is more than 30 buttons in the database
0
 
richard_garAuthor Commented:
but how can I position the buttons after to be at a certain positioning?
0
 
utter77Commented:
add the buttons to a panel and positioning them there. then add the panel to the flowlayout.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now