1

[edit]

calling the follows directly is not possible in my code:

void control_Changed(object sender, EventArgs e) 

This function is loop through a Collection of DropDownListBox, and each DropDownListBox has different Select_Change function. Also they are not in the same page, they collection of DropDownListBox is come from different user control of the page.


I saw a lot of solution are simply calling the function that the event should trigger.. But this will not be working on my case.

I have a code that will mapping the data to a collection of dropdownlistbox and select the proper dropdownlistbox item for each dropdownlistbox.

So, is kind of like this:

foreach (Control aControl in aControlCollection){
    if (aControl.GetType() == typeof(RadComboBox))
                {
                    bool FoundItem = false;
                    RadComboBox aComboBox = (aControl as RadComboBox);
                    foreach (RadComboBoxItem aComboItem in aComboBox.Items)
                    {
                        Debug.WriteLine("aComboItem " + aComboItem.Text + " Value" + aComboItem.Value);
                        if (aComboItem.Value.ToLower() == _dataObject.ToString().ToLower())
                        {
                            //aComboBox.SelectedIndex = aComboBox.Items.IndexOf(aComboItem);
                            aComboItem.Selected = true;
                            FoundItem = true;
                            ~~~FIRE EVENT HERE~~~~~
                            //break;
                        }
                        else {
                            aComboItem.Selected = false;                        
                        }
                    }
                    if (!FoundItem)
                    {
                        RadComboBoxItem aComboItem = new RadComboBoxItem();
                        aComboItem.Value = _dataObject.ToString();
                        aComboItem.Text = _dataObject.ToString();
                        aComboBox.Items.Add(aComboItem);
                        aComboBox.SelectedIndex = aComboBox.Items.IndexOf(aComboItem);
                    }
                }

            }
}

Normally in the page, when user select the a first dropdownbox, the 2nd dropdownbox that follows will generate the proper dropdownlist item according to the first dropdownbox (from the first dropdownbox selectindexchange event).

So I wonder if there is anyway I can fire the DropDownListBox programmatically?


Just to make it even more clear, the above function is call by iterates all DropDownListBox on the page, so they can be link into different function.

Cœur
  • 37,241
  • 25
  • 195
  • 267
King
  • 65
  • 1
  • 6
  • So you want to loop through a collection of DropDownLists and call the event that is specific to each one? – Abe Miessler Apr 04 '11 at 18:44
  • Yes, because each of them using different function and in this function there I have no way to call their function. – King Apr 04 '11 at 18:47

2 Answers2

1
Combobox_SelectedItem(null, null);

You can forge any arguments you desire into the parameters, if needed.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
  • Like I said, there is no way to call it function there.... They are a collection of DropDownBox and the fucntion is loop through a collection of DropDownBox, each DropDownBox links to different function.... – King Apr 04 '11 at 18:52
  • I'm not following why that matters, it's still just boils down to invoking the method, it's possible you might need a switch statement to figure out which method to call – Chris Marisic Apr 04 '11 at 18:55
  • Each of the DropDownListBox is from different User Control, this function doesn't know where the DropDownListBox come from, it is a generic function. – King Apr 04 '11 at 19:03
  • Does this happen to be to switch the Captioners at NCI? –  Apr 04 '11 at 19:05
  • @King do each of those user controls expose the event you want to fire? If they do you can call it exactly like my example. If the event is not public, you might need to try to use reflection to access it. Regardless of either of these cases you need to fundamentally know who to call though. The only other route is using polymorphism but I don't think that would work for your model well since these are already user controls. – Chris Marisic Apr 04 '11 at 19:32
  • No, they don't. I made each of the User Control self contains, each User Control contains their form, it has DropDownListBox, and the SelectIndexChange function is inside that User Control as well. The above code I have is loop through all User Control, find all DropDownListBox, and map data to the DropDownListBox. I wonder the reflection you said can use to get the function to call it? – King Apr 04 '11 at 19:37
  • http://stackoverflow.com/questions/135443/how-do-i-use-reflection-to-invoke-a-private-method-in-c for some help on calling a private method via reflection. Once again this comes down to needing to fundamentally KNOW what you're calling. But with these events it could be really simple if the Event you're calling is the same name in each user control. If the names are different I would consider standardizing them if that's an option. – Chris Marisic Apr 04 '11 at 19:41
0

If you were to use the traditional void control_Changed(object sender, EventArgs e) code...

if (aComboItem.Value.ToLower() == _dataObject.ToString().ToLower())
{
  //aComboBox.SelectedIndex = aComboBox.Items.IndexOf(aComboItem);
  aComboItem.Selected = true;
  FoundItem = true;
  control_Changed(aComboItem, new EventArgs());
}


void control_Changed(object sender, EventArgs e) {
  // your code here
}