【发布时间】:2015-09-18 15:22:51
【问题描述】:
我的程序的目的是获取一个数据 txt 文件并对其进行编辑,和/或对其进行加减。 文本文件格式是这样的:
Name|Address|Phone|# of people coming|isRSVP
我的代码似乎一直在做它的工作,直到我尝试单击 listbox 中的一个名称,它需要搜索多维数组以提取信息并放置在一组可以编辑的文本框中。问题是我使用的 foreach 循环给了我一个 out of bounds 异常。我尝试进行调试以确保数组中的数据正确并查看过程。一切似乎都正确,但由于某种原因,条件语句 person[0]==listbox1.selectedIndex 没有返回 true,即使两者都与我在进入流程的步骤中看到的相同。任何帮助将不胜感激。
这是我的代码:
StringBuilder newFile = new StringBuilder();
static string txtList= "guest_list.txt";
static string[] file = File.ReadAllLines(txtList);
static int x = file.Count();
string[,] list = new string[x ,5];
public void loadGuestList()
{
int count2 = 0;
foreach (string line in file)
{
string[] sliced = line.Split('|');
int count = 0;
list[count2, count] = sliced[0];
count++;
list[count2, count] = sliced[1];
count++;
list[count2,count] = sliced[2];
count++;
list[count2,count]= sliced[3];
count++;
list[count2, count] = sliced[4];
count++;
listBox1.Items.Add(list[count2,0]);
count2++;
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (string person in list)
{
if ( person[0].ToString()==listBox1.SelectedItem.ToString())
{
addTxt.Text = char.ToString(person[1]);
textPhone.Text = char.ToString(person[2]);
textPeople.Text = char.ToString(person[3]);
if (person[4] == 'n' )
{
}
else
{
chkRSVP.Checked = true;
}
break;
}
}
}
【问题讨论】:
-
您还没有告诉我们异常发生在哪里 - 或者您尝试做些什么来诊断它。如果您可以展示一个简短但完整的程序来演示该问题,那将非常有帮助 - 就像一个控制台应用程序一样。我怀疑当您尝试编写这样的应用程序来隔离问题时,您会发现错误..
-
"statement
person[0]==listbox1.selectedIndexis not return true 即使即使两者相同" - 你说 index,在代码中你是使用选定的项目。这会是个问题吗? -
listBox1.Items[listBox1.SelectedIndex].ToString() ??
标签: c# arrays multidimensional-array foreach textbox