1

I have a large number of buttons on a form (144 buttons). The names of each button is a combination of different factors.

I need them to all do the same thing when pressed. I have this event under a Private Sub called "ClickButton".

I want to use a loop, to AddHandler each button to the "ClickButton" event.

First however, I need to address each button. I do this by constructing their name dynamically in a loop.

Dim PeriodRoomID As New Button
Dim cntrl() As Control    

For Each Row In dsAllRooms.Tables("sqlAllRooms").Rows
            For Count = 1 To 13
                RoomID = Row.Item(0)
'This is where the name of each room is dynamically created. 
'It is stored in PeriodRoomID.name
                PeriodRoomID.Name = "R02" & RoomID.PadLeft(3, "0"c) & Count
                cntrl = Me.Controls.Find(PeriodRoomID.Name, True)
                AddHandler cntrl(0).Click, AddressOf ClickButton
            Next
        Next

The problem is nothing happens. The AddHandler doesn't work. However, if I simply write one of the names of the buttons;

AddHandler R020011, AddressOf ClickButton

Then it does work. Something is going wrong at Me.Controls.Find. It doesn't seem to be able to find any button under that name which is strange because I use the same code in other sections and it finds the button.

svick
  • 236,525
  • 50
  • 385
  • 514
Pejman Poh
  • 493
  • 2
  • 8
  • 20
  • At what point in the page lifecycle is this routine running? At what point are you creating the buttons? – jmaglio Mar 05 '12 at 00:05
  • Are there other buttons in the same form? In other words, would setting the handler for all of the buttons on the form work for you? – svick Mar 05 '12 at 00:08
  • @jfmags The buttons are not dynamically created, they are created with the form. – Pejman Poh Mar 05 '12 at 00:37
  • @svick There are other buttons unfortunately :( – Pejman Poh Mar 05 '12 at 00:37
  • Why dont you just specify the OnClick="ClickButton" in the design view then for each button? – jmaglio Mar 05 '12 at 00:41
  • @jfmags That is so redundant. I would have to do that for 144 buttons. – Pejman Poh Mar 05 '12 at 01:22
  • 2
    Why is PeriodicRoomID a button instance when it's just used as a string var to pass to find() function? Are your form created buttons named already, or are you trying to achieve that in that loop? – nik Mar 05 '12 at 03:08
  • Add these debugging statements prior to the `AddHandler` line: `Debug.Assert(cntrl.Length=1) : Debug.Assert(cntrl(0).Name=PeriodRoomId.Name)` and confirm they don't fail when you run this form (in a Debug configuration). – Mark Hurd Mar 05 '12 at 04:01

1 Answers1

1

You define Dim PeriodRoomID As New Button out of for statement so each time you set name you set it to the same button PeriodRoomID so you don't need this button and cntrl neither at all just :

For Each Row In dsAllRooms.Tables("sqlAllRooms").Rows
            For Count = 1 To 13
                RoomID = Row.Item(0)
                'This is where the name of each room is dynamically created.   
                dim Ctrls = Me.Controls.Find("R02" & RoomID.PadLeft(3, "0"c) & CStr(Count), True)
                If (Ctrls.Count > 0) Then
                   AddHandler Ctrls(0).Click, AddressOf ClickButton
                End If
            Next
Next
Amen Ayach
  • 4,288
  • 1
  • 23
  • 23