1

Ok so i've been struggling to get my head around this for a few fays not and need some help.

so i have a series of panels that are generated

example:

For i as integer 1 to dt.rows.count
    dim subpan as new panel
    *Code for creating panel"
Next

the problem is i need to be able to add event handlers to each of them including, click, mouseEnter and mouseLeave but i can't figure out how to index each panel so that they can be accessed and identified. i tried using a property but that didn't seem to work or i was doing it wrong.

thanks in advance for the help.

2 Answers 2

2

You want the AddHandler function:

For i as integer 1 to dt.rows.count 
    dim subpan as new panel 
    subpan.ID = "subpan1"  ' REQUIRED AND MUST BE UNIQUE
    AddHandler subpan.Click, AddressOf subpan1_Click  ' CLICK EVENT HANDLER ALSO UNIQUE
    ' ETC.
Next 

Private Sub subpan1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' CODE TO HANDLE CLICK EVENT
End Sub

I should mention that you can route all click events to the same event handler, they don't have to be unique unless each panel requires custom logic.

Sign up to request clarification or add additional context in comments.

3 Comments

how do u do subpan.ID as ID isnt a property of a panel
My apologies, I did that off the top of my head. It might be the Name property.
yeh ive done that but its only assigning it to the first one in the series
0

You can use AddHandler to add the handler:

For i As Integer = 1 To dt.Rows.Count
    Dim subpan As New Panel()

    'Code for creating panel

    'Attach events:
    AddHandler subpan.Click, AddressOf Some_Listener
    'etc.
Next

You can then access the current Panel in the event handlers by casting the sender argument to Panel.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.