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

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




Dynamically creating buttons --> need tips

 
Reply to this topicStart new topic

Dynamically creating buttons --> need tips

devilsangeleyes
16 Dec, 2007 - 10:33 AM
Post #1

New D.I.C Head
*

Joined: 24 Feb, 2006
Posts: 17


My Contributions
Hello,

can someone help me?

I want to create other buttons by clicking just one button, not just put them visible. But I have no idea how to start.
Can you give me some tips please?

Grtz
DEA
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Dynamically Creating Buttons --> Need Tips
16 Dec, 2007 - 11:43 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



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

My Contributions
The process is pretty easy and straight forward.

1) Declare a new variable of the control's type (in our example a Button)
2) Set its properties like its text, size, location, visibility etc like you would any other control
3) Add it to the form by adding it to the forms list of controls (it is actually a collection of controls called "controls"... pretty fitting huh?)

The steps above are demonstrated below in a click event of our button called "btnAddButton"

CODE

Private Sub btnAddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddButton.Click
        ' Declare a new button variable (hence the keyword 'New')
        Dim newbutton As New Button

        ' Give it some text
        newbutton.Text = "New Button"

        ' Lets set its position
        newbutton.Top = 10
        newbutton.Left = 10

        ' Finally, add it to the list of the form's controls.
        Me.Controls.Add(newbutton)
End Sub


You can see the steps I mentioned above in the example. We create a new variable of type "Button", we give it some text and set its location on the form (it will start at 10,10 from the upper left corner) and we then add it to the forms controls collection using the add method. (Me represents the current object... the form).

The result is a button with the text "New Button" in the upper left corner of the form when we click our add button.

Hope that makes sense and gives you an idea of how you can add controls dynamically. Enjoy!

"At DIC we be control adding code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

RudyVB.net
RE: Dynamically Creating Buttons --> Need Tips
12 May, 2008 - 09:14 AM
Post #3

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Hi martyr2!

Thanks for the quick little sample. I would like to tak this one step further.

What if I wanted to create multiple buttons? Add them off set by 5 from the previous button, and name them like box, then 1, and 2 and son on. I know the loop is the reigh of way, just sure how to write it so it moves the new box over, and give it a new name.

CODE
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ' Declare a new button variable (hence the keyword 'New')
        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

        ' Give it some text


        ' Lets set its position





        ' Finally, add it to the list of the form's controls.

    End Sub


This is what I tried, but it's not working.

Thanks!

Rudy
User is offlineProfile CardPM
+Quote Post

lewax00
RE: Dynamically Creating Buttons --> Need Tips
12 May, 2008 - 02:20 PM
Post #4

New D.I.C Head
*

Joined: 13 Jan, 2008
Posts: 24


My Contributions
CODE
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ' Declare a new button variable (hence the keyword 'New')
        
        Dim intLoop As Integer

        For intLoop = 0 To 10
                        Dim newbutton As New Button 'put it in the loop so you create a new button each time
            newbutton.Text = "New Button"
            newbutton.Top = 10 + (5 * intLoop)
            newbutton.Left = 25 + (1 * intLoop)
                        newbutton.Name = "box" & intLoop.ToString
            Me.Controls.Add(newbutton)
        Next

    End Sub


Just adjust the values in the parenthesis as necessary to get the offset you want.

Edit: Just a quick little note:
If you want to add click events, add this into your loop:
CODE
AddHandler newbutton.Click, AddressOf myHandler

where myHandler is the name of the sub.

Just make sure the sub you use has the right parameters, for example:
CODE
Private Sub myHandler(ByVal sender as Object, ByVal e as System.EventArgs)
        MessageBox.Show("Handler added successfully!")
End Sub


would work for the Click event of a button. If you have the wrong parameters just check the error message to see what you need.

This post has been edited by lewax00: 12 May, 2008 - 02:26 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Dynamically Creating Buttons --> Need Tips
12 May, 2008 - 02:23 PM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



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

My Contributions
This has been addressed in your other thread. Please stick to one thread per question. We will get to it when we are able. Thanks. smile.gif
User is offlineProfile CardPM
+Quote Post

RudyVB.net
RE: Dynamically Creating Buttons --> Need Tips
13 May, 2008 - 05:11 AM
Post #6

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Thanks Lewax!!
User is offlineProfile CardPM
+Quote Post

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

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