2013-03-27 84 views
0

我有一个RichTextBox,我想要add some texttextmiddle。 比如我有这样的文字:RichTextBox在文本中间添加一些文本

“使用firstText SecondText”

我想补充的"FirstText" and the "SecondText". 之间的一些文字我试图split the text to 2 strings,并添加到第一我额外的文本,然后把他加第二个字符串。它的工作,但它是摧毁我的richTextBox1.SelectionColor (I got color...). 那么,如何添加文本,而不削减我richTextBox1.Text或如何保存所有的颜色数据?

+0

你是如何设置的颜色? – 2013-03-27 13:32:49

回答

0

选中此post

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

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

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

希望它能帮助。

1

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

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

Thx它的工作:) – user2203448 2013-03-27 14:56:15

1

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

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

它将取代任何文字位置,你已经指定长度字“好”