我有一个文本文件,其中有3个用逗号分隔的“列”,
我可以将它们单独加载到每个组合框或文本框中
我想要做的是只从nameComboBox
中选择第一个列值,然后用同一行的值自动填充其他两个框。
此外,如果我可以改进将最后一列放入组合框中,然后放入文本框中的方式。
再考虑一下,我可以将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;
}
因此,例如,如果我从nameComboBox
中选择run
,我希望用2
填充numberComboBox
,用deletes the file
填充descriptionComboBox
。
Better yetI从nameComboBox
中选择run
,我希望用2
填充numberTextBox
,用descriptionTextBox
填充deletes the file
。
发布于 2019-09-03 09:39:55
您可以使用组合框的SelectedIndexChanged事件:
private void nameComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
numberComboBox.SelectedIndex = descriptionComboBox.SelectedIndex = nameComboBox.SelectedIndex;
}
您必须事先关联eventhandler:
this.nameComboBox.SelectedIndexChanged +=
new System.EventHandler(nameComboBox_SelectedIndexChanged);
https://stackoverflow.com/questions/57768417
复制相似问题