none
TextBox To Text File

    Question

  • Hi All,

    I have 2 textbox then want to write to Text file as showing like bellow;

    99  (43)   9643 , ( 25, 925)= 29
    88  (25)   2145 , ( 98, 987)= 24

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            TextBox1.Text = "99   (43)   9643" & vbCrLf & "88   (25)   2145"
            TextBox2.Text = "( 25, 925)= 29" & vbCrLf & "( 98, 987)= 24"
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            My.Computer.FileSystem.WriteAllText("C:\Users\family\Documents\TwoDigit_.txt", Me.TextBox1.Text + Me.TextBox2.Text, False)
        End Sub

    thank

    Tuesday, March 14, 2017 2:00 PM

Answers

  • Assuming both textboxes have the same number of line, loop the first textbox's lines and combine them with the second textbox's line at the same position and then write the resulting line to your output file.  Something like:

    Using writer As New IO.StreamWriter("C:\Users\family\Documents\TwoDigit_.txt")
       For i = 0 To TextBox1.Lines.Count -1
          writer.WriteLine(TextBox1.Lines(i) & ", " & TextBox2.Lines(i))
       Next
    End Using


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Tuesday, March 14, 2017 2:34 PM
    Moderator
  • Hi All,

    I have 2 textbox then want to write to Text file as showing like bellow;

    99  (43)   9643 , ( 25, 925)= 29
    88  (25)   2145 , ( 98, 987)= 24

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            TextBox1.Text = "99   (43)   9643" & vbCrLf & "88   (25)   2145"
            TextBox2.Text = "( 25, 925)= 29" & vbCrLf & "( 98, 987)= 24"
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            My.Computer.FileSystem.WriteAllText("C:\Users\family\Documents\TwoDigit_.txt", Me.TextBox1.Text + Me.TextBox2.Text, False)
        End Sub

    thank

    Hi

    Reed was quicker than me. Anyway, here is my contribution. This caters for differing line counts in each TextBox.

    NOTE: I use the ampersand to concatenate strings rather than the +

    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Dim FilePath As String = "C:\Users\lesha\Desktop\TestFolder\TwoDigit_.txt"
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    
            TextBox1.Text = "99   (43)   9643" & vbCrLf
            TextBox1.AppendText("76   (33)   2222")
    
            TextBox2.Text = "( 25, 925)= 29" & vbCrLf
            TextBox2.AppendText("( 98, 987)= 24")
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim lst As New List(Of String)
            Dim c1 As Integer = TextBox1.Lines.Count
            Dim c2 As Integer = TextBox2.Lines.Count
            If c1 < 1 AndAlso c2 < 1 Then Exit Sub
    
            IO.File.Delete(FilePath)
    
            Select Case c1
                Case Is = c2
                    For line As Integer = 0 To c1 - 1
                        My.Computer.FileSystem.WriteAllText(FilePath, TextBox1.Lines(line) & "," & TextBox2.Lines(line) & vbCrLf, True)
                    Next
                Case Is < c2
                    For line As Integer = 0 To c1 - 1
                        My.Computer.FileSystem.WriteAllText(FilePath, TextBox1.Lines(line) & "," & TextBox2.Lines(line) & vbCrLf, True)
                    Next
    
                    ' for extra lines beyond TextBox1 line count
                    For line As Integer = c1 To c2 - 1
                        My.Computer.FileSystem.WriteAllText(FilePath, "*******" & "," & TextBox2.Lines(line) & vbCrLf, True)
                    Next
                Case Else
                    For line As Integer = 0 To c2 - 1
                        My.Computer.FileSystem.WriteAllText(FilePath, TextBox1.Lines(line) & "," & TextBox2.Lines(line) & vbCrLf, True)
                    Next
    
                    ' for extra lines beyond TextBox2 line count
                    For line As Integer = c2 To c1 - 1
                        My.Computer.FileSystem.WriteAllText(FilePath, "," & TextBox1.Lines(line) & "," & "*******" & vbCrLf, True)
                    Next
            End Select
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by mipakteh Wednesday, March 15, 2017 1:40 PM
    Tuesday, March 14, 2017 3:02 PM

All replies

  • Assuming both textboxes have the same number of line, loop the first textbox's lines and combine them with the second textbox's line at the same position and then write the resulting line to your output file.  Something like:

    Using writer As New IO.StreamWriter("C:\Users\family\Documents\TwoDigit_.txt")
       For i = 0 To TextBox1.Lines.Count -1
          writer.WriteLine(TextBox1.Lines(i) & ", " & TextBox2.Lines(i))
       Next
    End Using


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Tuesday, March 14, 2017 2:34 PM
    Moderator
  • Hi All,

    I have 2 textbox then want to write to Text file as showing like bellow;

    99  (43)   9643 , ( 25, 925)= 29
    88  (25)   2145 , ( 98, 987)= 24

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            TextBox1.Text = "99   (43)   9643" & vbCrLf & "88   (25)   2145"
            TextBox2.Text = "( 25, 925)= 29" & vbCrLf & "( 98, 987)= 24"
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            My.Computer.FileSystem.WriteAllText("C:\Users\family\Documents\TwoDigit_.txt", Me.TextBox1.Text + Me.TextBox2.Text, False)
        End Sub

    thank

    Hi

    Reed was quicker than me. Anyway, here is my contribution. This caters for differing line counts in each TextBox.

    NOTE: I use the ampersand to concatenate strings rather than the +

    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Dim FilePath As String = "C:\Users\lesha\Desktop\TestFolder\TwoDigit_.txt"
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    
            TextBox1.Text = "99   (43)   9643" & vbCrLf
            TextBox1.AppendText("76   (33)   2222")
    
            TextBox2.Text = "( 25, 925)= 29" & vbCrLf
            TextBox2.AppendText("( 98, 987)= 24")
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim lst As New List(Of String)
            Dim c1 As Integer = TextBox1.Lines.Count
            Dim c2 As Integer = TextBox2.Lines.Count
            If c1 < 1 AndAlso c2 < 1 Then Exit Sub
    
            IO.File.Delete(FilePath)
    
            Select Case c1
                Case Is = c2
                    For line As Integer = 0 To c1 - 1
                        My.Computer.FileSystem.WriteAllText(FilePath, TextBox1.Lines(line) & "," & TextBox2.Lines(line) & vbCrLf, True)
                    Next
                Case Is < c2
                    For line As Integer = 0 To c1 - 1
                        My.Computer.FileSystem.WriteAllText(FilePath, TextBox1.Lines(line) & "," & TextBox2.Lines(line) & vbCrLf, True)
                    Next
    
                    ' for extra lines beyond TextBox1 line count
                    For line As Integer = c1 To c2 - 1
                        My.Computer.FileSystem.WriteAllText(FilePath, "*******" & "," & TextBox2.Lines(line) & vbCrLf, True)
                    Next
                Case Else
                    For line As Integer = 0 To c2 - 1
                        My.Computer.FileSystem.WriteAllText(FilePath, TextBox1.Lines(line) & "," & TextBox2.Lines(line) & vbCrLf, True)
                    Next
    
                    ' for extra lines beyond TextBox2 line count
                    For line As Integer = c2 To c1 - 1
                        My.Computer.FileSystem.WriteAllText(FilePath, "," & TextBox1.Lines(line) & "," & "*******" & vbCrLf, True)
                    Next
            End Select
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by mipakteh Wednesday, March 15, 2017 1:40 PM
    Tuesday, March 14, 2017 3:02 PM
  • thank,Reed

    Wednesday, March 15, 2017 1:39 PM
  • Very good .

    Thank Leshay

    Wednesday, March 15, 2017 1:40 PM