Question

Visual Basic 2015: Extra 18-1 Use inheritance with the Inventory Maintenance Application

Extra 18-1 Use inheritance with the Inventory Maintenance application In this exercise, you11 add two classes to the Invento

Source Code:

1. frmNewItem.vb

Public Class frmNewItem

Public InvItem As InvItem

Private Sub frmNewItem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.LoadComboBox()
End Sub

Private Sub LoadComboBox()
cboSizeOrManufacturer.Items.Clear()
If rdoPlant.Checked Then
cboSizeOrManufacturer.Items.Add("1 gallon")
cboSizeOrManufacturer.Items.Add("5 gallon")
cboSizeOrManufacturer.Items.Add("15 gallon")
cboSizeOrManufacturer.Items.Add("24-inch box")
cboSizeOrManufacturer.Items.Add("36-inch box")
Else
cboSizeOrManufacturer.Items.Add("Bayer")
cboSizeOrManufacturer.Items.Add("Jobe's")
cboSizeOrManufacturer.Items.Add("Ortho")
cboSizeOrManufacturer.Items.Add("Roundup")
cboSizeOrManufacturer.Items.Add("Scotts")
End If
End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
If IsValidData() Then
InvItem = New InvItem(CInt(txtItemNo.Text),
txtDescription.Text, CDec(txtPrice.Text))
Me.Close()
End If
End Sub

Private Function IsValidData() As Boolean
Return Validator.IsPresent(txtItemNo) AndAlso
Validator.IsInt32(txtItemNo) AndAlso
Validator.IsPresent(txtDescription) AndAlso
Validator.IsPresent(txtPrice) AndAlso
Validator.IsDecimal(txtPrice)
End Function

Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.Close()
End Sub

Private Sub rdoPlant_CheckedChanged(sender As Object, e As EventArgs) Handles rdoPlant.CheckedChanged
If rdoPlant.Checked Then
lblSizeOrManufacturer.Text = "Size:"
Else
lblSizeOrManufacturer.Text = "Manufacturer:"
End If
Me.LoadComboBox()
txtItemNo.Select()
End Sub
End Class

2. InvItem.vb

Public Class InvItem
Private m_ItemNo As Integer
Private m_Description As String
Private m_Price As Decimal

Public Sub New()

End Sub

Public Sub New(itemNo As Integer, description As String, price As Decimal)
Me.ItemNo = itemNo
Me.Description = description
Me.Price = price
End Sub

Public Property ItemNo As Integer
Get
Return m_ItemNo
End Get
Set(value As Integer)
m_ItemNo = value
End Set
End Property

Public Property Description As String
Get
Return m_Description
End Get
Set(value As String)
m_Description = value
End Set
End Property

Public Property Price As Decimal
Get
Return m_Price
End Get
Set(value As Decimal)
m_Price = value
End Set
End Property

Public Function GetDisplayText() As String
Return ItemNo & " " & Description & " (" & FormatCurrency(Price) & ")"
End Function
End Class

3. InvItemDB.vb

Imports System.Xml

Public Class InvItemDB
Private Const Path As String = "..\..\InventoryItems.xml"

Public Shared Function GetItems() As List(Of InvItem)
Dim items As New List(Of InvItem)

Dim settings As New XmlReaderSettings
settings.IgnoreWhitespace = True
settings.IgnoreComments = True

Dim xmlIn As XmlReader = XmlReader.Create(Path, settings)

If xmlIn.ReadToDescendant("Item") Then
Do
Dim item As New InvItem
xmlIn.ReadStartElement("Item")
Dim type As String = xmlIn.ReadElementContentAsString
If type = "Plant" Then
item = ReadPlant(xmlIn)
ElseIf type = "Supply" Then
item = ReadSupply(xmlIn)
End If
items.Add(item)
Loop While xmlIn.ReadToNextSibling("Item")
End If

xmlIn.Close()

Return items
End Function

Private Shared Function ReadPlant(xmlIn As XmlReader) As Plant
Dim p As New Plant
ReadBase(xmlIn, p)
p.Size = xmlIn.ReadElementContentAsString
Return p
End Function

