Reputation: 1151
I'm trying to save a report file to disk by using a RichTextBox but I'm getting an empty file.
ReportRtb.Text = Report & Chr(9) & Source
ReportRtb.SaveFile(FullReportPath, RichTextBoxStreamType.PlainText)
I don't understand what I'm doing wrong. This Function is called from a Thread, Can be related?
Upvotes: 1
Views: 198
Reputation: 1731
Yes this problem is most definitely related to the second thread you use, since you cannot set the Text of a RichTextBox (or any other control on a form) from any other thread but the one it was created from.
Try using the following code.
ReportRtb.Invoke(Sub
ReportRtb.Text = Report & Chr(9) & Source
ReportRtb.SaveFile(FullReportPath, RichTextBoxStreamType.PlainText)
End Sub)
With the Invoke(...)
statement, you can call a lambda (or a delegate sub, but a lambda is easier here) from the thread the control was created on, so this will set the text properly and then write it to the file.
If this does not fix the issue, then please check the content of your Report
and Source
variables with Console.WriteLine(...)
or any other logging / debugging output tool.
If this also does not work, check (with a breakpoint) if the function gets called at all, and if so, where does the code stop to work.
If all of this fails, consider implementing your own saving code, since you only use the RichTextBox to write Plain Text. This could be done much more efficient and quicker using the following code:
Dim sw As New StreamWriter(FullReportPath)
sw.Write(Report & Chr(9) & Source)
sw.Close() 'Never forget that because if you forget it, the file won't be written, too
Upvotes: 1