I have a user control consisting of a combobox and a label. In my application I need to enable or disable some other controls based on the selected value in that combobox. How do I access the SelectItemChanged event from the application?
Either way...neither of those are available to set properties for in the application because the combobox is encapsulated in the user control...
novacara
2009-07-30 19:36:31
Then youll need to tie another event or something back to the main application so you can update the other controls
SwDevMan81
2009-07-30 19:40:40
I don't understand what you are saying.
novacara
2009-07-30 19:43:35
See the following post at the accepted answer http://stackoverflow.com/questions/1088023/forward-a-keypress-event/1090259#1090259 Think of the Form1 as your main application and Form2 as your user control. Instead of OnKeyPress, use the SelectedValueChanged event i posted above
SwDevMan81
2009-07-30 19:55:59
+2
A:
You need to send the event handler that will handle the event to your custom control. Something similar to the code below:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
YourUserControl userctr = new YourUserControl();
//Sent the event handler linked to OnSelectedValueChanged
userctrl.HandleSelectedValueEvent(new EventHandler(OnSelectedValueChanged));
}
private void OnSelectedValueChanged(object sender, EventArgs e)
{
//Do something
}
}
public partial class YourUserControl : UserControl
{
public void HandleSelectedValueEvent(EventHandler handler)
{
this.comboBox1.SelectedIndexChanged += handler;
}
}
Francis B.
2009-07-30 19:53:50