Question

This must have been asked before, but I am fairly new and don't quite know how to express myself...

1) I have a UserControl that basically acts as a toolbar. I re-use the toolbar in each window, hence the need for a uc. 2) The toolbar is filled with buttons 3) the usercontrol doesn't act on the button (no code), but it should pass the event back to the parent window so the code in the parent window fires up.

How can I do this? Is this a routed event? any sample code in vb.net would be appreciated!

Was it helpful?

Solution

On you user control, you need events that you can fire when the buttons are clicked. Then in your form, you handle the events just like you do for every other control. IE:

public event Button1_ClickedEventHandler Button1_Clicked;
public delegate void Button1_ClickedEventHandler(object sender);

private void Button1_Click(object sender, EventArgs e)
{
    if (Button1_Clicked != null) {
        Button1_Clicked(this);
    }
}

You can call the event whatever you want and pass whatever you want. Here you will notice I am NOT sending the button but THIS, which in this case should be the User Control.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top