Windows Develop Bookmark and Share   
 index > Windows Forms General > How can I add lines in a richTextBox in C# ????
 

How can I add lines in a richTextBox in C# ????

Hi, I´m working Visual C# .NET 1.1

I have got a richTextBox control with 4 lines:

Line 1

Line 2

Line 3

Line 4

Now I want go add one line between Line 1 and Line 2 for example.

Line 1

New Line

Line 2

Line 3

Line 4

But I don´t know how I can do it.

I have tried with

richTextBox1.Lines[1].Insert

It is possible to add a new lines between others ????? Any example ?????

Any help ????

Thanks in advance !!!!!!

coloso  Wednesday, April 26, 2006 9:58 AM

Hi,

I don't know if there's a shorter solution but here's something you could try. I wrote this code after seeing your predicament and it worked out here.

string[] tempArray = new string[this.richTextBox1.Lines.Length];
string[] tempArray2 = new string[this.richTextBox1.Lines.Length +1];
tempArray = this.richTextBox1.Lines;

int count=0;
tempArray2[count] = tempArray[0];
count++;
tempArray2[count] = "New Line";
count++;
for (int counter = 2; counter < tempArray.Length; counter++)
{
tempArray2[counter]=tempArray[counter];
}

string strfn;
strfn = Convert.ToString(DateTime.Now.ToFileTime());
StreamWriter fs = new StreamWriter(strfn, true);
foreach(string s in tempArray2)
fs.WriteLine(s);
fs.Flush();
fs.Close();
string line;
this.richTextBox1.Clear ();
System.IO.StreamReader file = new System.IO.StreamReader(strfn);
while((line = file.ReadLine()) != null)
{
this.richTextBox1.AppendText(line+"\n");
}

What this code essentially doesisretrieve the contents of the rich textbox as a string array and then inserts a desired line at a given position (you can customize the above code to changethe string andthe position). Then it writes the string array to atemporary file and reads from thefile line by line into the rich text box. Am sure there must be some workaroundbetter than using temporary files but for now this is all I could find.

Hope that helps,

-M

Mamta M  Wednesday, April 26, 2006 12:11 PM

Hi,

I don't know if there's a shorter solution but here's something you could try. I wrote this code after seeing your predicament and it worked out here.

string[] tempArray = new string[this.richTextBox1.Lines.Length];
string[] tempArray2 = new string[this.richTextBox1.Lines.Length +1];
tempArray = this.richTextBox1.Lines;

int count=0;
tempArray2[count] = tempArray[0];
count++;
tempArray2[count] = "New Line";
count++;
for (int counter = 2; counter < tempArray.Length; counter++)
{
tempArray2[counter]=tempArray[counter];
}

string strfn;
strfn = Convert.ToString(DateTime.Now.ToFileTime());
StreamWriter fs = new StreamWriter(strfn, true);
foreach(string s in tempArray2)
fs.WriteLine(s);
fs.Flush();
fs.Close();
string line;
this.richTextBox1.Clear ();
System.IO.StreamReader file = new System.IO.StreamReader(strfn);
while((line = file.ReadLine()) != null)
{
this.richTextBox1.AppendText(line+"\n");
}

What this code essentially doesisretrieve the contents of the rich textbox as a string array and then inserts a desired line at a given position (you can customize the above code to changethe string andthe position). Then it writes the string array to atemporary file and reads from thefile line by line into the rich text box. Am sure there must be some workaroundbetter than using temporary files but for now this is all I could find.

Hope that helps,

-M

Mamta M  Wednesday, April 26, 2006 12:11 PM
Hi,

Were you able to add lines in RTB?

Thank you,
Bhanu.
Bhanu Prakash Nunna - MSFT  Wednesday, May 03, 2006 6:59 PM

Thank you for your help. It´s working.....

coloso  Tuesday, May 09, 2006 8:38 AM

Hi

This one is another simplest way

string data = "Line1"+System.Environment.NewLine +"Line2"+System.Environment.NewLine +"Line3"+System.Environment.NewLine

-thanks

Mahesh Kannan  Tuesday, May 09, 2006 9:16 AM

This is another simple way

rtextbox.Text = "Sample Data1 \n Sample Data2 \n\r";

VenkatPV  Thursday, May 18, 2006 7:17 PM
This simple method will not change your text formats (fonts, colors, etc.):
this.myRichTextBox.AppendText("Text");
If you simply use:
this.myRichTextBox += "Text";
then all of the previous added text will change to default font and color.
SpyroTheDragon  Monday, December 15, 2008 7:36 AM

You can use google to search for other answers

Custom Search

More Threads

• Visual Studio 'Publish Wizard' Issues
• Help with classes
• Detect the object under the mouse
• how to color a whole row of a datagridview at runtime
• Need shapes or transparent images, can't use either
• how to invoke a windows control on a form from another class?
• Acrobat Nightmares! Need help printing
• What's the difference between the System.Windows.Forms.Timer class and the System.Timers.Timer class?
• Theme override & mp3 visualizations
• PreFilterMessage of MessageFilter doesn't capture F10 key