연락처 및 연락처 정보를 저장하는 데 사용할 수있는 간단한 프로그램을 만들고 있습니다. 기능을 저장 /로드하고 총계가 개의 연락처가 삽입 된 문제가 있습니다. 대부분의 코드는 "TheNewBoston"자습서에서 가져온 것입니다. 이후에 더 많은 기능을 추가하려고합니다. 여기에 소스 코드 :vb.net 파일 및 기타 문제 저장 /로드
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", "[email protected]", "9541032163")
AddCustomer("Merry", "Jackson", "[email protected]", "8872101103")
AddCustomer("Rachel", "Smith", "[email protected]", "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
어떤 도움/힌트가 감사하고 코드 블록을 수정 게시 할 경우에도 그것이 어떻게 작동하는지 설명해주십시오 /도 무엇이 잘못된 것인지, 감사합니다.
exectly does not work? 메시지 상자에 어떤 오류가 표시됩니까? 스트림 작성기를 사용하는 대신 [텍스트 작성자] (http://msdn.microsoft.com/en-us/library/system.io.textwriter.aspx)를 확인해보십시오. . –
먼저 '고객'을 하드 코딩하지 않아야합니다. 그래서 .txt로 저장하고 앱 시작시로드하려고합니다. 둘째, 버튼을 클릭하여 삽입 한 총 '고객'을 표시 할 수 있었지만 앱 시작시 버튼없이 표시하려고했습니다. 오류가 표시되지 않습니다. – Jim