passing events from usercontrols

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

I place a usercontrol (2) on another usercontrol (1). The second usercontrol
has a button. Is there a way usercontrol (1) can know when the button is
clicked on usercontrol (2) without usercontrol (2) having an instance of
usercontrol (1)?

Reagrds
Stijn
 
I place a usercontrol (2) on another usercontrol (1). The second usercontrol
has a button. Is there a way usercontrol (1) can know when the button is
clicked on usercontrol (2) without usercontrol (2) having an instance of
usercontrol (1)?

Have usercontrol (2) raise an event when the button is clicked, and have
usercontrol (1) subscribe to it.
 
Cam said:
Have usercontrol (2) raise an event when the button is clicked, and have
usercontrol (1) subscribe to it.

thanks Cam for the fast reply. Since I 'm just a beginner I have some
problems interpreting your reply. I know what an event is but how can I
subscribe the Control to it?

regards
Stijn
 
thanks Cam for the fast reply. Since I 'm just a beginner I have some
problems interpreting your reply. I know what an event is but how can I
subscribe the Control to it?

No probs, I'll attempt to give an example, but this is rather off the top of
my head so you may need to look further in the msdn documentaion to get it
working correctly. In the example I'll just use two generic classes, with an
instance of one class declared in the other. User controls are just classes
in NET terms so it should work the same way in your example.

public class Control2 // analogous to (2) in your question
{
// declaration of the event type this class will raise to the outside world
// the two params are convention only, usually the first is a reference to
// the object sending the event, and the second is an object used to pass
// parameters to consuming classes.
public delegate void buttonClickedHandler(object sender, EventArgs e);

// this is an instance of the event type above. You could have several
of these
// for clicks on different buttons for instance.
public buttonClickedHandler OnButtonClicked;

// example function to raise event -- this would occur in the handler for
the button
// click event in your case
public void Click()
{
OnButtonClicked(this, null);
}
}

public class Control1 // analogous to (1) in your question
{
public Control2 c;

// subscribe to the event in Control2, basically just assign a function to
run when
// the event is raised.
c.OnButtonClicked += new Control2.buttonClickedHandler(buttonClickedInC);

// function to handle the event when raised
// signature must match the delegate in Control2 to which we want to
subscribe
private void buttonClickedInC(object sender, EventArgs e)
{
// do something in Control1 triggered by the click in Control2
}
}

Phew! Hope that clarifies things! In the end I cheated a little and
consulted O'Reilly's "Programming in C#" to help out with the syntax which
can be difficult to remember exactly if you don't use it every day!! :)

Hope this helps!
C.
 
public class Control1 // analogous to (1) in your question
{
public Control2 c;

This should have been:
-------------------------------------------
public class Control1 // analogous to (1) in your question
{
public Control2 c;

public Control1() // default constructor
{
c = new Control2(); // need an instance to work with
}
 
Back
Top