所以我想用一個文本文件中的項來填充列表框,然後我需要能夠使用組合框對列表框項進行排序,例如,如果我選擇漢堡在組合框上只有漢堡包應該在列表框中。從文本文件中填充列表框 - 庫存應用程序
到目前爲止,我有這樣的代碼:
private void categoryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(@"inventory.txt"))
{
while (!sr.EndOfStream)
{
for (int i = 0; i < 22; i++)
{
string strListItem = sr.ReadLine();
if (!String.IsNullOrEmpty(strListItem))
listBox.Items.Add(strListItem);
}
}
}
}
}
問題是,它將填充列表框,但如果我點擊下拉框任何東西它只是增加了對收益的所有項目和我結束了兩倍之多項目。
添加之前,在方法開始之前清除所有項目。 'listBox.Items.Clear()' – Nino
感謝像魅力 –