应用程序运行正常,直到我添加了两个数组的代码。我有一些类级别的字符串:
string cFileName = "customer.txt";
string[] cName = new string[0];
string[] cPhone = new string[0];
我已将此添加到Window_Loaded
事件:
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
//read file on start
//ReadFile();
int counter = 0;
string line;
StreamReader custSR = new StreamReader(cFileName);
line = custSR.ReadLine();
while (custSR.Peek() != -1)
{
Array.Resize(ref cPhone, cPhone.Length + 1);
Array.Resize(ref cName, cName.Length + 1);
cPhone[cPhone.Length - 1] = line;
cName[cName.Length - 1] = line;
counter++;
//phoneComboBox.Items.Add(cPhone[cPhone.Length - 1]);
}
custSR.Close();
for (int i = 0; i < counter; i++)
{
phoneComboBox.Items.Add(cPhone[i]);
}
//focus when program starts
phoneComboBox.Focus();
}
我//ReadFile()
的原因是因为我尝试将其作为单独的方法并调用Window_Loaded
事件。我也尝试从循环中输入组合框。我之前没有发生这种情况,也无法弄清楚我做了什么。
答案 0 :(得分:0)
我没有读下一行来打破循环。我将此标记为我的答案,因为它修复了问题,并且任何人在将来偶然遇到这个问题。
while (custSR.Peek() != -1)
{
Array.Resize(ref cPhone, cPhone.Length + 1);
Array.Resize(ref cName, cName.Length + 1);
cPhone[cPhone.Length - 1] = line;
cName[cName.Length - 1] = line;
counter++;
line = custSR.ReadLine(); //needed this to read next line
}