【发布时间】:2016-06-13 16:22:24
【问题描述】:
我从一个文本文件创建了两个字符串数组,并用 array1 填充了一个组合框。我想了解的是,如何获得一个文本框来显示与组合框(array1)的选定索引匹配的array2的索引?
我认为这样的事情可能会奏效:
if(phoneComboBox.Text == cPhone[index])
{
nameTextBox.Text = cName[index]; //show equal index item to cPhone/phoneComboBox
}
但这似乎不起作用。我也尝试过 foreach 循环,也许我做错了。我在 window_loaded 事件中读取了文本文件和数组,但不知道这是否是问题所在。我已经看到 SelectedIndexChanged 事件在类似问题中提到了很多,但我没有该事件可以使用,只有 SelectionChanged。
有人可以指出我的正确方向吗?我知道数组在这里可能不是最好的用途,但它们是我用过的,所以请帮助我正确理解它。
这就是我读取数组的方式:
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
//read file on start
int counter = 0;
string line;
StreamReader custSR = new StreamReader(cFileName);
line = custSR.ReadLine();
while (line != null)
{
Array.Resize(ref cPhone, cPhone.Length + 1);
cPhone[cPhone.Length - 1] = line;
counter++;
line = custSR.ReadLine();
Array.Resize(ref cName, cName.Length + 1);
cName[cName.Length - 1] = line;
counter++;
line = custSR.ReadLine();
phoneComboBox.Items.Add(cPhone[cPhone.Length - 1]);
}
custSR.Close();
/*string changeAll = string.Join(Environment.NewLine, cPhone);
string allOthers = string.Join(Environment.NewLine, cName);
MessageBox.Show(changeAll + allOthers);*/
//focus when program starts
phoneComboBox.Focus();
}
【问题讨论】:
-
显示代码你是如何填充数组的?