Reputation: 23
I apologize if this question has been asked before but I am so close to getting my head around this, essentially, when I click the combobox it gives me 4 options to filter the list box (All, Pizza, Burger, Sundry) Pizza, Burger, and Sundry are words that are in the category name. How do I make it so my listbox displays only what is selected in the combobox.
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();
}
}
}
Upvotes: 2
Views: 189
Reputation: 37080
You can do this without explicitly using a StreamReader
by using the File
class' static ReadAllLines
method, which reads all the lines from a text file and returns them in an array.
Then, for each line, you can use the string
class' static Split
method, which will split the line on one or more characters (we'll use a comma in your case) and returns the items in an array.
Next, we can generate a new InventoryItem
based on the contents of the split lines. I like to test the length of the array each time, just so we don't hit an exception if there's a line that is missing some commas (in other words, don't try to access an index in the array unless you know it exists).
Finally, we can add our new InventoryItem
to our ListBox
.
Note: this assumes you have an InventoryItem
class like:
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}";
}
}
Then we can parse the file and update our ListBox
like so:
// 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);
}
}
Upvotes: 0
Reputation: 56499
you'll need to split the line
being read on the delimiter ,
.
var array = line.Split(',');
then you can access the numbers at index array[2]
& array[3]
.
double firstNumber = double.Parse(array[2]);
double secondNumber = double.Parse(array[3]);
Upvotes: 3