VB编程的新手,需要一些建议

时间:2013-04-13 05:00:52

标签: vb.net

我正在尝试编写一个从访问文件中获取数据的程序,然后我可以点击一个按钮并在文本文件中为它创建一个标签。现在,如果我点击按钮创建一个多于一个的标签,它只显示最后一个被选中的标签。我想,我需要一个所有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

Bob Smith

123修路

佛罗里达州坦帕市12345

XXXXXX

XXXXXX

XXXXXX

Jane Doe

987帮助巷

丹佛,科罗拉多州56789

XXXXX

XXXXX

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

3 个答案:

答案 0 :(得分:0)

对于space,你可以使用Strings.Space(numOfSpaces)方法,也许它可以在写入文件之前附加到firstname变量。 至于最初的问题,我不知道你究竟要求什么伙伴......你可以改写一下吗?也希望你没有绑定任何文本框

答案 1 :(得分: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

答案 2 :(得分: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