【问题标题】:join lines连接线
【发布时间】:2010-12-10 19:20:34
【问题描述】:

在 Windows 窗体上,我有 RichTextBox,其中包含一些文本,分几行。和表单上的一个按钮。

我希望当我点击那个按钮时,将所有的 Richtextbox 行合并成一行,但不要松散文本样式(如字体系列、颜色等)

我不能用 Replace 来做到这一点,比如 \r\n,而不是用 Replace(Environment.NewLine, "")........ :-((

我也尝试过替换\par和\pard,但还是没有成功......

请帮忙!!!


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

这个不行,因为字体定义很松散(颜色、粗体、下划线等)。

好的,再具体一点……

我有 RichTextBox 控件,有 4 行文本:

line 1
line 2
line 3
line 4

第 3 行是红色的。

我需要得到以下信息:

line 1 line 2 line 3 line 4

(并且“第 3 行”和以前一样是红色的)。

当我尝试使用时

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

...我明白了:

line 1
line 2   
line 34

“第 2 行”为红色。

我该怎么做才能解决这个问题?

【问题讨论】:

  • 对不起,你的问题不是很清楚。替换时会发生什么?你能包括一个你想要的“之前”和“之后”的例子吗?

标签: c# richtextbox paragraph


【解决方案1】:

这将起作用:

        // 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;
        }

【讨论】:

    【解决方案2】:

    TextBox 控件有自己的查找和替换文本的方法。看看这篇文章(它是 VB.NET,但我希望你能明白):http://www.codeproject.com/KB/vb/findandriplace_rtb.aspx

    【讨论】:

      【解决方案3】:

      我打赌你只是在文本字符串上调用 Replace,你需要做的是这样的:

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

      这里的关键是你需要将函数的结果赋给富文本框的文本,否则什么都不会发生。看,字符串是不可变的,每当你对一个字符串执行操作时,你必须将操作的结果分配给某个东西(即使是原始变量也会起作用),否则什么也不会发生。

      【讨论】:

        【解决方案4】:

        我认为这对你有帮助:

        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());
        

        好的,ChrisF 是对的。这个怎么样:

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

        :-|

        【讨论】:

        • 这只会连接文本,并去除格式
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-12
        • 1970-01-01
        相关资源
        最近更新 更多