简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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