Creating Control Array

  • Thread starter Tor Inge Rislaa
  • Start date
T

Tor Inge Rislaa

Creating Control Array

How to create an array of buttons, with common procedures based on the index
of the control.

How would this Example from VB 6.0 be in VB.NET?

Private Sub Command1_Click(Index As Integer)

Select Case Index

Case 0

MsgBox "Button1 is clicked"

Case 1

MsgBox "Button2 is clicked"

End Select

End Sub

TIRislaa
 
I

Igor Stanek

Try this:

Private buttons(5) As Button

button(0) = New Button()
button(1) = New Button()
button(2) = New Button()
button(3) = New Button()
button(4) = New Button()


Private Sub buttons_Click(sender As System.Object, ByVal e As System.EventArgs) Handles buttons(0).Click, buttons(1).Click, buttons(2).Click, buttons(3).Click, buttons(4).Click, buttons(5).Click

End Sub


spigi


Hello Tor,
 
C

Cor Ligthert

Tor

There are a lot of methods, another one than Igor

I hope this helps?

Cor
\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim btnArea As Button() = New Button() {Button1, Button2}
For Each btn As Button In btnArea
AddHandler btn.MouseLeave, AddressOf Button_MouseLeave
AddHandler btn.MouseEnter, AddressOf Button_MouseEnter
Next
End Sub
///
\\\And than use it by instance as
Private Sub Button_MouseLeave(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, Button).BackColor = Color.Black
Me.Label1.Text = ""
End Sub
Private Sub Button_MouseEnter(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, Button).BackColor = Color.Red
Me.Label1.Text = DirectCast(sender, Button).Name
End Sub
///
 
H

Herfried K. Wagner [MVP]

Tor Inge Rislaa said:
How to create an array of buttons, with common procedures based on the index
of the control.

In addition to the other comments: You can store the control's ID/index in
its 'Tag' property, if this is needed.
 
G

Guest

I've come across this problem before, and the only way I could get an ID like
in VB6 is to use the tag property, and then use sender.Tag in the event
handler. But I'm watching to see if anybody suggests a better method.
 
C

Cor Ligthert

Bonj,

See in my sample in this thread this part

Dim btnArea As Button() = New Button() {Button1, Button2}

With that you can create a control array exactly as you want and from all
parents whatever that is there parent.

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
See in my sample in this thread this part

Dim btnArea As Button() = New Button() {Button1, Button2}

With that you can create a control array exactly as you want
and from all parents whatever that is there parent.

But you don't associate an ID with the items in the array, except the unique
reference passed to the handler in 'sender'.
 
C

Cor Ligthert

Herfried,
But you don't associate an ID with the items in the array, except the
unique
reference passed to the handler in 'sender'.

I get the reference as well when I add an handler, where would I need it
more?
For the rest I can reference it direct in my opinion. So maybe you can clear
for me where I do not understand you?

I do not see what I cannot get

directcast(myarray(0), control).text etc

Give me an example?

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
I do not see what I cannot get

directcast(myarray(0), control).text etc

Give me an example?


Well, assume that you want to associate an item ID with a button, then using
the 'Tag' property is IMO a good idea:

\\\
.... = DirectCast(sender, Control).Tag
///
 
C

Cor Ligthert

Well, assume that you want to associate an item ID with a button, then
using
the 'Tag' property is IMO a good idea:

\\\
... = DirectCast(sender, Control).Tag
///
You can do everything with it (read beneath this also)
\\\
Dim btnArea As Button() = New Button() {Button1}
btnArea(0).Tag = "Hello Herfried"
MessageBox.Show(btnArea(0).Tag.ToString)
///
I think you are mixing this up with the toolbar, for that I have saved your
sample as snippet .

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
I think you are mixing this up with the toolbar

I think you missed that my post should provide an extension, not a
replacement to your post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top