Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code in c#. please keep in mind that I am a complete beginner. Review the code Open the InventoryMaintenance project You may need to set

Code in c#. please keep in mind that I am a complete beginner.

Review the code

Open the InventoryMaintenance project

You may need to set the property of the .txt file to "Content" and "Copy Always" or "Copy if newer" in the file's properties window

Display the code for the InvItemList class and note that it contains a readonly property named DisplayText.

Display the code for the Inventory Maintenance form and review the code that loads the items in the combo box and the code that executes when the selected item in the combo box is changed.

Run the application and view the items in the combo box. Notice that nothing happens when you change the selected item. Exit the application.

Add code that sorts and filters the inventory items

Find the FillItemListBox() method in the Inventory Maintenance form. It starts by storing the selected value of the combo box in a variable named filter and declaring a local collection of InvItem objects named filteredItems.

Code an if/else statement that uses LINQ to query the class-level collection named invItems based on the value of the filter variable. The LINQ queries should also order the inventory items by Description. Assign the result of each LINQ query to the filteredItems collection. Note: You can use method-based queries or query expressions here.

Update the foreach statement so it loops through the local filteredItems collection rather than the class-level invItems collection.

Test the application to be sure it displays the items in alphabetical order and filters the items by price when the selected item in the combo box changes.

Add the following new item to the inventory: ItemNo: 4372639 Description: Creeping Phlox Price: 24.99 Notice that the new item is sorted so it appears alphabetically in the display. Use the combo box to show items whose price is between $10 and $50 and see that the new item is displayed. When youre done, exit the application.

Open the InventoryItems.txt file and see that the items arent in alphabetical order, and that the item you just added is the last item in the file

Add code that uses LINQ to find the inventory item to delete

Run the application, select the item you added and click Delete. Note that the confirmation message identifies the wrong item for deletion. Click Cancel and exit the application.

Display the btnDelete_Click() event handler in the Inventory Maintenance form and comment out the line of code that uses the selected index value of the ListView control to get the selected InvItem object from the invItems collection.

Add code that uses that selected index value to retrieve the display text of the selected item from the Items collection of the ListView control.

Use a LINQ query to get the selected InvItem object from the InvItems collection. The LINQ query should select the item whose DisplayText property is equal to the display text value you just retrieved. Note: To do this, you need to use the First() or FirstOrDefault() method. Because of that, you should code this as a method-based query.

Run the application and try to delete the item you added in step 4. This time, the confirm message should present the correct inventory item. Click OK to delete the item and exit the application

=================

Here is what I have ==================

frmInvMaint.cs

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

