How to search through all items of a combobox in C#? [closed]
Asked Answered
S

2

17

I have a combobox, and I would like to search through every element in it.

How can I do this? (also the number of items is not the same everytime, but this is not so important).

I am using c# windows form application.

Shophar answered 26/8, 2013 at 11:35 Comment(3)
There already are a lot of questions including answers on stackoverflow, for searching trough all items in a combobox...Volotta
Soner Gonul: it's a simple combobox, with 2 items in it. what is to show, really?! it was added visually in c# windows forms...Shophar
There are API methods to do exactly that: FindString, FindStringExactLeoraleos
J
37

you can do this

for (int i = 0; i < myComboBox.Items.Count; i++)
{
     string value = myComboBox.GetItemText(myComboBox.Items[i]); 
}
Judejudea answered 26/8, 2013 at 11:38 Comment(5)
could you help a bit more please? this code doesn't return correct value.. it returns in the textbox "System.datarow": string pac = (string)comboBox1.Items[0].ToString(); textBox4.Text = pac;Shophar
you can read like this string pac =comboBox1.Text;Judejudea
but i want to search for a string match through all the elements in the combobox (inside the "for" loop from your first answer)Shophar
oh man, again THANK YOU A LOT for helping me!! i tried in many ways, but didn't knew this had to be done. have a great day! :)Shophar
GetItemText not there any more, use getContent()Ragouzis
T
10

Use a foreach loop. It will iterate all your items of ComboBox regardless of their count, e.g.

foreach(var item in myComboBox.Items)
{
// do something with your item
}
Tumescent answered 26/8, 2013 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.