Reputation: 4287
I am trying to do a favorites control in my system so I have a form with some ComboBoxs, TextBoxes and buttons and in my code I am adding an envets like SelectedValueChanged event to ComboBoxs, TextChange inTextBoxes and Click to buttons.
Next to each control I have a panel control to which I am adding an instance of my user control (ToolStripButton) I created to show the favorites.
In order to collect the data for the favorites I need to listen to the events of the controls in my form (e.g. listen to the SelectedValueChanged event of a ComboBox and save this value to the favorites).
Is there a way I can listen to these events from the favorites user control ?
Upvotes: 1
Views: 1082
Reputation: 236318
In order to listen some event you should subscribe to that event. You can either subscribe to event in your favorites user control (you will need to pass control which raises event into your user control):
// code for you FavoritesUserControl
public void Subscribe(ListBox listBox)
{
listBox.SelectedValueChanged += ListBoxSelectedValueChanged;
}
private void ListBoxSelectedValueChanged(object sender, EventArgs e)
{
// do what you want
}
Usage:
favorites1.Subscribe(listbox1);
Or you can subscribe to event on your form, and notify user control in event handler:
// code for your Form
private void ListBoxSelectedValueChanged(object sender, EventArgs e)
{
favorites1.DoSomething();
}
Upvotes: 1