tags:

views:

23637

answers:

12

I have a string "test1" and my comboBox contains test1, test2, test3, how do I set the selected item to "test1"? I.e. how do I match my string to one of the comboBox items?

I was thinking of the line below but this doesn't work.

comboBox1.SelectedText = "test1";

Thanks

+1  A: 

The items in your ComboBox are strings ?

Have you tried:

comboBox1.SelectedItem = "test1";
Frederik Gheysels
SelectedItem is read-only
Henryk
No it is not:http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem.aspx
Frederik Gheysels
A: 
  • Enumarate ListItems in combobox
  • get equal ones listindex set combobox
  • listindex to found one.

EDIT: But if I see such a code as a code reviewer, I would recommend to reconsider all the method algorithm.

A: 

You don't have that property in the ComboBox. You have SelectedItem or SelectedIndex. If you have the objects you used to fill the combo box then you can use SelectedItem.

If not you can get the collection of items (property Items) and iterate that until you get the value you want and use that with the other properties.

hope it helps.

Megacan
A: 

Assuming that your combobox isn't databound you would need to find the object's index in the "items" collection on your form and then set the "selectedindex" property to the appropriate index.

comboBox1.SelectedIndex = comboBox1.Items.IndexOf("test1");

Keep in mind that the IndexOf function may throw an argumentexception if the item isn't found.

Spence
A: 

Supposing test1, test2, test3 belong to comboBox1 collection following statement will work.

comboBox1.SelectedIndex = 0;

ihcarp
A: 
_cmbTemplates.SelectedText = "test1"

or maybe

_cmbTemplates.SelectedItem= _cmbTemplates.Items.Equals("test1");
Dean
with this code, you assign a bool to the SelectedItem property ... won't work imho.
Frederik Gheysels
+8  A: 

This should do the trick:

Combox1.SelectedIndex = Combox1.FindStringExact("test1")
norbertB
tried and tested... this works fine :)!! Thanks...
MNM
A: 

Hm

comboBox1.SelectedText = "test1";

...works fine for me.

Are you sure the exact string is an item in the combo box?

Tor Haugen
+1  A: 

SelectedText is to get or set the actual text in the string editor for the selected item in the combobox as documented here . This goes uneditable if you set:

comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

Use:

comboBox1.SelectedItem = "test1";

or:

comboBox1.SelectedIndex = comboBox1.Items.IndexOf("test1");
Brian Rudolph
+12  A: 

Have you tried the Text property? It works for me.

ComboBox1.Text = "test1";

The SelectedText property is for the selected portion of the editable text in the textbox part of the combo box.

Andrew Kennan
THIS IS CORRECT. IT NEEDS UPVOTES! :)
Domenic
Surely that just sets the text in the editable area of the ComboBox rather than selecting the relevant item from the list? If the list items collection contains objects rather than just strings, then I doubt this would select the appropriate ListItem object, rather it would just set the Text property on the ComboBox?
TabbyCool
It does set the SelectedValue property of the control
Henryk
A: 

hi when test1 is combobox.valuemember, how set selectedindex? please help me.

A: 

for me this worked only:

foreach (ComboBoxItem cbi in someComboBox.Items)
                    {
                        if (cbi.Content as String == "sometextIntheComboBox")
                        {
                            someComboBox.SelectedItem = cbi;
                            break;
                        }
                    }

MOD: and if You have your own objects as items set up in the combobox, then substitute the ComboBoxItem with one of them like:

foreach (Debitor d in debitorCombo.Items)
                {
                    if (d.Name == "Chuck Norris")
                    {
                        debitorCombo.SelectedItem = d;
                        break;
                    }
                }

Please mark as answer if this helps.

gabore