Private Shared Sub ReadBase(xmlIn As XmlReader, i As InvItem)
i.ItemNo = xmlIn.ReadElementContentAsInt
i.Description = xmlIn.ReadElementContentAsString
i.Price = xmlIn.ReadElementContentAsDecimal
End Sub

Private Shared Function ReadSupply(xmlIn As XmlReader) As Supply
Dim s As New Supply
ReadBase(xmlIn, s)
s.Manufacturer = xmlIn.ReadElementContentAsString
Return s
End Function

Public Shared Sub SaveItems(items As List(Of InvItem))
Dim settings As New XmlWriterSettings
settings.Indent = True
settings.IndentChars = (" ")

Dim xmlOut As XmlWriter = XmlWriter.Create(Path, settings)

xmlOut.WriteStartDocument()
xmlOut.WriteStartElement("Items")

Dim item As InvItem
For i As Integer = 0 To items.Count - 1
item = CType(items(i), InvItem)
If item.GetType.Name = "Plant" Then
WritePlant(xmlOut, CType(item, Plant))
ElseIf item.GetType.Name = "Supply" Then
WriteSupply(xmlOut, CType(item, Supply))
End If
Next

xmlOut.WriteEndElement()

xmlOut.Close()
End Sub

Private Shared Sub WritePlant(xmlOut As XmlWriter, p As Plant)
xmlOut.WriteStartElement("Item")
xmlOut.WriteElementString("Type", "Plant")
WriteBase(xmlOut, p)
xmlOut.WriteElementString("Size", p.Size)
xmlOut.WriteEndElement()
End Sub

Private Shared Sub WriteBase(xmlOut As XmlWriter, i As InvItem)
xmlOut.WriteElementString("ItemNo", i.ItemNo.ToString)
xmlOut.WriteElementString("Description", i.Description)
xmlOut.WriteElementString("Price", i.Price.ToString)
End Sub

Private Shared Sub WriteSupply(xmlOut As XmlWriter, s As Supply)
xmlOut.WriteStartElement("Item")
xmlOut.WriteElementString("Type", "Supply")
WriteBase(xmlOut, s)
xmlOut.WriteElementString("Manufacturer", s.Manufacturer)
xmlOut.WriteEndElement()
End Sub
End Class

4. InvItemList.vb

Public Class InvItemList
Private invItems As List(Of InvItem)

Public Event Changed(itemList As InvItemList)

Public Sub New()
invItems = New List(Of InvItem)
End Sub

Public ReadOnly Property Count As Integer
Get
Return invItems.Count
End Get
End Property

Default Public Property Item(index As Integer) As InvItem
Get
If index < 0 OrElse index >= invItems.Count Then
Throw New ArgumentOutOfRangeException(index.ToString)
Else
Return invItems(index)
End If
End Get
Set(value As InvItem)
invItems(index) = value
RaiseEvent Changed(Me)
End Set
End Property

Public Sub Add(invItem As InvItem)
invItems.Add(invItem)
RaiseEvent Changed(Me)
End Sub

Public Sub Add(itemNo As Integer, description As String, price As Decimal)
Dim item As New InvItem(itemNo, description, price)
invItems.Add(item)
RaiseEvent Changed(Me)
End Sub

Public Sub Remove(invItem As InvItem)
invItems.Remove(invItem)
RaiseEvent Changed(Me)
End Sub

Public Shared Operator +(il As InvItemList, i As InvItem) As InvItemList
il.Add(i)
Return il
End Operator

Public Shared Operator -(il As InvItemList, i As InvItem) As InvItemList
il.Remove(i)
Return il
End Operator

Public Sub Fill()
invItems = InvItemDB.GetItems
End Sub

Public Sub Save()
InvItemDB.SaveItems(invItems)
End Sub

End Class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE :

1.Program.cs


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

namespace InventoryMaintenance
{
    static class Program
    {
        ///< summary>
        /// The main entry point for the application.
        ///< /summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmInvMaint());
        }
    }
}

2.frmNewItem.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 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();
        }
    }
}

3.frmNewItem.designer.cs

