-4

I just read a certain article about the control arrays of VB6 not present in VB.NET and rather, became a "collection" or something.... (http://visualbasic.about.com/od/usingvbnet/l/bldykctrlarraya.htm)

Now, I'm planning to learn VB.NET and accept it as whole new language.

So, as a "few steps" for me to migrate, here is my code from VB6:

Private Sub Command1_Click()
    For i = 0 to 9
        Command2(i).Caption = i
    Next i
End Sub

Private Sub Command2_Click(Index as Integer)
    Label1.Caption = Label1.Caption + Index
End Sub

I wonder if you get what my program is? Let's just say it's a certain number pad program. I'll explain what this program does, at least for now...

As you can see, I have 12... controls? (I'm sorry, I'm still a little bit new in terms of programming)... Yeh, 12 of them... 11 buttons and 1 Label. That 1 button, Command1, will give the captions of my other 10 buttons Command2(Index). And when Command2(Index) is pressed, Label1's current caption will be concatenated by Command2(Index)'s Index... (It's like a calculator, let's skip this now)

So, will you teach me a version/translation of this in VB.NET? :) Thanks!

MarkJ
  • 30,070
  • 5
  • 68
  • 111
  • here's a tutorial (which is actually off topic for SO), but could help you get the grips of VB.Net http://www.homeandlearn.co.uk/NET/nets1p17.html – jbutler483 Sep 10 '14 at 13:00
  • this site is not for tution –  Sep 10 '14 at 13:04
  • @jbutler483, it's not my point... The link you've given me, in its first page, I'll have to create each button (Name: btn Plus a Number (btnOne, btnTwo, btnThree, etc)) And there's my point... Unlike in VB6 (where I can use btn(0), btn(1), etc), is there a secret in VB.NET that can make things like that? – Pat-kun Teruel Sep 10 '14 at 13:23
  • That's how VB.NET works! (BTW if you ctrl C ctrl V, the buttons will appear as 'button1', 'button2', etc. - you would then have to change the text of each one to a number anyway! – jbutler483 Sep 10 '14 at 14:38
  • possible duplicate of [Control Array in vb.net](http://stackoverflow.com/questions/5497403/control-array-in-vb-net). Also [this question](http://stackoverflow.com/questions/5299435/how-to-create-control-arrays-in-vb-net) and [this question](http://stackoverflow.com/questions/39541/whats-the-simplest-net-equivalent-of-a-vb6-control-array) and [this question](http://stackoverflow.com/questions/14789112/control-array-vb-net) – C-Pound Guru Sep 10 '14 at 16:38

1 Answers1

1

Just Add 12 regular buttons onto your form Button1 - Button12

Then create a list to hold a reference to these:

Private _buttonList As New List(Of Button)

Add your buttons to the list on Form_Load:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    _buttonList.Add(Button1)
    _buttonList.Add(Button2)
    _buttonList.Add(Button3)
    'etc.
End Sub

Then you can use the list to access your buttons by (zero based) index:

    _buttonList(4).Text = "foo"
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143