In the following code I need to modify it so that the btnSave control's click event procedure determines whether the user has entered the phone number in the required format: three digits, a hyphen, three digites, a hyphen, and four digits. If not a message should display saying the format is incorrect. The text box (txtPhone) is used to enter the phone number, the numbers are then added to a text file (phoneNumbers.txt). In the original there are to labe boxes for output, one for the numbers as they appear on the text file(111222333) called lblFileContents, and another after being correctly formatted through code(111-222-3333) called lblFormattedNumbers. Obviously both label boxes will display the same numbers once the code is modified to accept only the proper format(111-222-3333). Here is the original code:
Public Class frmMain
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub txtPhone_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPhone.KeyPress
' allows the text box to accept numbers, the hyphen, and the Backspace key
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> "-" AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
Private Sub txtPhone_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPhone.TextChanged
lblFileContents.Text = String.Empty
lblFormattedNumbers.Text = String.Empty
End Sub
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
' saves the phone number to a sequential access file
' declare variables
Dim strPhone As String
Dim outFile As IO.StreamWriter
' remove the hyphens
strPhone = txtPhone.Text.Replace("-", String.Empty)
' verify the length
If strPhone.Length = 10 Then
outFile = IO.File.AppendText("phoneNumbers.txt")
outFile.WriteLine(strPhone)
outFile.Close()
Else
MessageBox.Show("Invalid phone number",
"Phone Numbers",
MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
' clear the txtPhone control, then set the focus
txtPhone.Text = String.Empty
txtPhone.Focus()
End Sub
Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
' displays the phone numbers contained in the file
' also displays the phone numbers after inserting hyphens
' declare variables
Dim inFile As IO.StreamReader
Dim strPhone As String
' clear previous phone numbers from the labels
lblFileContents.Text = String.Empty
lblFormattedNumbers.Text = String.Empty
' determine whether the file exists
If IO.File.Exists("phoneNumbers.txt") = True Then
' open the file for input
inFile = IO.File.OpenText("phoneNumbers.txt")
' process loop until end of the file
Do Until inFile.Peek = -1
' read a phone number, then display the number
strPhone = inFile.ReadLine
lblFileContents.Text = lblFileContents.Text &
strPhone & ControlChars.NewLine
' display the phone number with hyphens
strPhone = strPhone.Insert(3, "-")
strPhone = strPhone.Insert(7, "-")
lblFormattedNumbers.Text =
lblFormattedNumbers.Text &
strPhone & ControlChars.NewLine
Loop
' close the file
inFile.Close()
Else
MessageBox.Show("Can't find the phoneNumbers.txt file",
"Phone Numbers", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
End Sub
End Class
Any help is greatly appreciated.