Click here to Skip to main content
15,911,707 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!

I'm a newbie who is from Vietnam (so I'm not good at English)!

I have a problem in VB.NET.
As far as we have known, VB's supported control array, but VB.NET isn't supported.

I've designed a lot of buttons (100 button) on a form.
Then I've set names for them are Button1, Button2,...Button100.

I'd like to create an array to contain this Buttons (Button1,..Button100 that I have designed on the form)-(NOT CREATE AN ARRAY TO CONTAIN NEW BUTTONS).

So, How can I do it? please show me a way!

help me please! :confused:
Posted
Updated 16-Apr-18 8:11am
v6
Comments
Dalek Dave 22-Feb-11 4:19am    
Edited for Readability (Your English is excellent, far better than my Vietnamese!)
[no name] 24-Feb-11 20:49pm    
u can speak Vietnamese, rn't u? That's great! ^_^ I would like to make acquaintance with u, Dalek! my yahoo id: gregorimendel! Please add me on ur friend list if u read this msg! ^_^

CMember 7655557 wrote:
The Text of each button is a number which from 0 to 99. I'd like to use the For...Next loop to add number which from 0 to 99 on the text of each button of button array.


After your clarification, I would suggest to use following code. You don't need array to do that.

VB
'Dim buttons() As Button


For i = 1 To 100
  Dim btns() As Button = Controls.Find("Button" & i, True)
  Dim btn As Button
  If btns IsNot Nothing Then
    btn = btns(0)
    'If buttons Is Nothing Then
    '  ReDim buttons(0)
    'Else
    '  ReDim Preserve buttons(buttons.Length)
    'End If
    'buttons(UBound(buttons)) = btn
    btn.Text = i - 1 'here you can change whatever you want
  End If
Next
 
Share this answer
 
Comments
[no name] 21-Feb-11 23:26pm    
Nice idea,
But Creating one button 100 time doesn't gives event handler

how would you handle the events of the 100 buttons
Prerak Patel 22-Feb-11 3:47am    
use addhandler to add event handlers
Tries Followinn links

Dynamic creatio nof array

This lasy programming dear tries google search.
 
Share this answer
 
Step 1, Create a Form called Form1.
Step 2, Add a TableLayoutPanel called TableLayoutPanel1.
Step 3, Make it 10 X 10 with 10% each.
Step 4, Set its anchor to T, B, L, and R. (It could be in any size in design time)
Step 5, Copy the code to Form1.vb.
Step 6, Run it, and you get it.
Step 7, If it works for you, please mark this one as 5.00. I wish to get 10 * 5.00 in 3 days.

Public Class Form1
    'Use 0 based array
    Private NRow As Integer = 9
    Private NCol As Integer = 9
    Private BtnArray((NRow + 1) * (NCol + 1) - 1) As Button
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TableLayoutPanel1.Size = Me.ClientSize
        For i As Integer = 0 To BtnArray.Length - 1
            BtnArray(i) = New Button()
            BtnArray(i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
            BtnArray(i).Text = CStr(i)
            TableLayoutPanel1.Controls.Add(BtnArray(i), i Mod (NCol + 1), i \ (NCol + 1))
            AddHandler BtnArray(i).Click, AddressOf ClickHandler
        Next
    End Sub
    Public Sub ClickHandler(ByVal sender As Object, ByVal e As _
   System.EventArgs)
        MsgBox("I am button #" & CType(sender, Button).Text)
    End Sub
End Class
 
Share this answer
 
v4
VB
Dim buttons() As Button


buttons = Nothing
For Each b As Button In Me.Controls
  If buttons Is Nothing Then
    ReDim buttons(0)
  Else
    ReDim Preserve buttons(buttons.Length)
  End If
  buttons(UBound(buttons)) = b
Next


buttons will be your array.
By the way, why you need the array?
 
Share this answer
 
v2
Comments
[no name] 21-Feb-11 7:34am    
Thank you so much! I'm glad when u write to me! I have an application in VB.NET, It's about the numbers, The Text of each button is a number which from 0 to 99. I'd like to use the For...Next loop to add number which from 0 to 99 on the text of each button of button array. But, as u know, I don't know how to create an array to contain my buttons without creating new buttons. Do you actually understand me?
Dalek Dave 22-Feb-11 4:20am    
Spot On!
Try:
Dim buttons As Button() = New Button() {Button1, Button2, Button3}


BTW: Life is a lot easier to work with when you don't call things "Button1" and "Button2" but "butChangeName", "butChangeTown" and so forth: What does "Button76" actually do? Without looking in your code?
 
Share this answer
 
Comments
[no name] 21-Feb-11 7:35am    
Thanks a lot!
OriginalGriff 21-Feb-11 7:38am    
No problem!
Dalek Dave 22-Feb-11 4:21am    
Sage advice about names!
hey,
to creat controls dynamically we must deside were we create perticular control.

for your question.
declare following statement in new or in formLoad menthod
Dim temp As Label()


which assign memory for array of controls.

Dim h as integer
Dim i as integer
for i=0 to 10                                     'for 10 labels. this value can varies accordingly
  temp(i) = New Label
  temp(i).Top = 10+h
  temp(i).Left = 10
  temp(i).Width =60
  temp(i).Height =20
  temp(i).TextAlign = HorizontalAlignment.Right 
  temp(i).Text = "this is label"


  Me.Controls.Add(temp(i))
   h= h+ 65
 
Share this answer
 
v2
Comments
[no name] 21-Feb-11 7:23am    
Thanks for help me! but I'm sorry, I don't actually know what you mean! can you show me the detail of them! please!
[no name] 21-Feb-11 22:34pm    
Hey dear,
you want details on above code and you want to create button instead of label plese goes with following code.


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim temp As Button()
Dim h as integer
Dim i as integer
for i=0 to 100 'for 10 labels. this value can varies accordingly
temp(i) = New Button
temp(i).Top = 10+h
temp(i).Left = 10
temp(i).Width =60
temp(i).Height =20
temp(i).TextAlign = HorizontalAlignment.Right
temp(i).Text = "this is button"+Convert.ToString(i)


Me.Controls.Add(temp(i))
h= h+ 65


End Sub


this will work fine
Dalek Dave 22-Feb-11 4:20am    
Good Call.
[no name] 22-Feb-11 19:53pm    
thanks a lot! buy guy, I said I'd like to create an array to contain the buttons that I've designed on the form. I wouldn't like to create one to contain new bttons. Do u actually understand me?
Link1[^] and Link2[^] might help you.
 
Share this answer
 
Comments
[no name] 22-Feb-11 19:15pm    
thanks a lot! It's so useful!
[no name] 22-Feb-11 22:39pm    
Its my pleasure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900