0

我正在尝试编写一个从访问文件中获取数据的程序,然后我可以点击一个按钮并在文本文件中为其创建一个标签。现在,如果我点击按钮为多个标签创建标签,它只会显示最后一个被选中的标签。我想,我需要一个循环outfile.write's,我只是不确定要使用什么循环。它没有给我任何错误或任何东西,它只是没有在这个文本文件中打印一个以上的标签。另外,我如何在例如人名和姓氏之间放置空格,因为它们在同一行?到目前为止,这是我的代码。

Imports System.IO

Public Class frmAccess
    Dim outFile As StreamWriter

    Private Sub TblMemberBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TblMemberBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.TblMemberBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.GolfDataSet)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'GolfDataSet.tblMember' table. You can move, or remove it, as needed.
        Me.TblMemberTableAdapter.Fill(Me.GolfDataSet.tblMember)



    End Sub


    Private Sub cmdCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreate.Click
        Dim outFile As StreamWriter
        outFile = File.CreateText("Labels.txt")

        outFile.WriteLine()
        outFile.WriteLine()
        outFile.Write(lblFirst.Text, " ")
        outFile.WriteLine(lblLast.Text)
        outFile.WriteLine(lblAddress.Text, " ")
        outFile.Write(lblCity.Text, ", ")
        outFile.Write(lblState.Text, " ")
        outFile.Write(lblZip.Text)
        outFile.WriteLine()
        outFile.Close()

    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click

        Me.Close()

    End Sub
End Class

例如,我的文本文件应如下所示(xxx= 空行):

xxxx

xxxxx

鲍勃·史密斯

123修复路

佛罗里达州坦帕市 12345

xxxxxx

xxxxxx

xxxxxx

简·多伊

987帮助巷

科罗拉多州丹佛 56789

xxxxx

xxxxx

因此,我的文本文件不会显示两个标签,而只会显示 Jane Doe 的标签。

4

3 回答 3

0

对于空间,您可以使用 Strings.Space(numOfSpaces) 方法,也许它可以在写入文件之前附加到 firstname 变量。至于最初的问题,我不明白你到底要问什么……你能改写吗?也希望你没有绑定任何文本框

于 2013-04-13T05:21:41.330 回答
0

你可以使用以下

Private Sub cmdCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreate.Click
    Dim outFile As StreamWriter
    outFile = File.CreateText("Labels.txt")

    outFile.WriteLine()
    outFile.WriteLine()
    outFile.Write(lblFirst.Text & " ")
    outFile.WriteLine(lblLast.Text)
    outFile.WriteLine(lblAddress.Text & " ")
    outFile.Write(lblCity.Text & " " & ", ")
    outFile.Write(lblState.Text & " ")
    outFile.Write(lblZip.Text)
    outFile.WriteLine()
    outFile.Close()

End Sub
于 2013-04-13T05:29:30.240 回答
0

尝试使用此代码显示所有姓名和地址详细信息

Private Sub cmdCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreate.Click

 Dim outFile As StreamWriter
        If File.Exists("D:\sathish\Labels.txt") Then
            outFile = New StreamWriter("Labels.txt", True)
        Else
            outFile = New StreamWriter("Labels.txt")
        End If


        outFile.WriteLine()
    outFile.WriteLine()
    outFile.Write(lblFirst.Text & " ")
    outFile.WriteLine(lblLast.Text)
    outFile.WriteLine(lblAddress.Text & " ")
    outFile.Write(lblCity.Text & " " & ", ")
    outFile.Write(lblState.Text & " ")
    outFile.Write(lblZip.Text)
    outFile.WriteLine()
    outFile.Close()

End Sub
于 2013-04-13T06:11:14.737 回答