如何使用组合框过滤我的列表框项?

时间:2017-05-03 15:30:07

标签: c# combobox listbox

如果之前已经问过这个问题,我很抱歉,但是我非常接近这个问题,基本上,当我点击组合框时,它给了我4个选项来过滤列表框(All,Pizza,Burger,Sundry) Pizza,Burger和Sundry是类别名称中的单词。如何制作它,以便我的列表框仅显示组合框中选择的内容。

class InventoryItem
{

        public string CategoryName { get; set; }
        public string FoodName { get; set; }
        public double Cost { get; set; }
        public double Quantity { get; set; }

       public override string ToString()

    {
        return $"{CategoryName} - {FoodName}. Cost: {Cost:0.00}, Quantity: {Quantity}";

    }


}

public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
    }


    private void inventoryButton_Click(object sender, RoutedEventArgs e )
    {
        InventoryWindow wnd = new InventoryWindow();

        //Var filepath allows the code to access the Invenotry.txt from the bin without direclty using a streamreader in the code 
        var filePath = "inventory.txt";

        if (File.Exists(filePath))
        {
            // ReadAllLines method can read all the lines from the inventory.text file and then returns them in an array, in this case the InventoryItem
            var fileContents = File.ReadAllLines(filePath);
            foreach (var inventoryLine in fileContents)
            {

                // This makes sure our line has some content with a true or false boolean value, hence continue simply allows the code to continue past the if statment
                if (string.IsNullOrWhiteSpace(inventoryLine)) continue;

                //We can now Split a line of text in the inventory txt into segments with a comma thanks to the inventoryLine.Split
                var inventoryLineSeg = inventoryLine.Split(',');
                var inventoryItem = new InventoryItem();

                // if the code was succesful in trying to parse the text file these will hold the value of cost and quantity
                double cost;
                double quantity;

                // Assign each part of the line to a property of the InventoryItem
                inventoryItem.CategoryName = inventoryLineSeg[0];
                if (inventoryLineSeg.Length > 1)
                {
                    inventoryItem.FoodName = inventoryLineSeg[1];
                }
                if (inventoryLineSeg.Length > 2 & double.TryParse(inventoryLineSeg[2], out cost))
                {
                    inventoryItem.Cost = cost;
                }
                if (inventoryLineSeg.Length > 3 & double.TryParse(inventoryLineSeg[3], out quantity))
                {
                    inventoryItem.Quantity = quantity;
                }


                //Now able to add all the InventoryItem to our ListBox
                wnd.ListBox.Items.Add(inventoryItem);
            }
            wnd.ShowDialog();

        }
    }       
    private void foodMenuButton_Click(object sender, RoutedEventArgs e)
    {
        FoodMenuWindow wnd = new FoodMenuWindow();
        wnd.ShowDialog();
    }

    private void newOrderButton_Click(object sender, RoutedEventArgs e)
    {
        OrderWindow wnd = new OrderWindow();
        wnd.ShowDialog();
    }

    private void completedOrdersButton_Click(object sender, RoutedEventArgs e)
    {
        CompletedOrdersWindow wnd = new CompletedOrdersWindow();
        wnd.ShowDialog();
    }

    private void quitButton_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
}

}

2 个答案:

答案 0 :(得分:3)

您需要在分隔符line上拆分正在读取的,

var array = line.Split(',');

然后您可以访问索引为array[2]&的数字。 array[3]

double firstNumber = double.Parse(array[2]);
double secondNumber = double.Parse(array[3]);

答案 1 :(得分:0)

您可以在未使用StreamReader类'明确使用File的情况下执行此操作。 static ReadAllLines方法,它从文本文件中读取所有行并将它们返回到数组中。

然后,对于每一行,您可以使用string类'静态Split方法,它将在一个或多个字符上拆分行(我们将在您的情况下使用逗号)并返回数组中的项目。

接下来,我们可以根据分割线的内容生成新的InventoryItem。我喜欢每次测试数组的长度,所以如果有一行缺少一些逗号(换句话说,不要尝试访问索引),我们就不会遇到异常在数组中,除非你知道它存在)。

最后,我们可以将新的InventoryItem添加到ListBox

注意:这假设您有一个InventoryItem类,如:

class InventoryItem
{
    public string CategoryName { get; set; }
    public string FoodName { get; set; }
    public double Cost { get; set; }
    public double Quantity { get; set; }
    public override string ToString()
    {
        return $"{CategoryName} ({FoodName}) - Price: ${Cost:0.00}, Qty: {Quantity}";
    }
}

然后我们可以解析文件并更新我们的ListBox,如下所示:

// Path to our inventory file
var filePath = @"f:\public\temp\inventory.txt";

if (File.Exists(filePath))
{
    var fileContents = File.ReadAllLines(filePath);

    foreach (var inventoryLine in fileContents)
    {
        // Ensure our line has some content
        if (string.IsNullOrWhiteSpace(inventoryLine)) continue;

        // Split the line on the comma character
        var inventoryLineParts = inventoryLine.Split(',');
        var inventoryItem = new InventoryItem();

        // These will hold the values of Cost and Quantity if `double.TryParse` succeeds
        double cost;
        double qty;

        // Assign each part of the line to a property of the InventoryItem
        inventoryItem.CategoryName = inventoryLineParts[0].Trim();
        if (inventoryLineParts.Length > 1)
        {
            inventoryItem.FoodName = inventoryLineParts[1].Trim();
        }
        if (inventoryLineParts.Length > 2 && 
            double.TryParse(inventoryLineParts[2], out cost))
        {
            inventoryItem.Cost = cost;
        }
        if (inventoryLineParts.Length > 3 && 
            double.TryParse(inventoryLineParts[3], out qty))
        {
            inventoryItem.Quantity = qty;
        }

        // Add this InventoryItem to our ListBox
        wnd.ListBox.Items.Add(inventoryItem);
    }
}