VBE 2005 - 'Index' Property For A Control

G

Guest

Guyz,

Whatever happened to the 'Index' property for a control, that used to be
present in
VB 3.0 / 4.0 / 5.0 / 6.0 and which now seems to be missing from VBE 2005?

I need to be able to make 4 command buttons behave in the same way, the
'Index'
property indicating which command button I clicked on.

The 'Index' property is an useful time saver because it saves me making
copies of
the same code to the other command buttons and I needed all command buttons
to behave in exactly the same way.

How do I accomplish this in VBE 2005? Thanks in advance.


SB
 
R

RobinS

I'm sorry to tell you this, but control arrays have left the building.
Disappointing, isn't it?

If you want a bunch of buttons to perform the same event, just add your own
event handlers for them in your Form_Load event:

AddHandler myButton1.Click, DoThisForAllOfThem
AddHandler myButton2.Click, DoThisForAllOfThem
AddHandler myButton3.Click, DoThisForAllOfThem
AddHandler myButton4.Click, DoThisForAllOfThem
AddHandler myButton5.Click, DoThisForAllOfThem

If you want to know which button invoked the click, you can probably do it
some fancy way (Reflection comes to mind), but I just put some identifier
in the [Tag] property for each button and check that like this:

Private Sub DoThisForAllOfThem(ByVal sender as Object, ByVal e as
EventArgs)
Dim theButton As Button = DirectCast(sender, Button)
Select Case theButton.Tag
Case "1"
GoToSchool
Case "2"
GoToWork
Case "3"
GoShopping
Case "4"
GoToDinner
Case "5"
GoDrinking
End Select
End Sub

Be sure your buttons have a tag in them, if it is blank, this will throw a
NullException.

Robin S.
 
C

Chris Dunaway

I'm sorry to tell you this, but control arrays have left the building.
Disappointing, isn't it?

If you want a bunch of buttons to perform the same event, just add your own
event handlers for them in your Form_Load event:

AddHandler myButton1.Click, DoThisForAllOfThem
AddHandler myButton2.Click, DoThisForAllOfThem
AddHandler myButton3.Click, DoThisForAllOfThem
AddHandler myButton4.Click, DoThisForAllOfThem
AddHandler myButton5.Click, DoThisForAllOfThem

If you want to know which button invoked the click, you can probably do it
some fancy way (Reflection comes to mind), but I just put some identifier
in the [Tag] property for each button and check that like this:

Private Sub DoThisForAllOfThem(ByVal sender as Object, ByVal e as
EventArgs)
Dim theButton As Button = DirectCast(sender, Button)
Select Case theButton.Tag
Case "1"
GoToSchool
Case "2"
GoToWork
Case "3"
GoShopping
Case "4"
GoToDinner
Case "5"
GoDrinking
End Select
End Sub

Be sure your buttons have a tag in them, if it is blank, this will throw a
NullException.

While the Tag property is useful, you can also just check the button
itself:

Private Sub DoThisForAllOfThem(ByVal sender as Object, ByVal e as
EventArgs)
Select Case True
Case sender is myButton1
GoToSchool
Case sender is myButton2
GoToWork
Case sender is myButton3
GoShopping
Case sender is myButton4
GoToDinner
Case sender is myButton5
GoDrinking
End Select
End Sub

Although some don't like using the Select Case True trick.

Another option would be to use the Tag property and store a delegate
to the desired function there and then just call the function.

Public Delegate Sub DoSomethingDelegate() 'This delegate sig
matches the desired sub

Public Sub New()
InitializeComponent

Button1.Tag = New DoSomethingDelegate(AddressOf GoToSchool)
Button2.Tag = New DoSomethingDelegate(AddressOf GoToWork)
Button3.Tag = New DoSomethingDelegate(AddressOf GoOutDrinking)
'etc.
End Sub

Private Sub DoThisForAllOfThem(ByVal sender as Object, ByVal e as
EventArgs)
Dim theButton As Button = DirectCast(sender, Button)
DirectCast(theButton.Tag, DoSomethingDelegate).Invoke()
End Sub

Public Sub GoToSchool()
End Sub

Public Sub GoShopping()
End Sub

'etc.

This method is more complicated and perhaps a little less readable so
I don't know if it is very useful, but you know the old saying:
"There's more than one way to skin a cat" (By the way, who comes up
with these sayings anyway? This one is kind of sick!!)

Chris
 
R

RobinS

Chris Dunaway said:
I'm sorry to tell you this, but control arrays have left the building.
Disappointing, isn't it?

If you want a bunch of buttons to perform the same event, just add your
own
event handlers for them in your Form_Load event:

AddHandler myButton1.Click, DoThisForAllOfThem
AddHandler myButton2.Click, DoThisForAllOfThem
AddHandler myButton3.Click, DoThisForAllOfThem
AddHandler myButton4.Click, DoThisForAllOfThem
AddHandler myButton5.Click, DoThisForAllOfThem

If you want to know which button invoked the click, you can probably do
it
some fancy way (Reflection comes to mind), but I just put some
identifier
in the [Tag] property for each button and check that like this:

Private Sub DoThisForAllOfThem(ByVal sender as Object, ByVal e as
EventArgs)
Dim theButton As Button = DirectCast(sender, Button)
Select Case theButton.Tag
Case "1"
GoToSchool
Case "2"
GoToWork
Case "3"
GoShopping
Case "4"
GoToDinner
Case "5"
GoDrinking
End Select
End Sub

Be sure your buttons have a tag in them, if it is blank, this will throw
a
NullException.

While the Tag property is useful, you can also just check the button
itself:

Private Sub DoThisForAllOfThem(ByVal sender as Object, ByVal e as
EventArgs)
Select Case True
Case sender is myButton1
GoToSchool
Case sender is myButton2
GoToWork
Case sender is myButton3
GoShopping
Case sender is myButton4
GoToDinner
Case sender is myButton5
GoDrinking
End Select
End Sub

Although some don't like using the Select Case True trick.

Another option would be to use the Tag property and store a delegate
to the desired function there and then just call the function.

Public Delegate Sub DoSomethingDelegate() 'This delegate sig
matches the desired sub

Public Sub New()
InitializeComponent

Button1.Tag = New DoSomethingDelegate(AddressOf GoToSchool)
Button2.Tag = New DoSomethingDelegate(AddressOf GoToWork)
Button3.Tag = New DoSomethingDelegate(AddressOf GoOutDrinking)
'etc.
End Sub

Private Sub DoThisForAllOfThem(ByVal sender as Object, ByVal e as
EventArgs)
Dim theButton As Button = DirectCast(sender, Button)
DirectCast(theButton.Tag, DoSomethingDelegate).Invoke()
End Sub

Public Sub GoToSchool()
End Sub

Public Sub GoShopping()
End Sub

'etc.

This method is more complicated and perhaps a little less readable so
I don't know if it is very useful, but you know the old saying:
"There's more than one way to skin a cat" (By the way, who comes up
with these sayings anyway? This one is kind of sick!!)

Chris

I like the delegate idea. I'm going to use it. :)

Robin S.
 

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