2

I have several blank line in my richbox and I need to remove them from rich.

I can fine blank lines but can't remove them. how can I do it? I used this code but it didn't work:

RichTextBox rtfBox = new RichTextBox();
rtfBox.Rtf = SOME NTEXT DATA;

int lineNumber = 0;
foreach (string a in rtfBox.Lines)
{
    if (a == "")
    {
        int start_index = rtfBox.GetFirstCharIndexFromLine(lineNumber);
        int countLineChar = rtfBox.Lines[lineNumber].Length;
        // Eat new line chars
        if (lineNumber < rtfBox.Lines.Length - 1)
        {
            countLineChar += rtfBox.GetFirstCharIndexFromLine(lineNumber + 1) -
                ((start_index + countLineChar - 1) + 1);
        }
        rtfBox.Text = rtfBox.Text.Remove(start_index, countLineChar);
    }
    else
        lineNumber++;
}

this line rtfBox.Text = rtfBox.Text.Remove(start_index, countLineChar); don't work.

thanks

Update

Thanks everybody, your suggestion are useful when richbox content is just text, but I have image and table in my rich too. when I use rtb.Text = or rtfBox.Text = or richTextBox1.Text = images and tables will be remove from richbox.

Tim Post
  • 33,371
  • 15
  • 110
  • 174
Bahaareh
  • 23
  • 1
  • 1
  • 3
  • Please just edit your question if you need to supply additional information. Additionally, try not to use 'text speak' abbreviations in your posts. – Tim Post Apr 24 '11 at 13:31

6 Answers6

6

This will match any line containing one of the new line characters, optionaly beginning with any whitespace;

rtb.Text = Regex.Replace(rtb.Text, @"^\s*$(\n|\r|\r\n)", "", RegexOptions.Multiline);

(Like the other Text & Replace this will break any formatting should there be any)

Alex K.
  • 171,639
  • 30
  • 264
  • 288
0

This will remove all the lines from the text:

string value = Regex.Replace(someText, @"(\n|\r)", "", RegexOptions.Multiline);
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Alexei Bondarev
  • 812
  • 7
  • 9
0

I was having the same issue. I have a rich text box (rtb) on my Windows Form, for inputting information by copy/paste. If the user happens to get a blank line(s) and pastes that data into the rtb. How to clean up the rtb, so it only has lines of useful data. I did my process a little different.

                string[] lines = RichTextBox1.Lines.ToArray(); //This is to split the rich text box into an array
                string richText = string.Empty;
                foreach (string line in lines)
                {
                    if (!string.IsNullOrEmpty(line))
                    {//Here is were you re-build the text in the rich text box with lines that have some data in them
                        richText += line;
                        richText += Environment.NewLine;
                    }
                    else
                        continue;
                }
                richText = richText.Substring(0, richText.Length - 2); //Need to remove the last Environment.NewLine, else it will look at it as an other line in the rick text box.
                RichTextBox1.Text = richText;
0

From this question :

I don't know if there is an easy way to do it in one step. You can use the .Split function on the .Text property of the rich text box to get an array of lines

string[] lines = richTextBox1.Text.Split( "\n".ToCharArray() );

and then write something to re-assemble the array into a single text string after removing the line you wanted and copy it back to the .Text property of the rich text box.

Here's a simple example:

            string[] lines = richTextBox1.Text.Split("\n".ToCharArray() );


            int lineToDelete = 2;                   //O-based line number

            string richText = string.Empty;

            for ( int x = 0 ; x < lines.GetLength( 0 ) ; x++ )
            {
                    if ( x != lineToDelete )
                    {
                            richText += lines[ x ];
                            richText += Environment.NewLine;
                    }
            }

            richTextBox1.Text = richText;

If your rich text box was going to have more than 10 lines or so it would be a good idea to use a StringBuilder instead of a string to compose the new text with.

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Saral Doshi
  • 326
  • 1
  • 5
  • 14
0

Hard to tell exactly what you are doing but it looks like you are looping over the lines in the outer foreach and the looking at rtfBox.Lines to see if this one is... I don't know... this part is strange. Then you are changing rtfBox.Text

Even if your inner logic is right, this won't work. When you set rtfBox.Text you change the item you are looping on. Then you continue working on a copy of the original which does not matter.

I suggest you not do it this way... don't worry about the Lines collection just fixup the Text however you want. Here is a suggestion (I did not test)

rtfBox.Text = rtfBox.Text.Replace("\r\n\r\n","\r\n");
rtfBox.Text = refBox.Text.Replace("\n\n","\n");

or even better

rtfBox.Text = refBox.Text.Replace(Environement.NewLine+Environement.NewLine,Environement.NewLine);

You might have to put these in a loop till you find no matches.

For example

string doubleNewLine = Environement.NewLine+Environement.NewLine;
while (rtfBox.Text.Contains(doubleNewLine)
{
   refBox.Text = refBox.Text.Replace(doubleNewLine,Environement.NewLine);
}
// deal with special case of text box starting with newline.
Hogan
  • 69,564
  • 10
  • 76
  • 117
0

Lines convert to List.Read the list backwards and remove empty strings in List.

public void RemoveEmptyLines(RichTextBox richBox)
{
    List<string> Lines= richBox.Lines.ToList();
        //using forr
        for (int i = Lines.Count - 1; i >= 0; i--)
        {
            if (Lines[i].Trim() == string.Empty)
            {
                Lines.RemoveAt(i);
            }
        }
      ` richBox.Lines = Lines.ToArray();
}
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 21 '21 at 00:47