'Program: Programming Exercise Chapter 10.2 'Programmer: Joseph Schuman 'Date: October 2014 'Class: VendorsForm.vb 'Description: A form for entering and viewing Vendor objects ' in a Vendor collection. Uses a Listbox collection. Public Class vendorForm Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click ' Create a Vendor object and add it to the collection. 'Declare a Vendor object Dim aVendor As Vendor 'Declare a boolean variable to aid in testing for a duplicate value and initialize it to False Dim FoundBoolean As Boolean = False 'Validate that all text boxes have data in them If nameTextBox.Text <> "" Then If phoneTextBox.Text <> "" Then If contactTextBox.Text <> "" Then If emailTextBox.Text <> "" Then ' Declare and instantiate a new Vendor. Try 'Check for Duplicate vendor With vendorsListBox 'Check the list box for dupicates using a loop. For indexInteger As Integer = 0 To .Items.Count - 1 'Instantite a vendor object with the current item in the check box. 'Change the list item in the check box to a vendor object and store in aVendor. aVendor = CType(.Items.Item(indexInteger), Vendor) 'Check for duplicate in the check box. If nameTextBox.Text.ToLower = aVendor.Name.ToLower Then 'Set FoundBoolean to true and Exit. FoundBoolean = True Exit For End If 'Otherwise go to the next item in the list box. Next indexInteger End With 'If not found then create aVendor object and add it to the items collection If Not foundBoolean Then 'Create the vendor object in the class by calling the constructor function aVendor = New Vendor(nameTextBox.Text, phoneTextBox.Text, _ contactTextBox.Text, emailTextBox.Text) 'Add the vendor object to the Items collection. vendorsListBox.Items.Add(aVendor) 'Display the count in countLabel.Text countLabel.Text = vendorsListBox.Items.Count.ToString ' Clear the text boxes. With nameTextBox .Clear() .Focus() End With phoneTextBox.Clear() contactTextBox.Clear() emailTextBox.Clear() Else 'Duplicate name found MessageBox.Show("Duplicate Vendor name." & _ ControlChars.NewLine & nameTextBox.Text & _ " Not Added.", "A Collection of Vendors") With nameTextBox .Clear() .Focus() End With phoneTextBox.Clear() contactTextBox.Clear() emailTextBox.Clear() End If 'Add the Catch statement to process any errors during the add Catch Ex As Exception MessageBox.Show("Error: " & Ex.Message & ControlChars.NewLine & "Unable To Process", "Error", _ MessageBoxButtons.OK, MessageBoxIcon.Exclamation) With nameTextBox .SelectAll() .Focus() End With End Try Else With emailTextBox .SelectAll() .Focus() End With MessageBox.Show("Please enter an e-mail address.", _ "A Collection of Vendors") End If Else With contactTextBox .SelectAll() .Focus() End With MessageBox.Show("Please enter a contact person.", _ "A Collection of Vendors") End If Else With phoneTextBox .SelectAll() .Focus() End With MessageBox.Show("Please enter a phone number.", _ "A Collection of Vendors") End If Else With nameTextBox .SelectAll() .Focus() End With MessageBox.Show("Please enter a name.", _ "A Collection of Vendors") End If End Sub Private Sub removeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeButton.Click ' Remove selected item from the collection. With vendorsListBox If .SelectedIndex <> -1 Then 'Remove the current item from the list box .Items.RemoveAt(.SelectedIndex) ' Display the count. countLabel.Text = .Items.Count.ToString Else MessageBox.Show("Select a Vendor from the list.", _ "A Collection of Vendors") End If End With End Sub Private Sub displayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displayButton.Click ' Display the information for the selected Vendor. Dim aVendor As Vendor Dim messageString As String With vendorsListBox If .SelectedIndex <> -1 Then ' Get the selected vendor item from the collection. aVendor = CType(.Items.Item(.SelectedIndex), Vendor) 'Display the informstion from the aVendor object messageString = "The information for the selected company: is: " & _ ControlChars.NewLine & "Name : " & aVendor.Name & _ ControlChars.NewLine & "Phone : " & aVendor.Phone & _ ControlChars.NewLine & "Contact : " & aVendor.Contact & _ ControlChars.NewLine & "E-Mail : " & aVendor.EMail MessageBox.Show(messageString, "A Collection of Vendors") Else MessageBox.Show("Select a Vendor from the list.", _ "A Collection of Vendors") End If End With End Sub End Class 'Program: Programming Exercise Chapter 10.2 'Programmer: Joseph Schuman 'Date: October 2014 'Class: VendorClass.vb 'Description: Create a Vendor class. Public Class Vendor ' Private property variables. Private nameString As String Private phoneString As String Private contactString As String Private emailString As String ' Class constructor. Public Sub New(ByVal nameString As String, _ ByVal phoneString As String, ByVal contactString As String, ByVal emailString As String) ' Create a Vendor object. Me.Name = nameString Me.Phone = phoneString Me.Contact = contactString Me.EMail = emailString End Sub ' Property procedures. Public Property Name() As String Get Return nameString End Get Set(ByVal Value As String) nameString = Value End Set End Property Public Property Phone() As String Get Return phoneString End Get Set(ByVal Value As String) phoneString = Value End Set End Property Public Property Contact() As String Get Return contactString End Get Set(ByVal Value As String) contactString = Value End Set End Property Public Property EMail() As String Get Return emailString End Get Set(ByVal Value As String) emailString = Value End Set End Property ' Add the Procedure in the Vendor class to override the ToString function. Public Overrides Function ToString() As String 'Return the Name property for this object's ToString method. Return nameString End Function End Class