Adding Event Handlers Dynamically

callraheel

Well-known member
Joined
Dec 12, 2004
Messages
65
Location
London,UK
Programming Experience
1-3
Hi there
I have created a form , a main panel and a lot of panels inside the main panel by getting data from database... Everything gone fine.
Now i came across a problem.
In those "a lot of panels" i have a button "View Full Details", "Update" and "Delete"....
As panels are added on run time so i need such a handler which could give the reference of panel through which button is clicked...

I hope you got what i want

For now im using the following handler for clicking of the panel... note there is a loop which create new panels and then add handles to these panels and add them to main panel

' code im using to add handler
AddHandler objPanel.Click, AddressOf _ChangeBackColor


' code of the sub _changeBackColor
Private Sub _ChangeBackColor(ByVal e As System.Object, ByVal ev As System.EventArgs)
' i need to change the back color of current panel being clicked...
pnlMain.BackColor = Color.PeachPuff

End Sub

It work well but do not give me the reference of panel at which the mouse was clicked and also i need to change the back color of current panel...not main panel...How can i do this....Please help out........I'm stucked here...:rolleyes:

Regards
Raheel
 
The signature that is written for a button's Click Event handler by the IDE is :
ByVal sender As System.Object, ByVal e As System.EventArgs
It will definately work as you've written, but the above signature is more commonly used.
The first parameter sender (e in your signature) is the reference to the button that raised the event. You can use the Parent property of the button referenced to get a reference to the panel.
 
Well i tried the following in the Procedure

PrivateSub _ChangeBackColor(ByVal e As System.Object, ByVal ev As System.EventArgs)

Dim objPanel As Panel = e.Parent
objPanel.BackColor = Color.Navy

EndSub

But it changed the back color of all panels , i think it changed the color of main panel...I just need to change the color of current panel being clicked....

So will you please write some code to do this in this procedure


 
Wait Wait......... Its done..... I tried e.BackColor=Color.Navy........
Actually in object there is no property shown thats why i was a bit confused But everything settled now...

Paszt thanx for your help

Regards
Raheel
 
Sorry, for some reason I thought you were saying there was a button in the panel that was clicked. The 'e' parameter (as you've written the signature) is the panel that was clicked. Glad you got it working.
I sill think it would be best to use the standard signature, just a suggestion.
callraheel said:
in object there is no property shown
as good practice, you should avoid late binding. With option strict were turned on, the IDE doesn't allow late binding and it makes for better code to do the type casting yourself. The code would be:
VB.NET:
Private Sub _ChangeBackColor(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim objPanel As Panel = CType(sender, Panel)
    objPanel.BackColor = Color.Navy
End Sub
 
I have to concur with regards to the method signature. It is not a great idea to give the arguments other than the standard names without a very good reason, but it is a very bad idea to give one argument the same name that is normally used for the other. That's a very good way to cause confusion. If you must give them different names, at least make the names descriptive.
 
Back
Top