简体   繁体   English

在 ComboBox 中选择项目后更改后文本框不更新(ComboBox 从文本文件中获取列表)

[英]Text box does not update after changing after selected item in ComboBox (ComboBox gets the list from a text file)

I am confronting with an issue with the ComboBox in a WinForms app.我在 WinForms 应用程序中遇到了 ComboBox 的问题。 The combo box contains a list with items that are grabbed from a TXT file.组合框包含一个列表,其中包含从 TXT 文件中获取的项目。 To read and update the list I added the following code to Form_Load.为了阅读和更新列表,我将以下代码添加到 Form_Load。

string[] lineOfContents = File.ReadAllLines(Application.StartupPath + @"Items.txt");
        foreach(var line in lineOfContents)
        {
            string[] tokens = line.Split(',');
            ComboBox.Items.Add(tokens[0]);
        }

All nice and good, the list does update.一切都很好,列表确实更新了。 However, I also have some TextBoxes that are changing their text based on the string of the selected item in the ComboBox.但是,我也有一些文本框根据 ComboBox 中所选项目的字符串更改其文本。 For example, when the selected item in the ComboBox is "Example", the text in the first text box should change from empty to "I am an example,".例如,当 ComboBox 中的选中项为“示例”时,第一个文本框中的文本应由空变为“我是示例”。 but it doesn't and remains empty: The code for it is:但它没有并且仍然为空:它的代码是:

if(ComboBox.SelectedItem == "Example")
        {
            TextBox.Text = "I am an example!";
         }

At first I though it's a conversion issue so I tried to use Convert.ToString(tokens[0]);起初我认为这是一个转换问题,所以我尝试使用Convert.ToString(tokens[0]); but it did not work as well.但它也没有奏效。

Any suggestions on what I should try next to fix this issue?关于我接下来应该尝试解决此问题的任何建议?

You are describing bound behavior so the first thing to check is whether the TextBox is properly bound to the ComboBox .您正在描述绑定行为,因此首先要检查的是TextBox是否正确绑定到ComboBox Something like this would work:像这样的东西会起作用:

public MainForm()
{
    InitializeComponent();

    // Attach text box text to changes of combo box text
    textBox1.DataBindings.Add(
        nameof(textBox1.Text),
        comboBox1,
        nameof(comboBox1.Text)
    );
}

But the other thing I notice in your code is that you don't seem to select a value in the combo box after loading the tokens into it.但是我在您的代码中注意到的另一件事是,在将令牌加载到组合框中后,您似乎没有select组合框中的值。 This leaves the text box blank as demonstrated here:这将文本框留空,如下所示:

private void buttonLoad_Click(object sender, EventArgs e)
{
    loadMockFile();
}

private void loadMockFile()
{
    string[] lineOfContents = MockFile;
    foreach (var line in lineOfContents)
    {
        var token = 
            line.Split(',')
            .Select(_ => _.Trim())
            .Distinct()
            .First();
        if (!comboBox1.Items.Contains(token))
        {
            comboBox1.Items.Add(token);
        }
    }
}

string[] MockFile = new string []
{
    "Example, A, B,",
    "Other, C, D,",
};

需要选择


So the solution is to make sure that you make a selection after the tokens are loaded into the ComboBox , for example as shown in the handler for the [Load + Select] button:因此,解决方案是确保在将令牌加载到ComboBox后进行选择,例如 [Load + Select] 按钮的处理程序中所示:

private void buttonLoadPlusSelect_Click(object sender, EventArgs e)
{
    loadMockFile();
    var foundIndex = comboBox1.FindStringExact("Example");
    comboBox1.SelectedIndex = foundIndex;
}

加载后选择

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

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