[英]C# populating 2 combo boxes and 1 text box with values from a txt file with only 1 selection
我有一個文本文件,其中有3個用逗號分隔的“列”,
我可以將它們分別加載到每個組合框或文本框中
我想做的是僅從nameComboBox
選擇第一列值,然后自動使用同一行中的值填充其他2個框。
另外,如果我可以改進我將最后一列放入組合框的方式,然后將其放入文本框。
還考慮一下,我可以將numberComboBox
和descriptionComboBox
都更改為文本框,因為它們不會從中選擇?
文本文件( 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;
}
因此,例如,如果我選擇run
從nameComboBox
我想numberComboBox
與填充2
和descriptionComboBox
被填充deletes the file
。
更好的YETI選擇run
從nameComboBox
我想要的numberTextBox
與填充2
和descriptionTextBox
被填充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.