namespace InventoryMaintenance
{
    partial class frmNewItem
    {
        ///< summary>
        /// Required designer variable.
        ///< /summary>
        private System.ComponentModel.IContainer components = null;

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

        #region Windows Form Designer generated code

        ///< summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        ///< /summary>
        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.btnSave = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.txtPrice = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(15, 17);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(45, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Item no:";
            //
            // txtItemNo
            //
            this.txtItemNo.Location = new System.Drawing.Point(84, 17);
            this.txtItemNo.Name = "txtItemNo";
            this.txtItemNo.Size = new System.Drawing.Size(76, 20);
            this.txtItemNo.TabIndex = 1;
            this.txtItemNo.Tag = "Item no";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(15, 47);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(63, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "Description:";
            //
            // txtDescription
            //
            this.txtDescription.Location = new System.Drawing.Point(84, 43);
            this.txtDescription.Name = "txtDescription";
            this.txtDescription.Size = new System.Drawing.Size(200, 20);
            this.txtDescription.TabIndex = 3;
            this.txtDescription.Tag = "Description";
            //
            // btnSave
            //
            this.btnSave.Location = new System.Drawing.Point(84, 107);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(75, 23);
            this.btnSave.TabIndex = 6;
            this.btnSave.Text = "Save";
            this.btnSave.UseVisualStyleBackColor = true;
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            //
            // btnCancel
            //
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Location = new System.Drawing.Point(209, 107);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 7;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(15, 73);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(34, 13);
            this.label3.TabIndex = 4;
            this.label3.Text = "Price:";
            //
            // txtPrice
            //
            this.txtPrice.Location = new System.Drawing.Point(85, 70);
            this.txtPrice.Name = "txtPrice";
            this.txtPrice.Size = new System.Drawing.Size(75, 20);
            this.txtPrice.TabIndex = 5;
            this.txtPrice.Tag = "Price";
            //
            // frmNewItem
            //
            this.AcceptButton = this.btnSave;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.btnCancel;
            this.ClientSize = new System.Drawing.Size(311, 150);
            this.ControlBox = false;
            this.Controls.Add(this.txtPrice);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnSave);
            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.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.Button btnSave;
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox txtPrice;
    }
}

4.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 InvItemList invItems = new InvItemList();

       private void frmInvMaint_Load(object sender, System.EventArgs e)
       {
           invItems.Fill();
           FillItemListBox();
       }

       private void FillItemListBox()
       {
            InvItem item;
           lstItems.Items.Clear();
            for (int i = 0; i < invItems.Count; i++)
            {
                item = invItems.GetItemByIndex(i);
                lstItems.Items.Add(item.GetDisplayText());
            }
       }

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

       private void btnDelete_Click(object sender, System.EventArgs e)
       {
           int i = lstItems.SelectedIndex;
           if (i != -1)
           {
               InvItem invItem = invItems.GetItemByIndex(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);
                   invItems.Save();
                   FillItemListBox();
               }
           }
       }

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

5.frmInvMaint.designer.cs

namespace InventoryMaintenance
{
   partial class frmInvMaint
   {
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ComponentModel.IContainer components = null;

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

       #region Windows Form Designer generated code

       /// <summary>
       /// Required method for Designer support - do not modify
       /// the contents of this method with the code editor.
       /// </summary>
       private void InitializeComponent()
       {
            this.lstItems = new System.Windows.Forms.ListBox();
            this.btnExit = new System.Windows.Forms.Button();
            this.btnDelete = new System.Windows.Forms.Button();
            this.btnAdd = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // lstItems
            //
            this.lstItems.FormattingEnabled = true;
            this.lstItems.Location = new System.Drawing.Point(12, 12);
            this.lstItems.Name = "lstItems";
            this.lstItems.Size = new System.Drawing.Size(325, 160);
            this.lstItems.TabIndex = 10;
            //
            // btnExit
            //
            this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnExit.Location = new System.Drawing.Point(353, 72);
            this.btnExit.Name = "btnExit";
            this.btnExit.Size = new System.Drawing.Size(104, 24);
            this.btnExit.TabIndex = 9;
            this.btnExit.Text = "E&xit";
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            //
            // btnDelete
            //
            this.btnDelete.Location = new System.Drawing.Point(353, 42);
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Size = new System.Drawing.Size(104, 24);
            this.btnDelete.TabIndex = 8;
            this.btnDelete.Text = "Delete Item...";
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            //
            // btnAdd
            //
            this.btnAdd.Location = new System.Drawing.Point(353, 12);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(104, 24);
            this.btnAdd.TabIndex = 7;
            this.btnAdd.Text = "Add Item...";
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            //
            // frmInvMaint
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.btnExit;
            this.ClientSize = new System.Drawing.Size(475, 181);
            this.Controls.Add(this.lstItems);
            this.Controls.Add(this.btnExit);
            this.Controls.Add(this.btnDelete);
            this.Controls.Add(this.btnAdd);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            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);

       }

       #endregion

       private System.Windows.Forms.ListBox lstItems;
       private System.Windows.Forms.Button btnExit;
       private System.Windows.Forms.Button btnDelete;
       private System.Windows.Forms.Button btnAdd;
   }
}

6.InvItemList.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InventoryMaintenance
{
    public class InvItemList
    {
        private List<InvItem> invItems;

        public InvItemList()
        {
            invItems = new List<InvItem>();
        }

        public int Count
        {
            get
            {
                return invItems.Count;
            }
        }

        public InvItem GetItemByIndex(int i)
        {
            return invItems[i];
        }

        public void Add(InvItem invItem)
        {
            invItems.Add(invItem);
        }

        public void Add(int itemNo, string description, decimal price)
        {
            InvItem i = new InvItem(itemNo, description, price);
            invItems.Add(i);
        }

        public void Remove(InvItem invItem)
        {
            invItems.Remove(invItem);
        }

        public void Fill()
        {
            invItems = InvItemDB.GetItems();
        }

        public void Save()
        {
            InvItemDB.SaveItems(invItems);
        }
    }
}

7.InvItemDB.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

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

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

            // create the XmlReaderSettings object
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreWhitespace = true;
            settings.IgnoreComments = true;

            // create the XmlReader object
            XmlReader xmlIn = XmlReader.Create(Path, settings);

            // read past all nodes to the first Book node
            if (xmlIn.ReadToDescendant("Item"))
            {
                // create one Product object for each Product node
                do
                {
                    InvItem item = new InvItem();
                    xmlIn.ReadStartElement("Item");
                    item.ItemNo = xmlIn.ReadElementContentAsInt();
                    item.Description = xmlIn.ReadElementContentAsString();
                    item.Price = xmlIn.ReadElementContentAsDecimal();
                    items.Add(item);
                }
                while (xmlIn.ReadToNextSibling("Item"));
            }
          
            // close the XmlReader object
            xmlIn.Close();

            return items;
        }

        public static void SaveItems(List<InvItem> items)
        {
            // create the XmlWriterSettings object
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = ("    ");

            // create the XmlWriter object
            XmlWriter xmlOut = XmlWriter.Create(Path, settings);

            // write the start of the document
            xmlOut.WriteStartDocument();
            xmlOut.WriteStartElement("Items");

            // write each product object to the xml file
            foreach (InvItem item in items)
            {
                xmlOut.WriteStartElement("Item");
                xmlOut.WriteElementString("ItemNo", Convert.ToString(item.ItemNo));
                xmlOut.WriteElementString("Description", item.Description);
                xmlOut.WriteElementString("Price", Convert.ToString(item.Price));
                xmlOut.WriteEndElement();
            }

            // write the end tag for the root element
            xmlOut.WriteEndElement();

            // close the xmlWriter object
            xmlOut.Close();
        }
    }
}
----------------------------------------------
8.InvItem.cs
--------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InventoryMaintenance
{
    public class InvItem
    {
        private int itemNo;
        private string description;
        private decimal price;

        public InvItem()
        {
        }

        public InvItem(int itemNo, string description, decimal price)
        {
            this.itemNo = itemNo;
            this.description = description;
            this.price = price;
        }

        public int ItemNo
        {
            get
            {
                return itemNo;
            }
            set
            {
                itemNo = value;
            }
        }

        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }

        public decimal Price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }

        public string GetDisplayText()
        {
            return itemNo + "    " + description + " (" + price.ToString("c") + ")";
        }
    }
}

