vb.net保存/加载文件及其他问题

vb.net保存/加载文件及其他问题,vb.net,Vb.net,我正在创建一个简单的程序,可以用来保存联系人及其联系人的信息。我在保存/加载功能和插入联系人的总数方面遇到问题。大多数代码取自“TheNewBoston”教程,因为我喜欢它,所以我尝试添加更多功能。以下是源代码: Public Class Form1 Dim myCustomers As New ArrayList Dim FILE_NAME As String = "C:\test.txt" Private Sub Form1_Load(ByVal sender A

我正在创建一个简单的程序,可以用来保存联系人及其联系人的信息。我在保存/加载功能和插入联系人的总数方面遇到问题。大多数代码取自“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
公共类表单1
将myCustomers设置为新的ArrayList
Dim文件\u名称为String=“C:\test.txt”
私有子表单1_Load(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
添加客户(“Sam”、“Bond”、”sambond@msn.com", "9541032163")
AddCustomer(“Merry”、“Jackson”、”merryjackson@msn.com", "8872101103")
AddCustomer(“Rachel”、“Smith”和“rachelsmith@msn.com", "4839078565")
“不管用”
如果System.IO.File.Exists(File_NAME)=True,则
'AddCustomer.System.IO.StreamReader(文件名)
”“或者
AddCustomer(System.IO.StreamReader(文件名))
其他的
Show(“文件不存在”)
如果结束
端接头
“公共变量
私有结构客户
公共名作为字符串
公共LastName作为字符串
作为字符串的公共电子邮件
十进制公用电话
'姓名=姓和名吗
作为字符串的公共只读属性名()
得到
返回FirstName&&&LastName
结束
端属性
'在列表框中显示正确覆盖默认ToString函数的客户
Public将函数ToString()重写为字符串
返回名称
端函数
端部结构
'声明并连接到类型Customer
作为客户的私有对象客户
作为客户的专用OBJNEW客户
'创建客户格式
Private Sub AddCustomer(ByVal firstName作为字符串,ByVal lastName作为字符串,ByVal email作为字符串,ByVal phone作为十进制)
'声明类型为customer的objNewCustomer以供使用
Dim OBJNEW客户作为客户
'将客户的4件事连接到objNewCustomer
objNewCustomer.FirstName=FirstName
objNewCustomer.LastName=LastName
objNewCustomer.Email=电子邮件
objNewCustomer.Phone=电话
'将objNewCustomer添加到myCustomers数组列表中
myCustomers.Add(objNewCustomer)
'将客户名称添加到列表中
listCustomers.Items.Add(objNewCustomer.ToString())
端接头
'避免客户选择错误
私有只读属性SelectedCustomer()作为客户
得到
如果listCustomers.SelectedIndex-1,则
返回CType(我的客户(listCustomers.SelectedIndex),客户)
如果结束
结束
端属性
'启用选定客户
私有子列表客户\u SelectedIndexChanged(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理列表客户。SelectedIndexChanged
显示客户(SelectedCustomer)
端接头
'加载客户的信息
专用子显示客户(ByVal cust作为客户)
txtName.Text=cust.Name
txtFirstName.Text=cust.FirstName
txtLastName.Text=cust.LastName
txtEmail.Text=cust.Email
txtPhone.Text=cust.Phone
端接头
'添加用户(弹出新窗口)
私有子btnAdd_Click(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理btnAdd。单击
表格2.Show()
'System.IO.File.WriteAllText(“C:\test.txt”,Textbox1.Text)
端接头
“工作
Private Sub btnTotal_Click(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理btnttal。单击

txtotal.Text=myCustomers.Count.ToString() 端接头 '私有Total2为整数 "试验, '专用子区总计(ByVal总计为整数) ”“是吗 “”total2=myCustomers.Count.ToString() “”txtTotal.Text=total2 'txtTotal.Text=Total '系统.线程.线程.睡眠(5000) '循环 '末端接头 私有子列表Total_SelectedIndexChanged(ByVal sender作为System.Object,ByVal e作为System.EventArgs) 总计=我的客户数 “不管用” 做 'total2=myCustomers.Count.ToString() 'txtTotal.Text=total2
txtotal.Text=myCustomers.Count.ToString() 系统线程线程睡眠(5000) 环 端接头 私有合计为整数 “不管用” 私有子btnSave_单击(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理btnSave。单击 Dim writer作为新IO.StreamWriter(文件名) 尝试 作者:写(“”) writer.Close() 特例 MessageBox.Show(例如Message) 最后 writer.Close() 结束尝试 端子端类

感谢您提供的任何帮助/提示,如果您发布了更正的代码块,请解释其工作原理/错误所在,谢谢。

您试图使用流读取器作为
AddCustomer
函数的参数-这将不起作用。事实上,您根本不能以声明的方式使用
StreamReader
——您必须实例化一个这样的:

 Dim sr as New StreamReader
 Dim line as String
 line = sr.ReadLine()
等等。有关更多详细信息,请参阅

在任何情况下,如果您试图从逗号分隔的文件中读取客户数据,例如:

 Dim sr as New StreamReader
 Dim line as String
 line = sr.ReadLine()
C:\test.text---包含:

Sam, Bond, sambond@msn.com, 9541032163
Merry, Jackson, merryjackson@msn.com, 8872101103
Rachel, Smith, rachelsmith@msn.com, 4839078565
然后您必须执行类似的操作来读取中的值--注意,我们必须以声明函数的方式(即:四个字符串!)将参数传递给
AddCustomer

文本字段解析器读取字符串数组并自动执行sep
Rdr.Delimiters = New String() {"vbTab"}
Dim myCustomers As New List(Of Customer)
Do
    'total2 = myCustomers.Count.ToString()
    'txtTotal.Text = total2
    txtTotal.Text = myCustomers.Count.ToString()
    System.Threading.Thread.Sleep(5000)
Loop
Do while x < 10
   x = x + 1
   DoSomething()
Loop
txtTotal.Text = myCustomers.Count.ToString()