Question

I am developing project in widows c#.net. In the form i have more than 8 combobox control. I will load data to combobox2 when combobox1 selection is changed like below.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
     comboBox2.DataSource = DataTable;
     comboBox2.DisplayMember="Name";
     comboBox2.ValueMember = "ID";
}

Combobox3 will be loaded when i select combobox2 like below.

 private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
 {
     comboBox3.DataSource = DataTable;
     comboBox3.DisplayMember = "Age";
     comboBox3.ValueMember = "AgeCode";
 }

Like i will load data to the rest of combobox.
The problem here is error will occur, if i did not check in comboBox1_SelectedIndexChanged method whether comboBox2 is loaded or not.

I know we can check this by using Boolean variable, but horrible thing is we need to maintain its "true/false" state for rest all methods.

so what I have thought to fix this in simple way is that, I will use Add(combobox, Methodname) method and Remove(combobox, method) methods to add and remove the comboBox_SelectedIndexChanged function from combobox SelectedIndexChanged event.

But I couldn't pass the method as parameter. Can anybody tell me that how to pass method as parameter for my requirement.

Était-ce utile?

La solution

By method parameter in Add and Remove method if you meant comboBox1_SelectedIndexChanged and/or comboBox2_SelectedIndexChanged then solution is

private void Add(ListControl dropDownList, EventHandler handlerMethodName)
{
   dropDownList.OnSelectedIndexChanged += handlerMethodName;
   //some logic here
}

private void Remove(ListControl dropDownList, EventHandler handlerMethodName)
{
   dropDownList.OnSelectedIndexChanged -= handlerMethodName;
   //some logic here
}

Please note: It is DropDownList in ASP.NET, ComboBox is for WinForms applications.

For more refer: MSDN - DropDownList and MSDN - ListControl.SelectedIndexChanged Event

Autres conseils

I am not sure what you are tryng to do. but why not a dictionary of ComboBox and methods?

like this:

var dict = Dictionary<ComboBox, Action<object, EventArgs>>();

private void Add(ComboBox c, Action<object, EventArgs>e) {
   dict[c] = e;
}

private void Remove(ComboBox c, Action<object, EventArgs> e) {
   dict.Remove(c);
}

And to call ::

private void CallHandler(ComboBox c, EventArgs e)
{
   dict[c](c, e);
}

Alternatively

private void AddHandler(ComboBox c, Action<object, EventArgs> e)
{
   c.SelectedIndexChanged += e;
}

private void RemoveHandler(ComboBox c, Action<object, EventArgs> e)
{
   c.SelectedIndexChanged -= e;
}

I am also not 100% sure that kind of solution you are looking for, but my understanding of your need are the following methods:

public void Add(DropDownList combo, EventHandler method)
{
    combo.SelectedIndexChanged += method;
}

public void Remove(DropDownList combo, EventHandler method)
{
    combo.SelectedIndexChanged -= method;
}

Now you can define your own method that should have the same signature as EventHandler delegate:

public void MyMethod1(object sender, EventArgs e)
{}

And you can register and unregister your methods by calling the above defined methods:

DropDownList lst = new DropDownList();
Add(lst, MyMethod1);
Remove(lst, MyMethod1);

But beware, that this might be not an optimal solution of your problem.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top