jeyejow
jeyejow

Reputation: 429

C# program doesnt change combo box text

I tell the program to change the Combo box text to change to a string i have, and it doesnt change the text. Here is the part of the code:

int i = 0;
bool found = false;
do
{
    if (Globais.loc_txt[i] == (items[0] + " ") || Globais.loc_txt[i] == (items[0]))
    {
        cb_loc.Text = Globais.loc_txt[i]; // ele encontra bem, mas agora nao está a mudar o valor de text na cb
        break;
    }

    else { i++; }

} while (!found && i <= Globais.loc.Length);

Regardless, thanks.

PS: cb_loc is the name of my combo box and Globais.loc_txt[i] is a string array and it has a string in the i position.

enter image description here

Thats my combo box settings or whatever you call them.

Upvotes: 0

Views: 88

Answers (1)

Scrobi
Scrobi

Reputation: 1215

According to the msdn:

Setting the Text property to null or an empty string ("") sets the SelectedIndex to -1. Setting the Text property to a value that is in the Items collection sets the SelectedIndex to the index of that item. Setting the Text property to a value that is not in the collection leaves the SelectedIndex unchanged.

Is the text you are setting the combobox.Text to not an item in the combobox? If it is a new string trying adding the item to the collection first.

Your if logic just seems to be comparing a strings and checking it with trailing space. 1, I am presuming both are not in your combobox.items. 2, Could you just Trim the Globais.loc_txt[i]

int i = 0;
bool found = false;
do
{
    string text = Globais.loc_txt[i].TrimEnd()
    if (text == item[0])
    {
        cb_loc.Text = text; // ele encontra bem, mas agora nao está a mudar o valor de text na cb
        break;
    }

    else { i++; }

} while (!found && i <= Globais.loc.Length);

Upvotes: 1

Related Questions