C# 使用 txt 文件中的值填充 2 个组合框和 1 个文本框,只有 1 个选择

C# populating 2 combo boxes and 1 text box with values from a txt file with only 1 selection

我有一个文本文件,其中有 3 个 "columns" 以逗号分隔,

我可以将它们分别加载到每个组合框或文本框中

我想要做的是 select 只有 nameComboBox 的第一列值,然后其他 2 个框自动填充同一行的值。

此外,如果我可以改进将最后一列放入组合框然后放入文本框的方式。

也在考虑,我可以将 numberComboBoxdescriptionComboBox 都更改为文本框,因为它们不会 select from?

文本文件(notes.txt):

run, 1, runs the file
save, 2, saves the file
delete, 3, deletes the file

当前代码:

    public Main()
    {
        InitializeComponent();
        string[] notes = File.ReadAllLines("C:\notes.txt");
        foreach (var line in notes)
        {
            string[] tokens = line.Split(',');
            nameComboBox.Items.Add(tokens[0]);
            numberComboBox.Items.Add(tokens[1]);
            descriptionComboBox.Items.Add(tokens[2]);
        }

        descriptionComboBox.Text = descriptionTextBox.Text;
    }

例如,如果我从 nameComboBox select run 我希望 numberComboBox 填充 2descriptionComboBox 填充 deletes the file.

更好的是我 select run 来自 nameComboBox 我希望 numberTextBox 填充 2descriptionTextBox填充 deletes the file.

您可以使用 Combobox 的 SelectedIndexChanged 事件:

private void nameComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
          numberComboBox.SelectedIndex = descriptionComboBox.SelectedIndex = nameComboBox.SelectedIndex;
}

您必须事先关联事件处理程序:

this.nameComboBox.SelectedIndexChanged += 
        new System.EventHandler(nameComboBox_SelectedIndexChanged);