' PhoneBook-NoArrays Example ' The user inputs a name. The application looks up the ' name in a file and outputs the corresponding phone number. Imports System.IO Public Class Form1 Inherits System.Windows.Forms.Form #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 btnLookup As System.Windows.Forms.Button Friend WithEvents txtName As System.Windows.Forms.TextBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents txtPhoneNumber As System.Windows.Forms.TextBox Private Sub InitializeComponent() Me.btnLookup = New System.Windows.Forms.Button Me.txtName = 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.SuspendLayout() ' 'btnLookup ' Me.btnLookup.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnLookup.Location = New System.Drawing.Point(32, 152) Me.btnLookup.Name = "btnLookup" Me.btnLookup.Size = New System.Drawing.Size(216, 32) Me.btnLookup.TabIndex = 0 Me.btnLookup.Text = "Lookup Phone Number" ' 'txtName ' Me.txtName.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtName.Location = New System.Drawing.Point(32, 24) Me.txtName.Name = "txtName" Me.txtName.Size = New System.Drawing.Size(216, 26) Me.txtName.TabIndex = 1 Me.txtName.Text = "" ' 'Label1 ' Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(104, 56) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(56, 24) Me.Label1.TabIndex = 2 Me.Label1.Text = "Name" ' 'txtPhoneNumber ' Me.txtPhoneNumber.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtPhoneNumber.Location = New System.Drawing.Point(32, 88) Me.txtPhoneNumber.Name = "txtPhoneNumber" Me.txtPhoneNumber.Size = New System.Drawing.Size(216, 26) Me.txtPhoneNumber.TabIndex = 3 Me.txtPhoneNumber.Text = "" ' 'Label2 ' Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label2.Location = New System.Drawing.Point(72, 120) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(136, 24) Me.Label2.TabIndex = 4 Me.Label2.Text = "Phone Number" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(280, 206) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.txtPhoneNumber) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.txtName) Me.Controls.Add(Me.btnLookup) Me.Name = "Form1" Me.Text = "PhoneBook-NoArrays Example" Me.ResumeLayout(False) End Sub #End Region Private Sub btnLookup_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnLookup.Click ' Variable to hold a line from the input file and to hold ' return array from Split method. Dim line, fields() As String ' Variables to hold name and phone number from file. Dim name, phoneNumber ' Open file with names and phone numbers. Dim reader As New StreamReader("PhoneBookNumbers.txt") ' Get name to find. Dim nameToFind As String = txtName.Text ' Search for name in file. While reader.Peek() > -1 line = reader.ReadLine() fields = Split(line, ",") name = fields(0) phoneNumber = fields(1) If name = nameToFind Then txtPhoneNumber.Text = phoneNumber Exit Sub End If End While ' If you make it to end of array, print "Name not found." txtPhoneNumber.Text = "Name not found." End Sub End Class