views:

204

answers:

6

on a windows form, i have RichTextBox, with some text, in several lines. and one button on a form.

i wolud like when i click on that button, to join all richtextbox of lines in one line, but not to loose text style (like font family, color, etc.)

i can not do it with Replace, like \r\n, and not with the Replace(Environment.NewLine, "")........ :-((

i have also tryed to replace \par and \pard, but still no luck.......

please help!!!

A: 

The TextBox control has its own methods for finding and replacing text. Take a look at this article (it's VB.NET but I hope you will get the idea): http://www.codeproject.com/KB/vb/findandriplace_rtb.aspx

Konamiman
A: 

I bet you're just calling Replace on the text string, what you need to do is something like this:

richTextBox1.Text = richTextBox1.Text.Replace(Environment.NewLine, "");

The key here is that you need to assign the result of the function to the text of the rich text box, otherwise nothing happens. See, strings are immutable and whenever you perform an operation on one you must assign the result of the operation to something (even the original variable will work) otherwise nothing happens.

RCIX
You will lose the formatting here.
Philip Wallace
A: 

richTextBox1.Text = richTextBox1.Text.Replace(Environment.NewLine, "");

this one is not ok, because with that loose font definitions (color, bold, undesline etc).

ok, again to be more specific...

i have RichTextBox control, with 4 lines of text:

line 1
line 2
line 3
line 4

line 3 is colored red.

i need to get following:

line 1 line 2 line 3 line 4

(and that "line 3" be red as it was before).

when i try with

richTextBox1.Text = richTextBox1.Text.Replace(Environment.NewLine, " ");

... i get:

line 1
line 2   
line 34

"line 2" is colored red.

what do i have to do, to solve this problem?

nikola
BTW, it's generally best to edit this sort of thing into your question and not post it as a separate answer.Quite an odd bug, in that case i would recommend attempting to use the suggestions that Knonamiman gave you.
RCIX
A: 

hi, I think this help you:

StringBuilder strbld = new StringBuilder();

for (int i = 0; i < this.richTextBox1.Text.Length; i++)
{
   char c = this.richTextBox1.Text[i];

   if (c.ToString() != "\n")
      strbld.Append(c);
}

MessageBox.Show(strbld.ToString());

ok, ChrisF is right. How about this:

string strRtf = richTextBox1.Rtf.Replace("\\par\r\n", " ");
strRtf = strRtf.Replace("\\line", " ");
richTextBox2.Rtf = strRtf;

:-|

Amir Borzoei
This will just concatenate the text, and strip the formatting
ChrisF
+1  A: 

This will work:

        // Create a temporary buffer - using a RichTextBox instead
        // of a string will keep the RTF formatted correctly
        using (RichTextBox buffer = new RichTextBox())
        {
            // Split the text into lines
            string[] lines = this.richTextBox1.Lines;
            int start = 0;

            // Iterate the lines
            foreach (string line in lines)
            {
                // Ignore empty lines
                if (line != String.Empty)
                {
                    // Find the start position of the current line
                    start = this.richTextBox1.Find(line, start, RichTextBoxFinds.None);

                    // Select the line (not including new line, paragraph etc)
                    this.richTextBox1.Select(start, line.Length);

                    // Append the selected RTF to the buffer
                    buffer.SelectedRtf = this.richTextBox1.SelectedRtf;

                    // Move the cursor to the end of the buffers text for the next append
                    buffer.Select(buffer.TextLength, 0);
                }
            }

            // Set the rtf of the original control
            this.richTextBox1.Rtf = buffer.Rtf;
        }
Philip Wallace
If this proved to be the answer, can you mark it as so?
Philip Wallace
A: 

Philip, THANK YOU!!!!

you saved my bacon... :-)

p.s. thank you all also, because this was realy big one.

Glad to help! Just remember to select it as your answer... ;)
Philip Wallace
FYI, this should really be a comment on either my answer or your original question.Oh, and welcome to SO!
Philip Wallace