簡體   English   中英

C#使用僅1個選擇的txt文件中的值填充2個組合框和1個文本框

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

我有一個文本文件,其中有3個用逗號分隔的“列”,

我可以將它們分別加載到每個組合框或文本框中

我想做的是僅從nameComboBox選擇第一列值,然后自動使用同一行中的值填充其他2個框。

另外,如果我可以改進我將最后一列放入組合框的方式,然后將其放入文本框。

還考慮一下,我可以將numberComboBoxdescriptionComboBox都更改為文本框,因為它們不會從中選擇?

文本文件( 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;
    }

因此,例如,如果我選擇runnameComboBox我想numberComboBox與填充2descriptionComboBox被填充deletes the file

更好的YETI選擇runnameComboBox我想要的numberTextBox與填充2descriptionTextBox被填充deletes the file

您可以使用組合框的SelectedIndexChanged事件:

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

您必須事先關聯事件處理程序:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM