2012-08-02 39 views
0

我怎麼能在豐富的文本框... 例子的特殊行添加文字: 我想「這個文本」添加到6號線添加豐富的文本框在C#中的某一行文本

+1

你想動態地做到這一點..你在做什麼,即你在用什麼過程來填充富文本框..你熟悉使用列表需要更多的信息提供更好的建議 – MethodMan 2012-08-02 20:34:28

+2

我們是在談論一個WPF (['System.Windows.Controls'](http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.as px))或WinForms(['System.Windows.Forms'](http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox(v = vs.80).aspx)) 'RichTextBox'?請適當標記。 – Adam 2012-08-02 20:37:06

回答

0

我認爲你可以使用.Lines屬性,它是一個字符串[],並且是可讀寫的......但我認爲如果它們不在那裏,那麼你必須在第6行前面插入空行。

2

可以,只要線數可

例子:

string[] lines = richTextBox1.Lines; 
lines[6] = "This Text"; 
richTextBox1.Lines = lines; 
0

正如其他人說,在Windows窗體,你可以做,使用RichTextBox.Lines財產。

WPF RichTextBox這有點棘手:你需要在行的開頭得到一個TextPointer,將它向下移動7行然後返回一個位置,然後在那裏插入文本。像這樣的東西(我不是一個Visual Studio附近,這可能不是編譯):

public static void InsertText(RichTextBox richText, int line, string text) { 
    // Find the position at the end of the specified line. 
    var documentStart = richText.Document.ContentStart; 
    var lineEnd = documentStart.GetLineStartPosition(line + 1) 
         .GetPositionAtOffset(1, LogicalDirection.Backward); 

    // Insert the text there. 
    lineEnd.InsertTextInRun(text); 
} 
0

如果你想插入你的第6行文本沒有鬆動的當前文本

string[] buffer = new string[richTextBox1.Lines.Length+1]; 
Array.Copy(richTextBox1.Lines, 0, buffer, 0, 5); 
buffer[5] = "MyText"; 
Array.Copy(richTextBox1.Lines, 5, buffer, 6, richTextBox1.Lines.Length - 5); 
richTextBox1.Lines = buffer;