Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,086 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,773 people online right now. Registration is fast and FREE... Join Now!




create buttons on the fly

 
Reply to this topicStart new topic

create buttons on the fly

RudyVB.net
12 May, 2008 - 01:42 PM
Post #1

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

hello all.

I know how to create a button on the fly. My question is, how can I create multiple buttons based on a value. Say I need 10 buttons to be created, but I need to put them next to each other, 4 per row, and then a new row.

I started with something like this, but I'm not sure how to move the buttons.


CODE
Dim newbutton As New Button
        Dim intLoop As Integer

        For intLoop = 0 To 10
            newbutton.Text = "New Button"
            newbutton.Top = 10 + 5
            newbutton.Left = 25 + 1
            Me.Controls.Add(newbutton)
        Next

Thanks

Rudy
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Create Buttons On The Fly
12 May, 2008 - 02:15 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Basic math my friend. You are on the right track with the left and top, but those numbers are going to have to change based on the button. So those settings are going to be calculated based on an equation. As for the rows, that will be dependent on the counter.

vb

Dim intLoop As Integer

' Off sets
Dim y As Integer = 15
Dim x As Integer = 0

' Loop through buttons
For intLoop = 0 To 10

' Create button and set its width
Dim newbutton As New Button
newbutton.Width = 80
newbutton.Height = 20

' If we have hit 4 buttons, adjust the y value by adding 10 onto the controls height
' Reset x back to 0
If intLoop Mod 4 = 0 Then
' This gives us a 10 pixel buffer below each row of buttons
y += newbutton.Height + 10
x = 0
End If

' Set the text and set its top and left based on its dimensions and count
newbutton.Text = "New Button"
newbutton.Top = y

' This gives us a 5 pixel right buffer between buttons
newbutton.Left = 26 + (x * (newbutton.Width + 5))
x += 1
Me.Controls.Add(newbutton)
Next


Notice that every four buttons we reset the x value back to zero and increment the y (height) based on the buttons height plus a 10 pixel buffer. For each button we also adjust its left based on the buttons width plus a 5 pixel buffer.

The result is the grid style you are looking for. Feel free to adjust the 10 and 5 or the button's height/width to get a grid that suits your needs.

Enjoy!

"At DIC we be button grid printing code ninjas... just remember that no grid can stop us!" decap.gif
User is offlineProfile CardPM
+Quote Post

RudyVB.net
RE: Create Buttons On The Fly
13 May, 2008 - 05:10 AM
Post #3

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Pefect!! Thank you Matyr2.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:50PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month