Getting Button Events from Multiple manually instantiated controls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • !NoItAll
    Contributor
    • May 2006
    • 297

    #1

    Getting Button Events from Multiple manually instantiated controls

    I have created a simple custom control - we will call it a "panel with buttons". Here is how I instantiate it:

    Code:
     
    Dim Panel(PanelCount) As Panel.Panel
    Dim iTop As Integer = 5
    
            For I As Integer = 0 To (PanelCount - 1)
                Panel(I) = New Panel.Panel
                Panel(I).Parent = Me
                'the following just position the panels
                Panel(I).Top = iTop + 10
                iTop += 110
                Panel(I).Left = 5
            Next I
    So what I wind up with are multiple instances of my panel. Each of these panels has a button on it called Button1.
    My question is - how do I capture this button event in a way that I will know which panel index it came from?

    In the custom control there is only the Button1_click event - and when I click on it in any of the multiple panels I manually instantiated the event does occur, but I'm clueless as to how I can tell which one of my instantiated buttons were clicked...
  • !NoItAll
    Contributor
    • May 2006
    • 297

    #2
    Ok - found the answer. I will try to post some sample code later but for now suffice to say that in my custom control I needed to create an event and then raise that event.
    So I wound up doing something like:

    Code:
    Public Event ButtonPush(Index as Integer)
    Then in the Button_Click event I put

    Code:
    RaiseEvent ButtonPush(CIndex(me.tag))
    I will talk about the tag property in a minute...

    In the form that is using my custom control there were a few things I needed to do:

    When instantiating each control I need to set the tag property to an index (e.g. 0, 1, 2, 3, etc). This is the code I used:

    Code:
    Panel(I) = New Panel.Panel with {.Tag = I}
    Now each panel has a unique tag which I will use as an index.

    Next I needed to create a sub in my form to handle the event I wanted to catch

    Code:
    Sub PanelButtonPush_Clicked(Index as Integer) ' notice there is no Handles directive
       'code to do something here
    End Sub
    Finally I needed to add the event handler so it would get processed in the message pump...

    Code:
    AddHandler Panel(I).PanelButtonPush, AddressOf PanelButtonPush_Clicked
    Now whenever the button on any of the custom control array elements gets pushed the PanelButtonPush _Clicked event fires and the "Index" tells me which panel array element it came from.

    Comment

    Working...