我正在创建一个简单的程序,可用于保存联系人及其联系人的信息。我在保存/加载功能和插入的联系人总数方面遇到问题。大多数代码取自“TheNewBoston”的教程,因为我喜欢它,所以我尝试添加更多功能。这是源代码:
Public Class Form1
Dim myCustomers As New ArrayList
Dim FILE_NAME As String = "C:\test.txt"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddCustomer("Sam", "Bond", "sambond@msn.com", "9541032163")
AddCustomer("Merry", "Jackson", "merryjackson@msn.com", "8872101103")
AddCustomer("Rachel", "Smith", "rachelsmith@msn.com", "4839078565")
'DOESN'T WORK''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If System.IO.File.Exists(FILE_NAME) = True Then
'AddCustomer.System.IO.StreamReader(FILE_NAME)
'or
AddCustomer(System.IO.StreamReader(FILE_NAME))
Else
MessageBox.Show("File does not exist.")
End If
End Sub
'Public variables
Private Structure Customer
Public FirstName As String
Public LastName As String
Public Email As String
Public Phone As Decimal
'Does Name = First&Last Name
Public ReadOnly Property Name() As String
Get
Return FirstName & " " & LastName
End Get
End Property
'Shows the customers in the listbox properly overriding the default ToString function
Public Overrides Function ToString() As String
Return Name
End Function
End Structure
'Declaring and connecting to type Customer
Private objCustomer As Customer
Private objNewCustomer As Customer
'Makes customer format
Private Sub AddCustomer(ByVal firstName As String, ByVal lastName As String, ByVal email As String, ByVal phone As Decimal)
'declares objNewCustomer with the type of customer for use
Dim objNewCustomer As Customer
'Connects the Customer's 4 things to objNewCustomer
objNewCustomer.FirstName = firstName
objNewCustomer.LastName = lastName
objNewCustomer.Email = email
objNewCustomer.Phone = phone
'Adds to myCustomers array list the objNewCustomer
myCustomers.Add(objNewCustomer)
'Adds customer Name to list
listCustomers.Items.Add(objNewCustomer.ToString())
End Sub
'Avoids customer select error
Private ReadOnly Property SelectedCustomer() As Customer
Get
If listCustomers.SelectedIndex <> -1 Then
Return CType(myCustomers(listCustomers.SelectedIndex), Customer)
End If
End Get
End Property
'Enables select customer
Private Sub listCustomers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listCustomers.SelectedIndexChanged
DisplayCustomer(SelectedCustomer)
End Sub
'Loads the customers' information
Private Sub DisplayCustomer(ByVal cust As Customer)
txtName.Text = cust.Name
txtFirstName.Text = cust.FirstName
txtLastName.Text = cust.LastName
txtEmail.Text = cust.Email
txtPhone.Text = cust.Phone
End Sub
'Add User (pops up new window)
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Form2.Show()
'System.IO.File.WriteAllText("C:\test.txt", Textbox1.Text)
End Sub
'WORKS
Private Sub btnTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotal.Click
txtTotal.Text = myCustomers.Count.ToString()
End Sub
'Private Total2 As Integer
'experimenting
'Private Sub DisTotal(ByVal Total As Integer)
' Do
' 'total2 = myCustomers.Count.ToString()
' 'txtTotal.Text = total2
' txtTotal.Text = Total
' System.Threading.Thread.Sleep(5000)
' Loop
'End Sub
Private Sub listTotal_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Total = myCustomers.Count
'DOESN'T WORK''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Do
'total2 = myCustomers.Count.ToString()
'txtTotal.Text = total2
txtTotal.Text = myCustomers.Count.ToString()
System.Threading.Thread.Sleep(5000)
Loop
End Sub
Private Total As Integer
'DOESN'T WORK''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim writer As New IO.StreamWriter(FILE_NAME)
Try
writer.Write("")
writer.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
writer.Close()
End Try
End Sub End Class
任何帮助/提示表示赞赏,如果您发布更正的代码块,请解释它是如何工作的/出了什么问题,谢谢。