0

I am new to C# and I created a user control similar to the person from this thread:

add user control to a form

only, I used 4 dropdowns. I created a custom user control whose class is called CustomBaseUserControl.cs. It has all the selected index changed events for each dropdown. From the the form, call it TheFormControl, that the CustomBaseUserControl was dropped into, how do I access those event change values?

Thanks in advance!

Community
  • 1
  • 1
Allen
  • 181
  • 1
  • 4
  • 16

1 Answers1

1

If you need to retrieve selected index on TheFormControl, you could either

Use variables to store the value on CustomBaseUserControl, in this case you'll have to listen the SelectedIndexChanged events and updates your values.

Trigger a custom selected index changed from CustomBaseUserControl to TheFormControl

--

class CustomBaseUserControl: UserControl{
int idx1=-1;
public CustomBaseUserControl()
{
    Initialize();
    //Fill ComboBox

    //Suscribe Event
    combobox1.SelectedIndexChanged += combobox1_SelectedIndexChanged;
}
  void combobox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = combobox1.SelectedIndex;
        if (index != idx1)
        {
        idx1=index;
        RaiseIndexChanged(e);
        }

    }

        public virtual void RaiseIndexChanged(EventArgs ea)
    {
        var handler = OnIndexChanged;
        if (OnIndexChanged != null)
            OnIndexChanged(this, ea);
    }
    public event EventHandler OnIndexChanged;
}  

Caller class would be

class TheFormControl: Form
{
    CustomBaseUserControl cb;
    public TheFormControl()
    {
        Initialize();
        cb = new CustomBaseUserControl();
        cb.OnIndexChanged +=cb_OnIndexChanged;
    }
    void cb_OnIndexChanged(object sender, EventArgs e)
    {
     // Here you know index has changed on CustomBaseUserControl
    }
}
R Quijano
  • 1,301
  • 9
  • 10
  • Thanks for your suggestion R. Quijano but when I put a break point in the combobox1_SelectedIndexChanged, it doesn't get triggered (hit the break point) when the user changes the value of the dropdown. Would you have an idea to shake the marble in my head? I have been scratching my head for awhile now. thanks! – Allen Jan 08 '14 at 18:12
  • Thanks RQuijano!! That's exactly what I need! I had written some logic but it didn't quite work but you gave me the right direction! thank you! – Allen Jan 08 '14 at 19:31