namespace InventoryMaintenance { public partial class frmInvMaint : Form { public frmInvMaint() { InitializeComponent(); }

private List invItems = null;

private void frmInvMaint_Load(object sender, EventArgs e) { invItems = InvItemDB.GetItems(); LoadComboBox(); FillItemListBox(); }

private void LoadComboBox() { cboFilterBy.DataSource = new string[] { "All", "Under $10", "$10 to $50", "Over $50" }; }

private void FillItemListBox() { lstItems.Items.Clear(); string filter = cboFilterBy.SelectedValue.ToString(); IEnumerable filteredItems = null;

// add items to the filteredItems collection based on FilterBy value // change code to loop the filteredItems collection foreach (InvItem item in invItems) { lstItems.Items.Add(item.DisplayText); } }

private void btnAdd_Click(object sender, EventArgs e) { frmNewItem newItemForm = new frmNewItem(); InvItem invItem = newItemForm.GetNewItem(); if (invItem != null) { invItems.Add(invItem); InvItemDB.SaveItems(invItems); FillItemListBox(); } }

private void btnDelete_Click(object sender, EventArgs e) { int i = lstItems.SelectedIndex; if (i != -1) { InvItem invItem = (InvItem)invItems[i]; string message = $"Are you sure you want to delete {invItem.Description}?"; DialogResult button = MessageBox.Show(message, "Confirm Delete", MessageBoxButtons.YesNo); if (button == DialogResult.Yes) { invItems.Remove(invItem); InvItemDB.SaveItems(invItems); FillItemListBox(); } } }

private void btnExit_Click(object sender, EventArgs e) { this.Close(); }

private void cboFilterBy_SelectedIndexChanged(object sender, EventArgs e) { FillItemListBox(); } } }

frmInvMaint.resx

text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

frmInvMaint.Designer.cs

namespace InventoryMaintenance { partial class frmInvMaint { ///

/// Required designer variable. /// private System.ComponentModel.IContainer components = null;

///

/// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }

#region Windows Form Designer generated code

///

/// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.lstItems = new System.Windows.Forms.ListBox(); this.btnAdd = new System.Windows.Forms.Button(); this.btnDelete = new System.Windows.Forms.Button(); this.btnExit = new System.Windows.Forms.Button(); this.cboFilterBy = new System.Windows.Forms.ComboBox(); this.label2 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // lstItems // this.lstItems.FormattingEnabled = true; this.lstItems.ItemHeight = 15; this.lstItems.Location = new System.Drawing.Point(19, 55); this.lstItems.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.lstItems.Name = "lstItems"; this.lstItems.Size = new System.Drawing.Size(306, 154); this.lstItems.TabIndex = 10; // // btnAdd // this.btnAdd.Location = new System.Drawing.Point(348, 55); this.btnAdd.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.btnAdd.Name = "btnAdd"; this.btnAdd.Size = new System.Drawing.Size(97, 25); this.btnAdd.TabIndex = 3; this.btnAdd.Text = "Add Item"; this.btnAdd.UseVisualStyleBackColor = true; this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click); // // btnDelete // this.btnDelete.Location = new System.Drawing.Point(348, 92); this.btnDelete.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.btnDelete.Name = "btnDelete"; this.btnDelete.Size = new System.Drawing.Size(97, 25); this.btnDelete.TabIndex = 4; this.btnDelete.Text = "Delete Item"; this.btnDelete.UseVisualStyleBackColor = true; this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click); // // btnExit // this.btnExit.Location = new System.Drawing.Point(348, 128); this.btnExit.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.btnExit.Name = "btnExit"; this.btnExit.Size = new System.Drawing.Size(97, 25); this.btnExit.TabIndex = 5; this.btnExit.Text = "E&xit"; this.btnExit.UseVisualStyleBackColor = true; this.btnExit.Click += new System.EventHandler(this.btnExit_Click); // // cboFilterBy // this.cboFilterBy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboFilterBy.FormattingEnabled = true; this.cboFilterBy.Location = new System.Drawing.Point(65, 13); this.cboFilterBy.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.cboFilterBy.Name = "cboFilterBy"; this.cboFilterBy.Size = new System.Drawing.Size(129, 23); this.cboFilterBy.TabIndex = 1; this.cboFilterBy.SelectedIndexChanged += new System.EventHandler(this.cboFilterBy_SelectedIndexChanged); // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(19, 15); this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(39, 15); this.label2.TabIndex = 15; this.label2.Text = "Show:"; // // frmInvMaint // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.CancelButton = this.btnExit; this.ClientSize = new System.Drawing.Size(476, 233); this.Controls.Add(this.label2); this.Controls.Add(this.cboFilterBy); this.Controls.Add(this.btnExit); this.Controls.Add(this.btnDelete); this.Controls.Add(this.btnAdd); this.Controls.Add(this.lstItems); this.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.Name = "frmInvMaint"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Inventory Maintenance"; this.Load += new System.EventHandler(this.frmInvMaint_Load); this.ResumeLayout(false); this.PerformLayout();

}

#endregion

private System.Windows.Forms.ListBox lstItems; private System.Windows.Forms.Button btnAdd; private System.Windows.Forms.Button btnDelete; private System.Windows.Forms.Button btnExit; private System.Windows.Forms.ComboBox cboFilterBy; private System.Windows.Forms.Label label2; } }

frmNewItem.cs

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;

namespace InventoryMaintenance { public partial class frmNewItem : Form { public frmNewItem() { InitializeComponent(); }

private InvItem invItem = null;

public InvItem GetNewItem() { this.ShowDialog(); return invItem; }

private void btnSave_Click(object sender, EventArgs e) { if (IsValidData()) { invItem = new InvItem( Convert.ToInt32(txtItemNo.Text), txtDescription.Text, Convert.ToDecimal(txtPrice.Text) ); this.Close(); } }

private bool IsValidData() { return Validator.IsPresent(txtItemNo) && Validator.IsInt32(txtItemNo) && Validator.IsPresent(txtDescription) && Validator.IsPresent(txtPrice) && Validator.IsDecimal(txtPrice); }

private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } } }

frmNewItem.resx

text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

frmNewItem.Designer.cs

namespace InventoryMaintenance { partial class frmNewItem { ///

/// Required designer variable. /// private System.ComponentModel.IContainer components = null;

///

/// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }

#region Windows Form Designer generated code

///

/// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.txtItemNo = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.txtDescription = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.txtPrice = new System.Windows.Forms.TextBox(); this.btnSave = new System.Windows.Forms.Button(); this.btnCancel = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(18, 16); this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(51, 15); this.label1.TabIndex = 0; this.label1.Text = "Item no:"; // // txtItemNo // this.txtItemNo.Location = new System.Drawing.Point(96, 14); this.txtItemNo.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.txtItemNo.Name = "txtItemNo"; this.txtItemNo.Size = new System.Drawing.Size(106, 23); this.txtItemNo.TabIndex = 1; this.txtItemNo.Tag = "Item no"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(18, 48); this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(70, 15); this.label2.TabIndex = 2; this.label2.Text = "Description:"; // // txtDescription // this.txtDescription.Location = new System.Drawing.Point(96, 46); this.txtDescription.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.txtDescription.Name = "txtDescription"; this.txtDescription.Size = new System.Drawing.Size(190, 23); this.txtDescription.TabIndex = 3; this.txtDescription.Tag = "Description"; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(18, 80); this.label3.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(36, 15); this.label3.TabIndex = 4; this.label3.Text = "Price:"; // // txtPrice // this.txtPrice.Location = new System.Drawing.Point(96, 78); this.txtPrice.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.txtPrice.Name = "txtPrice"; this.txtPrice.Size = new System.Drawing.Size(106, 23); this.txtPrice.TabIndex = 5; this.txtPrice.Tag = "Price"; // // btnSave // this.btnSave.Location = new System.Drawing.Point(96, 119); this.btnSave.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.btnSave.Name = "btnSave"; this.btnSave.Size = new System.Drawing.Size(78, 25); this.btnSave.TabIndex = 6; this.btnSave.Text = "Save"; this.btnSave.UseVisualStyleBackColor = true; this.btnSave.Click += new System.EventHandler(this.btnSave_Click); // // btnCancel // this.btnCancel.Location = new System.Drawing.Point(206, 119); this.btnCancel.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.btnCancel.Name = "btnCancel"; this.btnCancel.Size = new System.Drawing.Size(78, 25); this.btnCancel.TabIndex = 7; this.btnCancel.Text = "Cancel"; this.btnCancel.UseVisualStyleBackColor = true; this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click); // // frmNewItem // this.AcceptButton = this.btnSave; this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.CancelButton = this.btnCancel; this.ClientSize = new System.Drawing.Size(307, 158); this.ControlBox = false; this.Controls.Add(this.btnCancel); this.Controls.Add(this.btnSave); this.Controls.Add(this.txtPrice); this.Controls.Add(this.label3); this.Controls.Add(this.txtDescription); this.Controls.Add(this.label2); this.Controls.Add(this.txtItemNo); this.Controls.Add(this.label1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "frmNewItem"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "New Inventory Item"; this.ResumeLayout(false); this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox txtItemNo; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox txtDescription; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox txtPrice; private System.Windows.Forms.Button btnSave; private System.Windows.Forms.Button btnCancel; } }

InventoryItems.txt

3245649|Agapanthus|7.95 3762592|Limonium|6.95 9210584|Snail pellets|12.95 4738459|Japanese Red Maple|89.95

InvItem.cs

using System; using System.Collections.Generic; using System.Text;

namespace InventoryMaintenance { public class InvItem { public InvItem() { }

public InvItem(int itemNo, string description, decimal price) { ItemNo = itemNo; Description = description; Price = price; }

public int ItemNo { get; set; } public string Description { get; set; } public decimal Price { get; set; }

public string DisplayText => $"{ItemNo} {Description} ({Price:c})"; } }

InvItemDB.cs

using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml;

namespace InventoryMaintenance { public static class InvItemDB { private const string Path = @"..\..\..\InventoryItems.txt"; private const string Separator = "|";

public static List GetItems() { // create the list List items = new List();

// create the object for the input stream for a text file StreamReader textIn = new StreamReader( new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Read));

// read the data from the file and store it in the list while (textIn.Peek() != -1) { string row = textIn.ReadLine(); string[] columns = row.Split(Separator); InvItem item = new InvItem(); item.ItemNo = Convert.ToInt32(columns[0]); item.Description = columns[1]; item.Price = Convert.ToDecimal(columns[2]); items.Add(item); }

// close the input stream for the text file textIn.Close();

return items; }

public static void SaveItems(List items) { // create the output stream for a text file that exists StreamWriter textOut = new StreamWriter( new FileStream(Path, FileMode.Create, FileAccess.Write));

// write each item foreach (InvItem item in items) { textOut.Write(item.ItemNo + Separator); textOut.Write(item.Description + Separator); textOut.WriteLine(item.Price); }

// close the output stream for the text file textOut.Close(); } } }

Program.cs

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms;

namespace InventoryMaintenance { static class Program { ///

/// The main entry point for the application. /// [STAThread] static void Main() { Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmInvMaint()); } } }

Validator.cs

using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms;

namespace InventoryMaintenance { public static class Validator { private static string title = "Entry Error"; public static string Title { get => title; set => title = value; }

public static bool IsPresent(TextBox textBox) { if (textBox.Text == "") { MessageBox.Show(textBox.Tag + " is a required field.", Title); textBox.Focus(); return false; } return true; }

public static bool IsDecimal(TextBox textBox) { decimal number = 0m; if (Decimal.TryParse(textBox.Text, out number)) { return true; } else { MessageBox.Show(textBox.Tag + " must be a decimal value.", Title); textBox.Focus(); return false; } }

public static bool IsInt32(TextBox textBox) { int number = 0; if (Int32.TryParse(textBox.Text, out number)) { return true; } else { MessageBox.Show(textBox.Tag + " must be an integer.", Title); textBox.Focus(); return false; } }

public static bool IsWithinRange(TextBox textBox, decimal min, decimal max) { decimal number = Convert.ToDecimal(textBox.Text); if (number < min || number > max) { MessageBox.Show(textBox.Tag + " must be between " + min + " and " + max + ".", Title); textBox.Focus(); return false; } return true; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions