I have this code:
typeof(ComboBox).GetProperty("SelectedItem").SetValue("comboBox1", "", null);
It is not working. Here is the error:
“Object does not match target type”
What is wrong?
Update:
I have the name of my object (comboBox in string. like:
string []s = new s[3];
s[0]="comboBox0";
s[1]="comboBox1";
s[2]="comboBox2";
I want to set a value to the property SelectedItem to my comboBox s[i]. How can I do it?
SetValueneed to haveComboBoxobject, not string “comboBox1”. I believe you have a ComboxBox object in your application, assume:So you can call:
If you use .NET 4.5, you can get rid of the last input:
Edit: based on your comment, you don’t need to use reflection, below is you can get combox1 if only knowing the name: