How to bubble an event from user control to form?

Sam_Sorrow

Newcomer
Joined
Dec 11, 2003
Messages
4
I have a previously selfcreated user-control which contains a
picturebox and some additional code, also included is a method
that changes the displayed Image in the Picturebox on Clicking.
This works fine so far.
Now I place this user control on a new form and i want it to fire a
Click-Event on my form when a user clicks it. The Image in the
Pictuebox changes on clicking but the Eventhandler on my form
doesnt fire anything on clicking! I need a possibility for the users
of my user control to write their own code for a click-event on
that control.
I read about the possibility to bubble events from a user control
to its parent control but it was related to web forms. Is it
equivalent to Windows forms? If it is, how does it
work because i dont get to the point yet...
Thanks in advance for your help,
Jan
 
what I would do is, make an event in the class of the selfcreated user-control called PictureBoxClicked, then add a private method called OnPictureBoxClicked(object sender, EventArgs e), and add OnPictureBoxClicked(this, EventArgs.Empty) to the last line of whatever code handles the OnClick event in the selfcreated user-control.

OnPictureBoxClicked would look like this

OnPictureBoxClicked(object sender, EventArgs e)
{
PictureBoxClicked(sender, e);
}
 
Dont understand that proposal

I cannot follow your proposal. How can I catch the Click event
within my parent form that contains the user control?
 
The parent form would need to listen to the event.

inside your parent form, you would need to do something like

selfCreatableUserControl myControl = new selfCreatableUserControl();

//tell the control what method(s) you want invoked after the control's picture box has been clicked and the image has changed
selfCreatableUserControl.PictureBoxClicked += new EventHandler(UserControlPictureClicked);
 
still confused

Many thanks so far, but how do I add an event to my user control
class? I think to do this a need a delegate to?
still confused,
jan
 
Could you post the code that you got to work?

I'm trying to get this done 2.0 style and I'm having difficulty registering the event on the aspx page because of the partial class business
 
Back
Top