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.
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] "Scanboy" <Scanboy@discussions.microsoft.com> wrote in message news:70194227-42B3-419E-ADBB-7C766B8AABA3@microsoft.com... > 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
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] "RobinS" wrote: > 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. > ------------------------------------------- > "Scanboy" <Scanboy@discussions.microsoft.com> wrote in message > news:70194227-42B3-419E-ADBB-7C766B8AABA3@microsoft.com... > > 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 > >
[quoted text, click to view] "Scanboy" wrote: > 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 >
I am pleased to be able to report that the code does work perfectly well!!! :) [quoted text, click to view] > > SB > ----------------------------------------------------------------------------------------------- > "RobinS" wrote: > > > 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. > > ------------------------------------------- > > "Scanboy" <Scanboy@discussions.microsoft.com> wrote in message > > news:70194227-42B3-419E-ADBB-7C766B8AABA3@microsoft.com... > > > 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 > > > >
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] "Scanboy" <Scanboy@discussions.microsoft.com> wrote in message news:EE4A0D5B-FC8D-4154-BFF3-EC9D8296610D@microsoft.com... > 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 > ----------------------------------------------------------------------------------------------- > "RobinS" wrote: > >> 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. >> ------------------------------------------- >> "Scanboy" <Scanboy@discussions.microsoft.com> wrote in message >> news:70194227-42B3-419E-ADBB-7C766B8AABA3@microsoft.com... >> > 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 >> >> >>
[quoted text, click to view] "RobinS" wrote: > 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.
OK. Thanks for the tip. [quoted text, click to view] > ------------------------------------------- > "Scanboy" <Scanboy@discussions.microsoft.com> wrote in message > news:EE4A0D5B-FC8D-4154-BFF3-EC9D8296610D@microsoft.com... > > 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 > > ----------------------------------------------------------------------------------------------- > > "RobinS" wrote: > > > >> 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. > >> ------------------------------------------- > >> "Scanboy" <Scanboy@discussions.microsoft.com> wrote in message > >> news:70194227-42B3-419E-ADBB-7C766B8AABA3@microsoft.com... > >> > 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 > >> > >> > >> > >
Don't see what you're looking for? Try a search.
|