Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用文本文件中的项目填充列表框-库存应用程序_C#_Wpf_Combobox_Listbox_Inventory Management - Fatal编程技术网

C# 用文本文件中的项目填充列表框-库存应用程序

C# 用文本文件中的项目填充列表框-库存应用程序,c#,wpf,combobox,listbox,inventory-management,C#,Wpf,Combobox,Listbox,Inventory Management,因此,我尝试使用文本文件中的项目填充列表框,然后我需要能够使用组合框对列表框项目进行排序,例如,如果我在组合框中选择汉堡,则列表框中应该只有汉堡 到目前为止,我有以下代码: private void categoryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { { using (System.IO.StreamReader sr = new System.IO.StreamR

因此,我尝试使用文本文件中的项目填充列表框,然后我需要能够使用组合框对列表框项目进行排序,例如,如果我在组合框中选择汉堡,则列表框中应该只有汉堡

到目前为止,我有以下代码:

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);
                }
            }
        }
    }
}
private void categoryComboBox\u SelectionChanged(对象发送者,SelectionChangedEventArgs e)
{
{
使用(System.IO.StreamReader sr=new System.IO.StreamReader(@“inventory.txt”))
{
而(!sr.EndOfStream)
{
对于(int i=0;i<22;i++)
{
字符串strlitItem=sr.ReadLine();
如果(!String.IsNullOrEmpty(strlitItem))
listBox.Items.Add(strListItem);
}
}
}
}
}

问题是,它将填充列表框,但如果我单击组合框上的任何内容,它只会添加所有增益项,而我最终会得到两倍的项。

因为在组合框的每个选择更改事件中,您都会向列表框添加项,如果不需要在每个selection changed事件中添加项,则可以将代码移动到构造函数中。如果确实要刷新每次选择中的项目,则更改意味着使用注释中建议的
listBox.items.Clear()
作为
Nino
。简而言之,你能做的最好的事情如下:

public void PopulateList()
{
   listBox.Items.Clear();
   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) && 
                       (categoryComboBox.SelectedItem!=null &&     
                       (strListItem.Contains(categoryComboBox.SelectedItem.ToString())))
                    listBox.Items.Add(strListItem);
                }
            }
        }
 }
public void PopulateList()
{
listBox.Items.Clear();
使用(System.IO.StreamReader sr=new System.IO.StreamReader(@“inventory.txt”))
{
而(!sr.EndOfStream)
{
对于(int i=0;i<22;i++)
{
字符串strlitItem=sr.ReadLine();
如果(!String.IsNullOrEmpty(strlitItem)&&
(categoryComboBox.SelectedItem!=null&&
(strListItem.Contains(categoryComboBox.SelectedItem.ToString()))
listBox.Items.Add(strListItem);
}
}
}
}
现在,您可以在初始化Component()之后在构造函数中调用该方法;如果需要,还可以在
类别通讯盒_SelectionChanged
中调用该方法

关于根据组合框中的selectedItem筛选项目:
您必须检查项目是否包含/startwith/ends(根据您的需要)在将当前项添加到列表框之前,请先将它们添加到列表框。

因为您在组合框的每个选择更改事件中都将项添加到列表框,如果不需要在每个选择更改事件中添加项,则可以将代码移到构造函数中。如果确实要刷新每个选择更改中的项,则意味着se
listBox.Items.Clear()
如注释中建议的
Nino
。简而言之,您可以做的最好的事情如下:

public void PopulateList()
{
   listBox.Items.Clear();
   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) && 
                       (categoryComboBox.SelectedItem!=null &&     
                       (strListItem.Contains(categoryComboBox.SelectedItem.ToString())))
                    listBox.Items.Add(strListItem);
                }
            }
        }
 }
public void PopulateList()
{
listBox.Items.Clear();
使用(System.IO.StreamReader sr=new System.IO.StreamReader(@“inventory.txt”))
{
而(!sr.EndOfStream)
{
对于(int i=0;i<22;i++)
{
字符串strlitItem=sr.ReadLine();
如果(!String.IsNullOrEmpty(strlitItem)&&
(categoryComboBox.SelectedItem!=null&&
(strListItem.Contains(categoryComboBox.SelectedItem.ToString()))
listBox.Items.Add(strListItem);
}
}
}
}
现在,您可以在初始化Component()之后在构造函数中调用该方法;如果需要,还可以在
类别通讯盒_SelectionChanged
中调用该方法

关于根据组合框中的selectedItem筛选项目:
在将项目添加到列表框之前,您必须检查项目是否包含当前项目的/startwith/结尾(根据您的需要)。

添加之前,在方法开始时,清除所有项目。
listBox.items.clear()
谢谢这就像一个字符一样,在添加之前,在方法开始时,清除所有项目。
listBox.items.clear()
谢谢,这就像一个骗子,他说他还需要排序,即项目过滤…@ZainUlAbidin:谢谢,我已经更新了帖子,请看一看,但他说他还需要排序,即项目过滤…@ZainUlAbidin:谢谢,我已经更新了帖子,请看一看