简体   繁体   English

C#程序不会更改组合框文本

[英]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. PS:cb_loc是我的组合框的名称,Globais.loc_txt [i]是一个字符串数组,并且在i位置有一个字符串。

在此处输入图片说明

Thats my combo box settings or whatever you call them. 那就是我的组合框设置或您所说的任何设置。

According to the msdn : 根据msdn

Setting the Text property to null or an empty string ("") sets the SelectedIndex to -1. 将Text属性设置为null或空字符串(“”)会将SelectedIndex设置为-1。 Setting the Text property to a value that is in the Items collection sets the SelectedIndex to the index of that item. 将Text属性设置为Items集合中的值会将SelectedIndex设置为该项目的索引。 Setting the Text property to a value that is not in the collection leaves the SelectedIndex unchanged. 将Text属性设置为不在集合中的值将保持SelectedIndex不变。

Is the text you are setting the combobox.Text to not an item in the combobox? 您正在设置combobox.Text的文本不是组合框中的项目吗? 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. 您的if逻辑似乎只是在比较字符串并将其与尾随空格进行比较。 1, I am presuming both are not in your combobox.items. 1,我想两者都不在您的combobox.items中。 2, Could you just Trim the Globais.loc_txt[i] 2,您能否只修剪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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM