Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi I have this code.

private void cmbIndex()
      {
          foreach (Control myControl in this.Controls)
          {
              ComboBox comboBox = myControl as ComboBox;
              if (comboBox != null && comboBox.Items.Count > 0)
              {
                  comboBox.SelectedIndex = 0;
              }
          }
      }


This selects all combo box index to zero. But I have one combo box with a selectedindexchanged event.
cmbIndex() does not fire anymore. Should I revise my codes for cmbIndex? Thank you
Posted
Comments
Sergey Alexandrovich Kryukov 11-Sep-14 1:57am    
cmbIndex cannot be "fired". This is a method. And it may or may not be used as a handler. Check up your += operators...
—SA
Bernhard Hiller 11-Sep-14 2:12am    
Is that a follow-up question to
http://www.codeproject.com/Questions/729061/selectedindexchanged-not-firing?arn=0
?
Or did you expect the SelectIndexChanged event to be fired when you set SelectedIndex=0 ?
Note the "Changed" part of the name - if the index was already 0, and you set it to 0 again, it is not changed, hence no event!

If I understood correctly you want tu use this method to init/reinit all your combo box controls that do not have yet index selected. For doing that you should change your method like this (because SelectedIndex with value -1 means no index selected):
C#
private void cmbIndex()
{
    foreach (Control myControl in this.Controls)
    {
        ComboBox comboBox = myControl as ComboBox;
        if (comboBox != null && comboBox.Items.Count > 0 && comboBox.SelectedIndex < 0)
        {
            comboBox.SelectedIndex = 0;
        }
    }
}
 
Share this answer
 
C#
//try this code.
private void cmbIndex()
        {
  foreach (Control c in this.Controls)
        {
            foreach (Control childc in c.Controls)
            {
                if (childc is ComboBox)
                {
                 ComboBox comboBox = childc as ComboBox;
                if (comboBox != null && comboBox.Items.Count > 0)
                {
                    comboBox.SelectedIndex = 0;
                }
                  // ((ComboBox)childc).SelectedIndex = 0;
                }
            }
        }
        }


if any issue then let me know.

-> M.U
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900