Reputation: 23
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.
Upvotes: 2
Views: 10520
Reputation: 7
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();
}
Upvotes: 0
Reputation: 336
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.
Upvotes: 0
Reputation: 822
This will remove all the lines from the text:
string value = Regex.Replace(someText, @"(\n|\r)", "", RegexOptions.Multiline);
Upvotes: 0
Reputation: 1
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;
Upvotes: 0
Reputation: 175766
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)
Upvotes: 6
Reputation: 70523
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.
Upvotes: 0