Question
Starting Out with Visual Basic 2012 6th edition CH 9 Programming Challenge 11 Customer Accounts: My code is based on tje posted solution on the
Starting Out with Visual Basic 2012 6th edition CH 9 Programming Challenge 11 Customer Accounts: My code is based on tje posted solution on the Chegg site. My application is a more complicate version that requires an AddForm, an additional button for Next Record (for use after searching records)Menus: File, Submenus(open, Print, exit) Edit, submenu(Add new Record), Search, submenus(By Customer Number, By Last Name) Help, submenu(About) It is not working and I am spinning my wheels making it worse: Can you help me correct the code provided so that AddForm accepts data input, saves the data in a file called Records.txt and clear the form for the next record to be entered?
| Text |
|
|
MenuStrip1 | MenuStrip1 |
|
|
sfdCustomerAccounts |
|
|
|
prtCustomerReport |
|
|
|
Menu Items | Text | Shortcut Keys |
|
mnuFile | &File | Ctrl+F |
|
mnuFileOpen | &Open | Ctrl+O |
|
mnuFilePrint | | Ctrl+P |
|
mnuFileExit | E&xit | Ctrl+Q |
|
mnuEdit | &Edit | Ctrl+E |
|
mnuEditAddNewRecord | Add &New Record | Ctrl+N |
|
mnuSearch | &Search | None |
|
mnuSearchByCustomerNumber | By Customer Number | None |
|
mnuSearchByLastName | By Last Name | None |
|
mnuHelp | &Help | Ctrl+H |
|
mnuHelpAbout | &About | None |
|
Control Type | Control Name | Property | Property Value |
Form | MainForm | Text | Customer Accounts |
|
|
|
|
GroupBox | GroupBox1 | Text | Account Information |
Label | Label1 | Text | Customer Account Information |
Label | Label2 | Text | Last Name: |
Label | Label3 | Text | First Name: |
Label | LaBEL4 | Text | Customer Number: |
Label | Label 5 | Text | Address: |
Label | Label 6 | Text | City: |
Label | Label 7 | Text | State: |
Label | Label 8 | Text | ZIP Code: |
Label | Label9 | Text | Telephone Number |
Label | Label10 | Text | Account Balance |
Label | Label11 | Text | Date of Last Payment: |
TextBox | txtLastName |
|
|
TextBox | txtFirstName |
|
|
TextBox | txtCustomerNumber |
|
|
TextBox | txtAddress |
|
|
TextBox | txtCity |
|
|
TextBox | txtState |
|
|
TextBox | txtZIPCode |
|
|
TextBox | txtTelephoneNumber |
|
|
TextBox | txtAccountBalance |
|
|
TextBox | txtDateOfLastPayment |
|
|
Button | btnNextRecord | Text | &Next Record |
Button | btnClear | Text | C&lear |
Button | btnExit | Text | E&xit |
|
|
|
|
Form | AddForm | Text | Add New Customer |
GroupBox | GroupBox1 | Text | Account Information |
Label | Label1 | Text | Enter New Customer Account Information |
Label | Label2 | Text | Last Name: |
Label | Label3 | Text | First Name: |
Label | LaBEL4 | Text | Customer Number: |
Label | Label 5 | Text | Address: |
Label | Label 6 | Text | City: |
Label | Label 7 | Text | State: |
Label | Label 8 | Text | ZIP Code: |
Label | Label9 | Text | Telephone Number |
Label | Label10 | Text | Account Balance |
Label | Label11 | Text | Date of Last Payment: |
TextBox | txtLastName |
|
|
TextBox | txtFirstName |
|
|
TextBox | txtCustomerNumber |
|
|
TextBox | txtAddress |
|
|
TextBox | txtCity |
|
|
TextBox | txtState |
|
|
TextBox | txtZIPCode |
|
|
TextBox | txtTelephoneNumber |
|
|
TextBox | txtAccountBalance |
|
|
Button | btnSaveRecord | Text | &Save Record |
Button | btnClear | Text | C&lear |
Button | btnExit | Text | E&xit |
Imports System.IO
Imports System.IO.FileStream
Public Class MainForm
Dim txtFile As IO.StreamWriter ' Object variable
Dim searchFile As IO.StringReader ' Object variable
Structure CustomerAccounts ' Set up the structure for the customer accounts.
Dim LastName As String
Dim FirstName As String
Dim CustomerNumber As String
Dim Address As String
Dim City As String
Dim State As String
Dim ZipCode As Integer
Dim TelphoneNumber As String
Dim AcctountBalance As Double
Dim DateOfLastPmyment As String
End Structure
' Define structure variables
Dim CSearchRecord As CustomerAccounts = New CustomerAccounts
Dim CustomerRecord As CustomerAccounts
Dim fileName As String
' Save the text of the content to a file.
Sub SaveDocument()
txtFile = File.AppendText(fileName)
txtFile.WriteLine(CustomerRecord.LastName)
txtFile.WriteLine(CustomerRecord.FirstName)
txtFile.WriteLine(CustomerRecord.CustomerNumber)
txtFile.WriteLine(CustomerRecord.Address)
txtFile.WriteLine(CustomerRecord.CustomerNumber)
txtFile.WriteLine(CustomerRecord.State)
txtFile.WriteLine(CustomerRecord.ZipCode)
txtFile.WriteLine(CustomerRecord.TelphoneNumber)
txtFile.WriteLine(CustomerRecord.AcctountBalance)
txtFile.WriteLine(CustomerRecord.DateOfLastPmyment)
' Close file after write operation.
txtFile.Close()
End Sub
Private Sub btnSaveRecord_Click(sender As Object, e As EventArgs)
' Assign customer data to the structure variables
CustomerRecord.LastName = txtLastName.Text
CustomerRecord.FirstName = txtFirstName.Text
CustomerRecord.CustomerNumber = txtCustomerNumber.Text
CustomerRecord.Address = txtAddress.Text
CustomerRecord.City = txtCity.Text
CustomerRecord.State = txtState.Text
CustomerRecord.ZipCode = CInt(txtZIPCode.Text)
CustomerRecord.TelphoneNumber = txtTelephoneNumber.Text
CustomerRecord.AcctountBalance = CDbl(txtAccountBalance.Text)
CustomerRecord.DateOfLastPmyment = txtDateOfLastPayment.Text
' Assign file name of the record.
fileName = "Records.txt"
' Test the file to find out if it is empty.
If fileName = String.Empty Then
If sfdCustomerAccounts.ShowDialog = Windows.Forms.DialogResult.OK Then
fileName = sfdCustomerAccounts.FileName
SaveDocument()
End If
Else
SaveDocument()
End If
End Sub
' Clear data from the form fields.
Private Sub clearFields()
' Clear the data from the fields.
txtAccountBalance.Clear()
txtAddress.Clear()
txtCity.Clear()
txtCustomerNumber.Clear()
txtDateOfLastPayment.Clear()
txtFirstName.Clear()
txtLastName.Clear()
txtState.Clear()
txtTelephoneNumber.Clear()
txtZIPCode.Clear()
' set the focus
txtLastName.Focus()
End Sub
' Open the file if the fileName is empty.
Sub OpenFile()
If fileName = String.Empty Then
fileName = "Records.txt"
End If
searchFile = File.OpenText(fileName)
End Sub
' Validation of the last name field.
Private Sub txtLastName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtLastName.Validating
If txtLastName.Text.Trim = "" Then
MessageBox.Show("Last name should not be empty")
txtLastName.Focus()
End If
End Sub
' Validation of the first name field.
Private Sub txtFirstName_validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtFirstName.Validating
If txtFirstName.Text.Trim = "" Then
MessageBox.Show("First name should not be empty")
txtFirstName.Focus()
End If
End Sub
' Validation of the customer number field.
Private Sub txtCustomerNumber_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtCustomerNumber.Validating
If txtCustomerNumber.Text.Trim = "" Then
MessageBox.Show("Customer number should not be empty")
txtCustomerNumber.Focus()
End If
End Sub
' Validation of the adress field.
Private Sub txtAddress_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtAddress.Validating
If txtAddress.Text.Trim = "" Then
MessageBox.Show("Address should not be empty")
txtAddress.Focus()
End If
End Sub
' Validation of the city field.
Private Sub txtCity_Validating(ByValsender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtCity.Validating
If txtCity.Text.Trim = "" Then
MessageBox.Show("City should not be be empty")
txtCity.Focus()
End If
End Sub
' Validation of the state field.
Private Sub txtState_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtState.Validating
If txtState.Text.Trim = "" Then
MessageBox.Show("State should not be empty")
txtState.Focus()
End If
End Sub
' Validation of the ZIP Code field.
Private Sub txtZIPCode_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtZIPCode.Validating
If txtZIPCode.Text.Trim = "" Then
MessageBox.Show("ZIP code should not be empty")
txtZIPCode.Focus()
End If
End Sub
' Validation of the telephone number field.
Private Sub txtTelephoneNumber_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtTelephoneNumber.Validating
If txtTelephoneNumber.Text.Trim = "" Then
MessageBox.Show("Telephone number should not be empty")
txtTelephoneNumber.Focus()
End If
End Sub
' Validation of the account balance field.
Private Sub txtAccountBalance_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtAccountBalance.Validating
If txtAccountBalance.Text.Trim = "" Then
MessageBox.Show(" Account balance should not be empty")
txtAccountBalance.Focus()
End If
End Sub
' Validation of the date of last payment field.
Private Sub txtDateOfLastPayment_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtDateOfLastPayment.Validating
If txtDateOfLastPayment.Text.Trim = "" Then
MessageBox.Show("Date of last payment should nor be empty")
txtDateOfLastPayment.Focus()
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
' Close the form.
Me.Close()
End Sub
' Search by last name.
Private Sub mnuSearchByLastName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuSearchByLastName.Click
Dim strLastName As String
Dim intFlag As Integer = 0
' Open the file in read mode to search for a record in an existing file.
OpenFile()
' Get the name of the record from the user.
strLastName = InputBox("Enter the Last Name to search for.")
Try
While CBool(Not CLng(searchFile.ReadToEnd))
CSearchRecord.LastName = searchFile.ReadLine()
CSearchRecord.FirstName = searchFile.ReadLine()
CSearchRecord.CustomerNumber = searchFile.ReadLine()
CSearchRecord.Address = searchFile.ReadLine()
CSearchRecord.City = searchFile.ReadLine()
CSearchRecord.State = searchFile.ReadLine()
CSearchRecord.ZipCode = CInt(searchFile.ReadLine())
CSearchRecord.TelphoneNumber = CStr(CInt(searchFile.ReadLine()))
CSearchRecord.AcctountBalance = CDbl(searchFile.ReadLine())
CSearchRecord.DateOfLastPmyment = searchFile.ReadLine()
' Compare the current record with the recoed being searched for.
If CSearchRecord.LastName.Equals(txtLastName) Then
intFlag = 1
Exit While
End If
End While
' If record the record is found, display in the form.
If intFlag = 1 Then
txtLastName.Text = CSearchRecord.LastName.ToString()
txtFirstName.Text = CSearchRecord.FirstName.ToString()
txtCustomerNumber.Text = CSearchRecord.CustomerNumber.ToString()
txtAddress.Text = CSearchRecord.Address.ToString()
txtCity.Text = CSearchRecord.City.ToString()
txtState.Text = CSearchRecord.State.ToString()
txtZIPCode.Text = CSearchRecord.ZipCode.ToString()
txtTelephoneNumber.Text = CSearchRecord.TelphoneNumber.ToString()
txtAccountBalance.Text = CSearchRecord.AcctountBalance.ToString()
txtDateOfLastPayment.Text = CSearchRecord.DateOfLastPmyment.ToString()
Else
' If record not found, alert the user.
MessageBox.Show("The record does not exist.")
clearFields()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
' Search by customer number.
Private Sub mnuSearchByCustomerNumber_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuSearchByCustomerNumber.Click
Dim strCustNumber As String
Dim intFlag As Integer
' Open file in read mode and search for record.
OpenFile()
intFlag = 0
' Get record number from the user.
strCustNumber = InputBox("Enter the customer number to search for.")
Try
While CBool(Not CLng(searchFile.ReadToEnd))
CSearchRecord.LastName = searchFile.ReadLine()
CSearchRecord.FirstName = searchFile.ReadLine()
CSearchRecord.CustomerNumber = searchFile.ReadLine()
CSearchRecord.Address = searchFile.ReadLine()
CSearchRecord.City = searchFile.ReadLine()
CSearchRecord.State = searchFile.ReadLine()
CSearchRecord.ZipCode = CInt(searchFile.ReadLine())
CSearchRecord.TelphoneNumber = CInt(CLng(searchFile.ReadLine()))
CSearchRecord.AcctountBalance = CDbl(searchFile.ReadLine())
CSearchRecord.DateOfLastPmyment = searchFile.ReadLine()
' Compare the current record with the recoed being searched for.
If CSearchRecord.LastName.Equals(txtLastName) Then
intFlag = 1
Exit While
End If
End While
' If record the record is found, display in the form.
If intFlag = 1 Then
txtLastName.Text = CSearchRecord.LastName.ToString()
txtFirstName.Text = CSearchRecord.FirstName.ToString()
txtCustomerNumber.Text = CSearchRecord.CustomerNumber.ToString()
txtAddress.Text = CSearchRecord.Address.ToString()
txtCity.Text = CSearchRecord.City.ToString()
txtState.Text = CSearchRecord.State.ToString()
txtZIPCode.Text = CSearchRecord.ZipCode.ToString()
txtTelephoneNumber.Text = CSearchRecord.TelphoneNumber.ToString()
txtAccountBalance.Text = CSearchRecord.AcctountBalance.ToString()
txtDateOfLastPayment.Text = CSearchRecord.DateOfLastPmyment.ToString()
Else
' If record not found, alert the user.
MessageBox.Show("The record does not exist.")
clearFields()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub mnuHelpAbout_Click(sender As Object, e As EventArgs) Handles mnuHelpAbout.Click
' Tell the purpose of the application.
MessageBox.Show("This application uses a structure to store information about customer accounts.")
End Sub
Private Sub mnuFilePrint_Click(sender As Object, e As EventArgs) Handles mnuFilePrint.Click
Dim printCustomersReport As Report
' Print the customer report.
printCustomersReport.Print()
End Sub
Private Sub printCustomesReport(sender As Object, e As Printing.PrintPageEventArgs) Handles prtCustomerReport.
PrintDialog()
Dim intCount As Integer = 0
Dim intVertPos As Integer
e.Graphics.DrawString("Customer Report" & "of Accounts", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 200, 10)
' Open the file in read mode.
OpenFile()
intVertPos = 50
Try
' Read to the end of file.
' Print the content to the PrintDocument.
While CBool(Not CLng(searchFile.ReadToEnd))
If intCount = 10 Then
intCount = 0
intVertPos += 20
Else
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Last Name:", searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 15, intVertPos)
intVertPos += 15
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "First Name:", searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 15, intVertPos)
intVertPos += 15
intCount += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Customer Number:", searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 15, intVertPos)
intVertPos += 15
intCount += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Address:", searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 15, intVertPos)
intVertPos += 15
intCount += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "City:", searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 15, intVertPos)
intVertPos += 15
intCount += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "State:", searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 15, intVertPos)
intVertPos += 15
intCount += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Zip Code:", searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 15, intVertPos)
intVertPos += 15
intCount += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Telephone Number:", searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 15, intVertPos)
intVertPos += 15
intCount += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Account Balance:", searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 15, intVertPos)
intVertPos += 15
intCount += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Date of Last Payment:", searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 15, intVertPos)
intVertPos += 15
intCount += 1
End If
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
clearFields()
End Sub
Private Sub mnuFileExit_Click(sender As Object, e As EventArgs) Handles mnuFileExit.Click
' Close the form.
Me.Close()
End Sub
Private Sub btnNextRecord_Click(sender As Object, e As EventArgs) Handles btnNextRecord.Click
End Sub
Private Sub mnuEditAddNewRecord_Click(sender As Object, e As EventArgs) Handles mnuEditAddNewRecord.Click
' Create an instance of the AddForm.
Dim frmAddForm As New AddForm
' Display the form in modal style.
frmAddForm.ShowDialog()
End Sub
End Class
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Imports System.IO
Imports System.IO.FileStream
Public Class AddForm
Dim txtFile As StreamWriter ' Object variable
Dim searchFile As StreamReader ' Object variable
' Structure declaration.
Structure CustomerAccounts
' Variable declaration.
Dim LastName As String
Dim FirstName As String
Dim CustomerNumber As String
Dim Address As String
Dim City As String
Dim State As String
Dim ZIPCode As Integer
Dim TelephoneNumber As Int64
Dim AccountBalance As Double
Dim DateOfLastPayment As String
End Structure
' Define structure variables and string variables.
Dim CSearchRecord As CustomerAccounts = New
Dim CustomerRecord As CustomerAccounts
Dim fileName As String
' Save the text content to the file.
Sub SaveDocument()
txtFile = File.AppendText(fileName)
' Write data to the file.
txtFile.WriteLine(CustomerRecord.LastName)
txtFile.WriteLine(CustomerRecord.FirstName)
txtFile.WriteLine(CustomerRecord.CustomerNumber)
txtFile.WriteLine(CustomerRecord.Address)
txtFile.WriteLine(CustomerRecord.City)
txtFile.WriteLine(CustomerRecord.State)
txtFile.WriteLine(CustomerRecord.ZIPCode)
txtFile.WriteLine(CustomerRecord.TelephoneNumber)
txtFile.WriteLine(CustomerRecord.AccountBalance)
txtFile.WriteLine(CustomerRecord.DateOfLastPayment)
' Close date file after writing the data.
txtFile.Close()
End Sub
Private Sub btnSaveRecord_Click(sender As Object, e As EventArgs) Handles btnSaveRecord.Click
' Assign data to structure variable.
CustomerRecord.LastName = txtLastName.Text
CustomerRecord.FirstName = txtFirstName.Text
CustomerRecord.CustomerNumber = txtCustomerNumber.Text
CustomerRecord.Address = txtAddress.Text
CustomerRecord.CustomerNumber = txtCity.Text
CustomerRecord.State = txtState.Text
CustomerRecord.ZIPCode = CInt(txtZIPCode.Text)
CustomerRecord.TelephoneNumber = CLng(txtTelephoneNumber.Text)
CustomerRecord.AccountBalance = CDbl(txtAccountBalance.Text)
CustomerRecord.DateOfLastPayment = txtDateOfLastPayment.Text
fileName = "Records.txt"
' Check to see if file is empty.
If fileName = String.Empty Then
If sfdCustomerAccounts.ShowDialog = Windows.Forms.DialogResult.OK Then
fileName = sfdCustomerAccounts.FileName
SaveDocument()
End If
Else
SaveDocument()
End If
End Sub
Private Sub ClearFields()
txtAccountBalance.Clear()
txtAddress.Clear()
txtCity.Clear()
txtFirstName.Clear()
txtLastName.Clear()
txtCustomerNumber.Clear()
txtDateOfLastPayment.Clear()
txtState.Clear()
txtTelephoneNumber.Clear()
txtZIPCode.Clear()
' Establish focus on last name.
txtLastName.Focus()
End Sub
End Class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started