' Project PhoneBook3 ' Find phone number given the name in a Windows application. ' Use one array list containing comma delimited strings and ' use the Split function to extract the fields. Public Class Form1 Inherits System.Windows.Forms.Form ' Global array of names and phone numbers. Public phoneNumberEntries() As String #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents txtNameToFind As System.Windows.Forms.TextBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents btnSearch As System.Windows.Forms.Button Friend WithEvents txtPhoneNumber As System.Windows.Forms.TextBox Friend WithEvents Label2 As System.Windows.Forms.Label Private Sub InitializeComponent() Me.txtNameToFind = New System.Windows.Forms.TextBox Me.Label1 = New System.Windows.Forms.Label Me.txtPhoneNumber = New System.Windows.Forms.TextBox Me.Label2 = New System.Windows.Forms.Label Me.btnSearch = New System.Windows.Forms.Button Me.SuspendLayout() ' 'txtNameToFind ' Me.txtNameToFind.Font = New System.Drawing.Font("Microsoft Sans Serif", _ 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, _ CType(0, Byte)) Me.txtNameToFind.Location = New System.Drawing.Point(32, 24) Me.txtNameToFind.Name = "txtNameToFind" Me.txtNameToFind.Size = New System.Drawing.Size(216, 29) Me.txtNameToFind.TabIndex = 0 Me.txtNameToFind.Text = "" ' 'Label1 ' Me.Label1.Location = New System.Drawing.Point(72, 64) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(144, 24) Me.Label1.TabIndex = 1 Me.Label1.Text = "Name To Find" ' 'txtPhoneNumber ' Me.txtPhoneNumber.Font = New System.Drawing.Font("Microsoft Sans Serif", _ 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, _ CType(0, Byte)) Me.txtPhoneNumber.Location = New System.Drawing.Point(32, 104) Me.txtPhoneNumber.Name = "txtPhoneNumber" Me.txtPhoneNumber.Size = New System.Drawing.Size(216, 29) Me.txtPhoneNumber.TabIndex = 2 Me.txtPhoneNumber.Text = "" ' 'Label2 ' Me.Label2.Location = New System.Drawing.Point(72, 144) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(144, 24) Me.Label2.TabIndex = 3 Me.Label2.Text = "Phone Number" ' 'btnSearch ' Me.btnSearch.Location = New System.Drawing.Point(32, 192) Me.btnSearch.Name = "btnSearch" Me.btnSearch.Size = New System.Drawing.Size(216, 40) Me.btnSearch.TabIndex = 4 Me.btnSearch.Text = "Search" ' 'Form1 ' Me.AcceptButton = Me.btnSearch Me.AutoScaleBaseSize = New System.Drawing.Size(10, 22) Me.ClientSize = New System.Drawing.Size(280, 266) Me.Controls.Add(Me.btnSearch) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.txtPhoneNumber) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.txtNameToFind) Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, _ System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, _ CType(0, Byte)) Me.Name = "Form1" Me.Text = "PhoneBook3 Example" Me.ResumeLayout(False) End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' Create array of names and phone numbers. phoneNumberEntries = New String() _ {"Mickey Mouse,323/736-9278", _ "Minnie Mouse,323/874-2383", _ "Donald Duck,323/763-3837", _ "Sylvester Cat,323/982-9883", _ "Dewey Duck,323/803-8473", _ "Hewey Duck,323/763-9388", _ "Louie Duck,323/763-9308", _ "Yosemite Sam,323/763-2183", _ "Tweety Bird,323/337-5394", _ "Snaggle Puss,323/653-9387"} End Sub Private Sub btnSearch_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSearch.Click Dim i As Integer Dim fields(), name, phoneNumber As String For i = 0 To phoneNumberEntries.GetUpperBound(0) - 1 ' Use Split function to extract name and phoneNumber. fields = Split(phoneNumberEntries(i), ",") name = fields(0) phoneNumber = fields(1) If txtNameToFind.Text = name Then txtPhoneNumber.Text = phoneNumber Exit Sub End If Next txtPhoneNumber.Text = "Name Not Found" End Sub End Class