' Project PhoneBook4 ' Find phone number given the name in a Windows application. ' Use one array list with comma delimited strings loaded ' from a text file. Imports System.IO Public Class Form1 Inherits System.Windows.Forms.Form Public phoneNumberEntries(12) As String Public index As Integer #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 = "PhoneBook4 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 Dim f As New StreamReader("PhoneBookNumbers.txt") Dim i As Integer = 0 Dim line As String ' Add names and phonenumbers to names ArrayList. index = 0 While f.Peek() > -1 line = f.ReadLine() phoneNumberEntries(index) = line index += 1 End While 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 index - 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