2017-05-03 85 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]);