![](/img/trans.png)
[英]C# Reading from multiple text files, splitting lines into a List, and then loading into ListBox
[英]Reading from multiple Text Files , arrays show on listbox and lables
嗨,我有一個程序:
1- 用戶應首先從ComboBox
中選擇一個項目。
選擇后,會在后台打開一個文本文件,並將其內容添加到ListBox
中。
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox2.SelectedIndex)
{
case 0:
listBox3.Items.Clear();
FileInfo file0 = new FileInfo("C:\\hardwaremaintenance.txt");
StreamReader stRead0 =file0.OpenText();
while (!stRead0.EndOfStream)
{
listBox3.Items.Add(stRead0.ReadLine());
}
break;
case 1:
listBox3.Items.Clear();
FileInfo file1 = new FileInfo("C:\\NetworkManagement.txt");
StreamReader stRead1 =file1.OpenText();
while (!stRead1.EndOfStream)
{
listBox3.Items.Add(stRead1.ReadLine());
}
break;
case 2:
listBox3.Items.Clear();
FileInfo file2 = new FileInfo("C:\\Software.txt");
StreamReader stRead2 =file2.OpenText();
while (!stRead2.EndOfStream)
{
listBox3.Items.Add(stRead2.ReadLine());
}
break;
case 3:
listBox3.Items.Clear();
FileInfo file3 = new FileInfo("C:\\SyriatelApplications.txt");
StreamReader stRead3 =file3.OpenText();
while (!stRead3.EndOfStream)
{
listBox3.Items.Add(stRead3.ReadLine());
}
break;
case 4:
listBox3.Items.Clear();
FileInfo file4 = new FileInfo("C:\\NewHardwareRequest.txt");
StreamReader stRead4 =file4.OpenText();
while (!stRead4.EndOfStream)
{
listBox3.Items.Add(stRead4.ReadLine());
}
break;
}
}
2 用戶 select 來自(最近)在列表框中添加的項目(從步驟 1 開始)中的一個項目,並且在此操作后,它再次打開一個新的文本文件,其中填充了這種格式的文本,其中“|”是分離符號
private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
{
int itemsCount = listBox3.Items.Count;
string[] items = new string[itemsCount];
for (int i = 0; i < itemsCount; i++)
items[i] = listBox3.Items[i].ToString();
我的大腦突然卡在這里。
下一步應該獲取ListBox
中的每個項目並將其與最后打開的文件中的行連接,其中項目選擇 == 任何行中的第一個單詞。
我不知道該怎么做是:
ListBox
中的選定元素與第二個文件中任何行中的第一個單詞進行比較。如果它們匹配,我想使用行中的剩余信息來填充標簽和文本框。
程序界面如下
如果讓您感到困惑,我真的很抱歉,但我在編程方面沒有那么經驗
一些幫助您入門的提示:
string[] items
添加到您的 class。switch()
中有重復代碼時,請使用Refactor|Extract 方法System.IO.File.ReadAllLines(fileName)
可以更好、更輕松地讀取文本文件str=listbox3.SelectedItem
)時,請使用lines[i].Contains(str)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.