9.Validator.cs

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

namespace InventoryMaintenance
{
    public static class Validator
   {
       private static string title = "Entry Error";

       public static string Title
       {
           get
           {
               return 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;
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
Visual Basic 2015: Extra 18-1 Use inheritance with the Inventory Maintenance Application Source Code: 1. frmNewItem.vb...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • As an example of code reuse take the rectangle.vb class and reuse it in two applications that dea...

    as an example of code reuse take the rectangle.vb class and reuse it in two applications that deal with 1 paint one gallon of paint will cover 400 square feet it costs $15 a gallon 2 flooring prices per square foot are: $1.59 for vinyl $5.79 for high-end hardwood $2.29 for bamboo these are 2 projects having trouble with 'Name: Rectangle.vb ' Programmer: <Hello> on <3/9/2030> Option Explicit On Option Strict On Option Infer Off Public Class Rectangle Private intLength...

  • The Murach's Visual Basic 2015 Book And Visual Studio Exercise 7-2 Enhance the Invoice Total application...

    The Murach's Visual Basic 2015 Book And Visual Studio Exercise 7-2 Enhance the Invoice Total application If you did exercise 6-1 in the last chapter, this exercise asks you to add data validation and exception handling to it. 1. Copy the Invoice Total application from your C:\VB 2015\Chapter 06 directory to your Chapter 07 directory. This should be your solution to exercise 6-1 of the last chapter. 2. If your application has both a Sub procedure and a Function procedure...

  • Visual Basic: Create an application that simulates a tic tac toe game. The form uses 9...

    Visual Basic: Create an application that simulates a tic tac toe game. The form uses 9 large labels to display the x's and o's. The application should use a two dimensional integer array to simulate the game board in memory. When the user clicks the "New Game" button the application should step throguh the array storing a random number of 0 to 1 in each element. The numer 0 represent the letter o and the number 1 reprsents the letter...

  • An application contains the Structure statement shown here. Structure MyFriend Public strName As String Public strBirthday...

    An application contains the Structure statement shown here. Structure MyFriend Public strName As String Public strBirthday As String End Structure Create a VB.Net Windows Form application named MyFriend_YourName. Change the name property of your form to frmMain. Add the MyFriend Structure to the public class frmMain. Create 2 text boxes txtName and txtBirthday, and a button with the name property changed to btnExecute and the text property changed to Execute. In the button click event, write a Dim statement that...

  • n JAVA, students will create a linked list structure that will be used to support a...

    n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...

  • Inventory (linked lists: insert at the front of a list)

    import java.util.Scanner;public class Inventory {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);             InventoryNode headNode;                                                    InventoryNode currNode;      InventoryNode lastNode;      String item;      int numberOfItems;      int i;      // Front of nodes list           ...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

  • In this exercise, you’ll add code to a form that converts the value the user enters...

    In this exercise, you’ll add code to a form that converts the value the user enters based on the selected conversion type. The application should handle the following conversions: 1. Open the Conversions project. Display the code for the form, and notice the rectangular array whose rows contain the value to be displayed in the combo box, the text for the labels that identify the two text boxes, and the multiplier for the conversion as shown above. Note: An array...

  • Project 10-1 Convert lengths In this assignment, you’ll add code to a form that converts the valu...

    Project 10-1 Convert lengths In this assignment, you’ll add code to a form that converts the value the user enters based on the selected conversion type. The application should handle the following conversions: From To Conversion Miles - Kilometers: 1 mile = 1.6093 kilometers Kilometers - Miles: 1 kilometer = 0.6214 miles Feet - Meters: 1 foot = 0.3048 meters Meters - Feet: 1 meter = 3.2808 feet Inches - Centimeters: 1 inch = 2.54 centimeters Centimeters - Inches: 1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT