2013-03-27 43 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"; 

它將取代任何文字位置,你已經指定長度字「好」

相關問題