views:

337

answers:

1

I wanted to know if it was possible to create a control from another control and which this new control could process certain events.

For example, lets say we have a Button that once it is clicked on will create a ComboBox. Could this new ComboBox be capable of processing a certain event such as a SelectionChanged event?

+2  A: 

Sure thing. Simply provide an event handler and hook it up to the event:

public Window1()
{
 InitializeComponent();

 Button button = new Button();
 button.Click += new RoutedEventHandler(button_Click);
}

void button_Click(object sender, RoutedEventArgs e)
{
 ComboBox combo = new ComboBox();
 combo.SelectionChanged += new SelectionChangedEventHandler(combo_SelectionChanged);
}

void combo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
 // Do your work here.
}
Charlie
So if you add, for example, 10 new ComboBoxes you will able to handle the events of each and every one of them?
Partial
Oh and will you be able to make them do different things?
Partial
Can you handle the events of each and every one? Definitely. The code I have provided will handle the SelectionChanged event for every single combo-box you add.Can you make them do different things? Sure, but there will need to be some underlying data that you can get at to differentiate between the combo-boxes. What do you have in mind?
Charlie
What I have in mind is a TabControl that has button in it that allows to create new tabs. Each tab would be used by the user to write some information.
Partial