Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从richbox中删除空行_C# - Fatal编程技术网

C# 如何从richbox中删除空行

C# 如何从richbox中删除空行,c#,C#,我的richbox中有几行空白,我需要从richbox中删除它们 我可以处理空白行,但不能删除它们。我怎么做?我使用了此代码,但不起作用: RichTextBox rtfBox = new RichTextBox(); rtfBox.Rtf = SOME NTEXT DATA; int lineNumber = 0; foreach (string a in rtfBox.Lines) { if (a == "") { int start_index = rtf

我的richbox中有几行空白,我需要从richbox中删除它们

我可以处理空白行,但不能删除它们。我怎么做?我使用了此代码,但不起作用:

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++;
}
RichTextBox rtfBox=new RichTextBox();
rtfBox.Rtf=一些NTEXT数据;
int lineNumber=0;
foreach(rtfBox.line中的字符串a)
{
如果(a==“”)
{
int start_index=rtfBox.GetFirstCharIndexFromLine(行号);
int countLineChar=rtfBox.Lines[lineNumber].Length;
//吃生菜
if(线号
此行
rtfBox.Text=rtfBox.Text.Remove(开始索引,countLineChar)不工作

谢谢

更新

谢谢大家,当richbox内容只是文本时,您的建议很有用,但我的richbox中也有图像和表格。当我使用rtb.Text=或rtfBox.Text=或richTextBox1.Text=时,图像和表格将从richbox中删除。

我不知道是否有一种简单的方法可以一步完成。您可以使用富文本框的.Text属性上的.Split函数来获取行数组

string[] lines = richTextBox1.Text.Split( "\n".ToCharArray() );
然后在删除所需的行并将其复制回富文本框的.text属性后,编写一些内容将数组重新组合为单个文本字符串

下面是一个简单的例子:

            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;
string[]line=richTextBox1.Text.Split(“\n.ToCharArray());
int lineToDelete=2//基于O的行号
string richText=string.Empty;
对于(int x=0;x

如果您的富文本框的行数超过10行左右,最好使用StringBuilder而不是字符串来编写新文本。

很难说清楚您在做什么,但看起来您正在外foreach中的行上循环,并查看
rtfBox.lines
,查看这一行是否正确是我不知道。。。这部分很奇怪。然后您正在更改
rtfBox.Text

即使你的内在逻辑是正确的,这也行不通。设置rtfBox.Text时,您会更改正在循环的项目。然后你继续写一份原件,这无关紧要

我建议你不要这样做。。。不用担心行集合,只需根据需要修改文本即可。这里有一个建议(我没有测试)

甚至更好

rtfBox.Text = refBox.Text.Replace(Environement.NewLine+Environement.NewLine,Environement.NewLine);
你可能必须把这些放在一个循环中,直到你找不到匹配的

比如说

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.

这将匹配任何包含新行字符之一的行,可选地以任何空格开头

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

(与其他文本一样&Replace这将中断任何格式设置(如果有)

这将删除文本中的所有行:

string value = Regex.Replace(someText, @"(\n|\r)", "", RegexOptions.Multiline);

我也有同样的问题。我的Windows窗体上有一个rtb,用于通过复制/粘贴输入信息。如果用户碰巧得到一个空行并将该数据粘贴到rtb中。如何清理rtb,使其只包含有用的数据行。我的过程有点不同

                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;

如果您需要提供更多信息,请编辑您的问题。此外,尽量不要在posts.rtfBox.Rtf=Regex.Replace(rtfBox.Rtf,@“\\par(\n | \r | \r\n)”,“”,RegexOptions.Multiline)中使用“text speak”缩写;
                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;