all groups > visual studio .net general > january 2007 >
You're in the

visual studio .net general

group:

VBE 2005 - 'Index' Property For A Control


VBE 2005 - 'Index' Property For A Control Scanboy
1/28/2007 4:32:00 PM
visual studio .net general:
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.


Re: VBE 2005 - 'Index' Property For A Control RobinS
1/28/2007 4:52:37 PM
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.
-------------------------------------------
[quoted text, click to view]

Re: VBE 2005 - 'Index' Property For A Control Scanboy
1/28/2007 6:25:01 PM
Rob,

Thanks! I have had a look around for control arrays in the VBE help system,
and
found out why. Event handlers made the use of control arrays redundant.

Thanks for introducing me to 'AddHandler', 'RemoveHandler', 'DirectCast'. I
have
just looked at the 'Control Arrays For Visual Basic 6.0 Users' page.

Would the following code equally work as well?

Private Sub Buttons_Clicked(ByVal Sender As System.Object, _
ByVal SysEvntArgs As System.EventArgs) Handles Button1.Click,
Button2.Click, _
Button3.Click, Button4.Click
Select Case DirectCast(Sender, Button).Name
Case Button1.Name
MsgBox("You clicked on the first button!")
Case Button2.Name
MsgBox("You clicked on the second button!")
Case Button3.Name
MsgBox("You clicked on the third button!")
Case Button4.Name
MsgBox("You clicked on the fourth button!")
End Select
End Sub


SB
-----------------------------------------------------------------------------------------------
[quoted text, click to view]
Re: VBE 2005 - 'Index' Property For A Control Scanboy
1/28/2007 7:59:00 PM


[quoted text, click to view]

I am pleased to be able to report that the code does work perfectly well!!!
:)

[quoted text, click to view]
Re: VBE 2005 - 'Index' Property For A Control RobinS
1/28/2007 9:20:40 PM
It would work just as well (and does, as you reported). Putting all the
buttons
on the Handles clause works okay, but may cause you problems if you add or
remove any buttons. You may not remember to add them to the Handles
clause (on my Visual Studio, that would be so far out to the right, I
wouldn't
see it), or remove any if you delete the button.

Robin S.
-------------------------------------------
[quoted text, click to view]

Re: VBE 2005 - 'Index' Property For A Control Scanboy
1/29/2007 1:42:00 AM


[quoted text, click to view]

OK. Thanks for the tip.

[quoted text, click to view]
AddThis Social Bookmark Button