Help!!!! - how to populate text boxes from selecting an item in a List box

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have 2 forms, one named New customer and one Named Customer. The user adds the record to a text file with a CustomerArray from the New Customer form. Then i have got the records to display on the Customer form with a list box, but when i select the customer on the List box i want their information to populate the text boxes. I am new to Visual Basic, Please help!!
Customer formImports System.IO
Public Class frmCustomers
Dim filereader As StreamReader
Dim CustomerArray() As Customer
Dim intNumberofRecords As Integer
Dim fileWriter As StreamWriter
Private Sub frmCustomers_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ClearList()
Dim file_name As String
file_name = "Customer.txt"
If My.Computer.FileSystem.FileExists("Customer.txt") = True Then
filereader = New StreamReader(file_name)
Call readrecord()
filereader.Close()
End If
End Sub
Sub readrecord()
Dim intcounter As Integer
intNumberofRecords = filereader.ReadLine

ReDim CustomerArray(intNumberofRecords)

intcounter = 0
Do Until filereader.EndOfStream = True

intcounter = intcounter + 1
CustomerArray(intcounter).strCustomerID = filereader.ReadLine
CustomerArray(intcounter).strTitle = filereader.ReadLine
CustomerArray(intcounter).strFirstName = filereader.ReadLine
CustomerArray(intcounter).strSurname = filereader.ReadLine
CustomerArray(intcounter).strTelephone = filereader.ReadLine
CustomerArray(intcounter).strEmail = filereader.ReadLine

lstCustomer.Items.Add(CustomerArray(intcounter).CustName)
Loop
End Sub
Sub ClearList()
lstCustomer.Items.Clear()
End Sub
Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
Me.Close()
End Sub
Private Sub mnuNewCustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNewCustomer.Click
frmNewCustomer.Show()
Me.Close()
End Sub
Private Sub mnuMainMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuMainMenu.Click
frmMainMenu.Show()
Me.Close()
End Sub
End Class
New Customer FormImports System.IO
Public Class frmNewCustomer
Dim Title As String
Dim FirstName As String
Dim Surname As String
Dim Telephone As String
Dim Email As String
Dim DotCom As String
Dim filereader As StreamReader
Dim CustomerArray() As Customer
Dim intNumberofRecords As Integer
Dim fileWriter As StreamWriter
Dim i As Integer = frmCustomers.lstCustomer.Items.Count
Private Sub frmNewCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
i = i + 1
txtCustomerNumber.Text = i
End Sub
Private Sub mnuAddCust_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAddCust.Click
Title = cmbTitle.Text
FirstName = txtFirstName.Text
Surname = txtSurname.Text
Telephone = txtTelephone.Text
Email = txtEmail.Text
StrConv(txtFirstName.Text, VbStrConv.ProperCase)
StrConv(txtSurname.Text, VbStrConv.ProperCase)

If Title = "" Then
MsgBox("Please enter the Customers Title")
ElseIf FirstName = "" Then
MsgBox("Please enter the Customers First Name")
ElseIf Surname = "" Then
MsgBox("Please enter the Customers Surname")
ElseIf Telephone = "" Then
MsgBox("Please enter the Customers Telephone Number")
ElseIf IsNumeric(Telephone) = False Then
MsgBox("Incorrect Telephone Number, Please Try Again")
ElseIf Telephone.Length = 10 = False Then
MsgBox("Incorrect Telephone Number, Please Try Again")
ElseIf Email = "" Then
MsgBox("Please enter the Customers Email")
ElseIf Email.Contains("@") = False Then
MsgBox("Incorrect Email Address, Please Try Again")
Else
Dim file_name As String
file_name = "Customer.txt"

If My.Computer.FileSystem.FileExists("Customer.txt") = True Then
filereader = New StreamReader(file_name)
Call readrecord()
filereader.Close()
End If
fileWriter = New StreamWriter(file_name)
Call writerecord()
fileWriter.Close()
clearform()
frmCustomers.Show()
Me.Close()
End If
End Sub
Sub readrecord()
Dim intcounter As Integer
intNumberofRecords = filereader.ReadLine

ReDim CustomerArray(intNumberofRecords)

intcounter = 0
Do Until filereader.EndOfStream = True

intcounter = intcounter + 1
CustomerArray(intcounter).strCustomerID = filereader.ReadLine
CustomerArray(intcounter).strTitle = filereader.ReadLine
CustomerArray(intcounter).strFirstName = filereader.ReadLine
CustomerArray(intcounter).strSurname = filereader.ReadLine
CustomerArray(intcounter).strTelephone = filereader.ReadLine
CustomerArray(intcounter).strEmail = filereader.ReadLine

frmCustomers.lstCustomer.Items.Add(CustomerArray(intcounter).CustName)
Loop

End Sub
Sub writerecord()
Dim intcounter As Integer
intNumberofRecords = intNumberofRecords + 1

ReDim Preserve CustomerArray(intNumberofRecords)

CustomerArray(intNumberofRecords).strFirstName = txtFirstName.Text
CustomerArray(intNumberofRecords).strCustomerID = txtCustomerNumber.Text
CustomerArray(intNumberofRecords).strTitle = cmbTitle.Text
CustomerArray(intNumberofRecords).strFirstName = txtFirstName.Text
CustomerArray(intNumberofRecords).strSurname = txtSurname.Text
CustomerArray(intNumberofRecords).strTelephone = txtTelephone.Text
CustomerArray(intNumberofRecords).strEmail = txtEmail.Text

fileWriter.WriteLine(intNumberofRecords)

For intcounter = 1 To intNumberofRecords
fileWriter.WriteLine(CustomerArray(intcounter).strCustomerID)
fileWriter.WriteLine(CustomerArray(intcounter).strTitle)
fileWriter.WriteLine(CustomerArray(intcounter).strFirstName)
fileWriter.WriteLine(CustomerArray(intcounter).strSurname)
fileWriter.WriteLine(CustomerArray(intcounter).strTelephone)
fileWriter.WriteLine(CustomerArray(intcounter).strEmail)

Next

MsgBox("The Customer has been Saved")

End Sub
Sub clearform()
txtCustomerNumber.Clear()
cmbTitle.Refresh()
txtFirstName.Clear()
txtSurname.Clear()
txtTelephone.Clear()
txtEmail.Clear()
End Sub
Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
Me.Close()
End Sub
Private Sub mnuMainMenu_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuMainMenu.Click
frmMainMenu.Show()
Me.Close()
End Sub
Private Sub mnuCustomer_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCustomer.Click
frmCustomers.Show()
Me.Close()
End Sub



End Class

View the full article
 
Back
Top