2017-05-03 80 views
2

我很抱歉,如果这个问题之前已经问过,但我非常接近我的头,基本上,当我点击组合框它给了我4个选项来过滤列表框(所有,披萨,汉堡,杂食)披萨,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(); 
    } 
} 

}

+0

做了'串的快速搜索。 Split()函数,它的重载简单修复 – MethodMan

+0

你的foreach在最后有一个问题......你正在迭​​代'DLL'中的项目(每个项目都是'x'),但是在循环中你不断地添加相同的“线路”。你应该添加'x'。 –

回答

0

你可以做到这一点没有明确使用StreamReader使用File类的静态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); 
    } 
} 
+0

Thankyou你的是我已经能够阅读和理解的唯一代码,但是仍然存在问题,你看起来应该可以工作,但不是在列表框中显示每行文本,而是出于某种原因写入ACW2.MainWindow + InventoryItem,你知道这是为什么吗? – Carlos

+0

哦,是的,我认为这是因为我们没有任何默认方式来显示'InventoryItem'的字符串值。我更新了上面的代码示例以添加“ToString”方法的重写。您可以调整它以显示您想要的方式。 –

+0

非常感谢! – Carlos

3

你需要拆分line被读取的分隔符,

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

,那么你可以在指数array[2] & array[3]访问号码。

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