How to create any number of buttons 
Author Message
 How to create any number of buttons

(VB.net)

How can I create a number of buttons dynamically based on a variable?

For example the user types in 506 and the program has to create 506
buttons. I will position them and set properties for each button in a
For...Next loop but I need to know how to declare an ARRAY of BUTTONS
(and labels and whatever).

Is that at all possible in VB.net (doesn't have the index property
like VB6) and if not, is there a way to do it differently?

TIA



Sun, 26 Dec 2004 23:14:43 GMT  
 How to create any number of buttons

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_v...
t/html/vbtchcreatingcontrolarraysinvisualbasicnetvisualcnet.asp

Tom Shelton


Quote:
> (VB.net)

> How can I create a number of buttons dynamically based on a variable?

> For example the user types in 506 and the program has to create 506
> buttons. I will position them and set properties for each button in a
> For...Next loop but I need to know how to declare an ARRAY of BUTTONS
> (and labels and whatever).

> Is that at all possible in VB.net (doesn't have the index property
> like VB6) and if not, is there a way to do it differently?

> TIA



Mon, 27 Dec 2004 00:51:18 GMT  
 How to create any number of buttons
Sure is possible. :)  There are however, a couple of things that you have to
do.  One create the routine(s) that will be the event handler(s) for the
buttons.  Then:

Dim butArray() as System.Windows.Forms.Button

Sub BuildButtons(ByVal NumberOfButtons)
    Dim Index As Integer
    Dim newButton as System.Windows.Forms.Button

    Redim butArray(NumberOfButtons - 1) ' Minus 1 as Arrays are 0 based

    For Index = 0 to NumberOfButtons - 1
        newButton = New System.Windows.Forms.Button
        'position your button code omitted
        butArray(Index) = newButton

        'repeat following for every event you want to handle
        AddHandler butArray(Index), AddressOf ButtonEventHandler
    Next

End Sub

HTH

David


Quote:
> (VB.net)

> How can I create a number of buttons dynamically based on a variable?

> For example the user types in 506 and the program has to create 506
> buttons. I will position them and set properties for each button in a
> For...Next loop but I need to know how to declare an ARRAY of BUTTONS
> (and labels and whatever).

> Is that at all possible in VB.net (doesn't have the index property
> like VB6) and if not, is there a way to do it differently?

> TIA



Mon, 27 Dec 2004 00:58:19 GMT  
 How to create any number of buttons
Hi,

Uhm, out of curiousity -- why would you want to create 506 buttons on a
form?

Assuming you just used that number as an example...

Private buttons() As Button

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

    Dim count As Integer
    Dim i As Integer

    count = InputBox("How many buttons?", "Buttons", "3")
    ReDim buttons(count)

    For i = 0 To count - 1
        buttons(i) = New Button()
        With buttons(i)
            .Text = "Button: " & i
            LLocation = New System.Drawing.Point(100, 50 + (50 * i))
            Me.Controls.Add(buttons(i))
        End With

    Next

End Sub

TTFN - Knet

Quote:
> (VB.net)

> How can I create a number of buttons dynamically based on a variable?

> For example the user types in 506 and the program has to create 506
> buttons. I will position them and set properties for each button in a
> For...Next loop but I need to know how to declare an ARRAY of BUTTONS
> (and labels and whatever).

> Is that at all possible in VB.net (doesn't have the index property
> like VB6) and if not, is there a way to do it differently?

> TIA



Mon, 27 Dec 2004 01:28:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to create a certain number of buttons / labels

2. Creating Successive Numbers (Order Numbers)

3. Creating a Macro to create simple numbers

4. Button remember number of clicks?

5. Paging a database with page number buttons?

6. Assoc. number pad with Command buttons

7. How to reduce number of window buttons in systray

8. When you create a field each field has a id number (field number) (how can you edit this number)

9. Create Unique Auto Number

10. Creating a uniqued record number with outside fields

11. Creating a Serial Number from an Autonumber

12. Create a sequential stock number ???

 

 
Powered by phpBB® Forum Software