Results 1 to 8 of 8

Thread: Problem Writing to Multi-Line Textbox and Streamwriter at Same Time

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2022
    Posts
    6

    Question Problem Writing to Multi-Line Textbox and Streamwriter at Same Time

    Hello,

    I'm scanning a JPG image file pixel by pixel and writing the results to a streamwriter (output.txt)...This part works perfectly.

    I'm now trying to list the same information to a multi-line textbox (textbox1) but it only lists the last line once it completes.

    I want all of the lines to list in the textbox and scroll as it scans the JPG file.

    Here's my Code:

    Private Sub Pixel_Button_Click(sender As System.Object, e As System.EventArgs) Handles Pixel_Button.Click
    Dim PixelImage As New Bitmap(Picture1.Image, Picture1.Width, Picture1.Height)
    Dim Writer As New System.IO.StreamWriter("C:\Users\Scott\Downloads\output.txt")
    For i As Integer = 0 To PixelImage.Width - 1
    For j As Integer = 0 To PixelImage.Height - 1
    Dim pixel As Color = PixelImage.GetPixel(i, j)
    Writer.WriteLine("Coordinates: " & j & "," & i & " - " & pixel.ToString)
    TextBox1.Text = "Coordinates: " & j & "," & i & " - " & pixel.ToString
    Next j
    Next i
    Writer.Close()
    MessageBox.Show("Pixel Scan Completed!")
    End Sub

    I appreciate any help I can get.

    Thanks

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: Problem Writing to Multi-Line Textbox and Streamwriter at Same Time

    Because inside your nested loops you are assigning a new value to TextBox1.Text each iteration, rather than appending to what is already present in TextBox1.Text.

    You should probably use a StringBuilder object, append text to this StringBuilder object inside the nested loops, and then after the loops complete you assign the value of this StringBuilder object to TextBox1.Text.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem Writing to Multi-Line Textbox and Streamwriter at Same Time

    That's because you keep setting the Text property over and over, thus replacing the existing value. You should be calling the AppendText method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2022
    Posts
    6

    Thumbs up Re: Problem Writing to Multi-Line Textbox and Streamwriter at Same Time

    Thanks...I now understand that I'm overwriting the value each time...

    I used the StringWriter and dumped to textbox after loop completes.

    Works great...Thanks for your help!
    Last edited by System Engineer; Sep 23rd, 2022 at 10:48 AM.

  5. #5
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: Problem Writing to Multi-Line Textbox and Streamwriter at Same Time

    TextBox1.AppendText("Read the error message")

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2022
    Posts
    6

    Re: Problem Writing to Multi-Line Textbox and Streamwriter at Same Time

    Got it...

    I used the StringWriter and dumped to textbox after loop completes.

    Works great...Thanks for your help!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem Writing to Multi-Line Textbox and Streamwriter at Same Time

    OptionBase1 is correct. You could call AppendText on the TextBox but there would be little point because you wouldn't see the result until the end anyway, and it would slow things down. Using the StringBuilder is the right way to go but you should actually take it a step further. Use only the StringBuilder in the loop and then, at the end, call ToString on it to get the entire text as a String. You can then assign that to the Text property of the TextBox but also pass call File.WriteAllText and pass that String to write it all to the file in one go too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2022
    Posts
    6

    Re: Problem Writing to Multi-Line Textbox and Streamwriter at Same Time

    Exactly what I did...Great minds think alike...Thanks!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width