Handling an Event

  • Thread starter Thread starter Sean
  • Start date Start date
S

Sean

I have an class that I want to have a PictureBox as a
member of (passed in), and I want to be able handle the on
click event for it. How do I do it?

Right now, the PictureBox is declared:

Public Class MyClass

Friend WithEvents f_PictureBox As
System.Windows.Forms.PictureBox

Private Sub PictureBox_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
f_PictureBox.Click

.....

End Sub

End Class

But it doesn't except the last f_PictureBox.Click as
valid. I get a Handle requires WithEvents variable error.

Help
 
I have an class that I want to have a PictureBox as a
member of (passed in), and I want to be able handle the on
click event for it. How do I do it?

Check out the AddHandler and RemoveHandler statements. When you pass in
the PictureBox, you can use AddHandler to assign the event handler to the
PictureBox.

HTH
 
After getting over being stupid this morning, I actually
did it more cleverly than that, and just have an object
reference in my class already associated with the correct
event handler, so all I have to do is assign that
reference by setting a MyClass.PictureBox =
pbSomePictureBox.
 
Back
Top