【问题标题】:RichTextBox add some text in the middle of the textRichTextBox 在文本中间添加一些文本
【发布时间】:2013-03-27 21:39:51
【问题描述】:

我有一个RichTextBox,我想在textmiddle 中添加add some text。 例如我得到了这个文本:

“第一文本第二文本”

我想在"FirstText" and the "SecondText". 之间添加一些文本 我尝试split the text to 2 strings 并添加到第一个我的额外文本然后添加他的第二个字符串。它有效,但它破坏了我的richTextBox1.SelectionColor (I got color...). 那么如何在不剪切richTextBox1.Text 的情况下添加文本或如何保存所有颜色数据?

【问题讨论】:

  • 你是怎么设置颜色的?

标签: c# forms richtextbox


【解决方案1】:

你必须自己找到起始索引:

int index = richTextBox1.Text.IndexOf(" ");
if (index > -1) {
  richTextBox1.Select(index, 1);
  richTextBox1.SelectedText = " Inserted Text ";
}

【讨论】:

    【解决方案2】:

    你熟悉起始位置和结束位置吗?你可以简单地做这样的事情

    richTextBox1.SelectionStart = index;
    richTextBox1.SelectionLength = length;//you need to assign an integer where to start
    richTextBox1.SelectedText =  "Good"; 
    

    它将用“好”一词替换文本中您指定长度的任何位置

    【讨论】:

      【解决方案3】:

      检查这个post

      您可能需要将 SelectionStart 的值更改为要放置新文本的位置。

      如果你需要找到正确的索引,你可以使用这样的东西:

          startIndex = richTextBox1.Find(expressionToFind, 0,
                                  richTextBox1.Text.Length,
                                  RichTextBoxFinds.WholeWord);
      

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-12
        • 2014-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-15
        • 2020-03-27
        相关资源
        最近更新 更多