我正在尝试使用流式阅读器进行简单的实时搜索,以读取 txt 文件并在列表视图中搜索并显示结果,问题是我只能搜索 1 个字母,因此搜索“1”将显示结果以 1 开头的所有内容,例如搜索 1 的结果为“123”,但搜索“12”或“123”不会显示相同的结果。用我试过的这段代码更容易解释。
编辑,我正在读取的文本文件具有以下结构: 123;asd;asd;asd;asd;asd;asd <- 行示例
public static string[] testtt(string sökord)
{
StreamReader asd = new StreamReader("film.txt");
string temp;
string[] xd;
while (asd.Peek() >= 0) // if I can read another row (I.E next row isnt empty)
{
temp = asd.ReadLine();
xd = temp.Split(';');
for (int i = 0; i < xd.Length; i++)
{
// this should check if my searchword is equal to any member of "xd"
// but this is where the problem occurs when the input is more than 1
// character, will post error message from debugger below this code.
if (xd[i].Substring(0, sökord.Length).ToLower() == sökord.ToLower())
return xd;
}
}
return null;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
listView1.Items.Clear();
ListViewItem item = new ListViewItem(testtt(textBox1.Text)[0]);
item.SubItems.Add(testtt(textBox1.Text)[1]);
item.SubItems.Add(testtt(textBox1.Text)[2]);
item.SubItems.Add(testtt(textBox1.Text)[3]);
item.SubItems.Add(testtt(textBox1.Text)[4]);
item.SubItems.Add(testtt(textBox1.Text)[5]);
item.SubItems.Add(testtt(textBox1.Text)[6]);
listView1.Items.Add(item);
if (textBox1.Text == "")
listView1.Items.Clear();
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
}
前任
{"Index and length must refer to a location within the string.\r\nParameter name: length"} System.Exception {System.ArgumentOutOfRangeException}