Harald
Harald

Reputation: 43

ComboBox filling

I have a problem filling my ComboxBox. Here´s my code:

string[] test = { "Ausgaben", "Eingaben" };
foreach (string r in test)
{
   cbEinAus.Items.Add(r);
}

The values from the string array are in the ComboBox, but the selected item is a empty string. I want one of this two string from the array to be my selected item.

I already tried with the SelectedItem property, but that doesn´t work.

Maybe its a simple solution... Can anybody help me please?

Upvotes: 0

Views: 378

Answers (5)

jasel
jasel

Reputation: 41

cbEinAus.SelectedIndex = 0;

replace item to index

Upvotes: 0

SwDevMan81
SwDevMan81

Reputation: 50018

You can set the Text property.

cbEinAus.Text = test[0];

More examples can be found at How do I set the selected item in a comboBox to match my string?

Upvotes: 0

CodingGorilla
CodingGorilla

Reputation: 19862

Use:

cbEinAus.SelectedIndex = 0;

You can replace the 0 with the zero based index of whichever item you want to select.

Upvotes: 1

V4Vendetta
V4Vendetta

Reputation: 38230

Try setting the SelectedItem as cbEinAus.Items[0]

Upvotes: 0

Neil N
Neil N

Reputation: 25278

Try

cbEinAus.Items[vbEinAus.SelectedIndex]

instead of SelectedItem. usually works for me.

Upvotes: 0

Related Questions