連絡先とその連絡先に関する情報を保存するために使用できる簡単なプログラムを作成しています。保存/読み込み機能と挿入された連絡先の合計数に問題があります。ほとんどのコードは "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
ヘルプ/ヒントをいただければ幸いです。また、修正されたコードブロックを投稿する場合は、それがどのように機能するか/何が間違っていたかを説明してください。