Add text to RichRextBox

Chong

Regular
Joined
Apr 4, 2003
Messages
79
Is there a way to add texts before a RichTextBox file load? Here's my situation. I have a string variable that holds a bunch of data in it. Below is how I load the file into the RichTextBox.

richTextBox1.LoadFile(filePath & strFileName, RichTextBoxStreamType.RichText)

However, I want to add the string variable to the RichTextBox along with the data that comes from the file load. Another word, I want to combine the two and store into one richtextbox. Is there a way to do this?

Any help is greatly appreciated!

ljCharlie
 
I think you'll have to load the string manually and then shove your original string plus the loaded text in to the RichTextbox yourself.

You could also add the string to the front of the textbox after you load the file, but you might notice a visual glitch. Not sure if there's a way to suspend drawing on the RTB while adding text or not.

-Nerseus
 
Thank you very much for the response. I think I like your first idea better. However, how do I load the string plus the text file both into the RTB? The string variable is not store in any database or file....it's created during run time.

ljCharlie
 
There are lots of ways to read textfiles. I like the following:
C#:
StreamReader reader = File.OpenText(@"c:\file.txt");
string stringFromFile = reader.ReadToEnd();

Then you can set the RTB's rtf or Text property (I forget) to your two strings, appended together, like:
rtb.Text = myString + stringFromFile;

-Nerseus
 
You could try this:
C#:
richTextBox1.LoadFile(filePath & strFileName, RichTextBoxStreamType.RichText);
richTextBox1.SelectionStart = richTextBox1.SelectionLength;
richTextBox1.SelectedText = myStringVar
 
Back
Top