-1

I have to process some controls by their names

this.Controls.Find(String.Format("lbl{0}", index),
            true)[0].Text = data[i].ToString();

but when I tryed to get a Combobox by name it can't show the SelectedIndex property

this.Controls.Find(String.Format("cmbDat{0}", index), true)[0].

Who i can do?

  • What has to do the Find method with the SelectedIndex property of a combobox? You don't use any SelectedIndex property in the code above – Steve Oct 31 '13 at 17:50
  • -.- I can't use a SelectedIndex property in the code above because intellisense don't show this option in my project – user2921326 Oct 31 '13 at 18:01

2 Answers2

3

You need to cast it to a ComboBox, because Find() returns a Control, which does not contain the SelectedIndex property.

Try this instead:

ComboBox theComboBox = this.Controls.Find(String.Format("cmbDat{0}", index), true) as ComboBox;

// Verify the combo box was found before trying to use it
if(theComboBox != null)
{
    // Do whatever you want with the combo box here
    theComboBox.SelectedIndex = ???
    theComboBox.Text = ???
}
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
0

This way looks easier https://stackoverflow.com/a/1639106/12537158, all you need is this line:

Control ctn = this.Controls[name];
Navigatron
  • 2,065
  • 6
  • 32
  • 61
latifa
  • 21
  • 3