简体   繁体   English

如何将多个文本框字段保存到VB.Net中的.txt文件

[英]How do I save multiple Textbox fields to a .txt file in VB.Net

So I have four textbox fields within a VB.Net Windows Form Application and when data is entered, I would like the data items to be saved as a .txt file with the file name to be the data input of the second textbox. 因此,我在VB.Net Windows窗体应用程序中有四个文本框字段,输入数据后,我希望将数据项另存为.txt文件,并将文件名作为第二个文本框的数据输入。

So far my code looks like this: 到目前为止,我的代码如下所示:

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

    If txtName.Text And txt_sID.Text = "" Then
        MsgBox("Please complete ALL fields before submitting!")
    End If
    **My.Computer.FileSystem.WriteAllText(file:=txt_sID.Text, txtName.Text, txt_sID.Text, txtScore.Text, txtQuiz.Text, False)**
End Sub

With the starred code the focus of this thread and the rest of the code for context. 使用加星标的代码时,该线程将重点放在上下文中,其余代码将用于上下文。

Thanks in advance 提前致谢

Dim sb As StringBuilder = New StringBuilder()

sb.Append(txt1.text)
sb.Append(txt2.text)
sb.Append(txt3.text)
sb.Append(txt4.text)

System.IO.File.WriteAllText(FilePath, sb.ToString())

The following is conceptual as I used standard text box names. 以下是概念性的,因为我使用了标准文本框名称。

If Not String.IsNullOrWhiteSpace(TextBox1.Text) AndAlso Not String.IsNullOrWhiteSpace(TextBox2.Text) Then
    If Not String.IsNullOrWhiteSpace(TextBox3.Text) Then
        If IO.Directory.Exists(IO.Path.GetDirectoryName(TextBox3.Text)) Then
            Dim sb As New System.Text.StringBuilder
            sb.AppendLine(TextBox1.Text)
            sb.AppendLine(TextBox2.Text)
            IO.File.WriteAllText(TextBox3.Text, sb.ToString)
        End If
    Else
        MessageBox.Show("Please provide a file name")
    End If
Else
    MessageBox.Show("Please complete all fields")
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM