|
-
Sep 23rd, 2022, 10:22 AM
#1
Thread Starter
New Member
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
-
Sep 23rd, 2022, 10:27 AM
#2
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.
-
Sep 23rd, 2022, 10:28 AM
#3
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.
-
Sep 23rd, 2022, 10:33 AM
#4
Thread Starter
New Member
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.
-
Sep 23rd, 2022, 10:39 AM
#5
Re: Problem Writing to Multi-Line Textbox and Streamwriter at Same Time
TextBox1.AppendText("Read the error message")
-
Sep 23rd, 2022, 10:49 AM
#6
Thread Starter
New Member
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!
-
Sep 23rd, 2022, 10:50 AM
#7
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.
-
Sep 23rd, 2022, 11:37 AM
#8
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|