如何在富文本框的特殊行中添加文字... 例子: 我想在第 6 行添加“This Text”
只要有电话号码就可以
例子:
string[] lines = richTextBox1.Lines;
lines[6] = "This Text";
richTextBox1.Lines = lines;
我认为你可以使用 .Lines 属性,它是一个字符串[],并且是可读写的......但我认为你必须在第 6 行前面插入空行,如果它们还没有的话.
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);
}
如果您想在第 6 行insert您的文本而不丢失当前文本
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;
一般:
string[] lines = richTextBox1.Lines;
lines[6] = "This Text";
richTextBox1.Lines = lines;
有效。但是你需要先添加这些行,所以首先你必须启动它们。例如:
private void RTBoxSetLines(RichTextBox rtbox, int lines)
{
while(lines > 0) // here you may use type you like
{
rtbox.AppendText(System.Environment.NewLine);
lines--;
}
}
注意命令 rtbox.Clear();也会清除所有行,所以在你清除所有行之后,你必须再次声明这些行。
你能帮忙在文本中插入带数字的行吗:
==========初始文本=== 我的睾丸在这里 1 我的睾丸在这里 2 我的睾丸在这里 3 我的睾丸在这里 4
===插入行后 描述 1 我的睾丸在这里 1 说明 2 我的睾丸在这里 2 描述 3 我的睾丸在这里 3 说明 4 我的睾丸在这里 4
谢谢
我正在尝试这个,但在文本末尾插入:
string[] line = richTextBox1.Lines;
for (int i = 0; i < line.Length; i++)
{
int x = i;
if ((x % 2) == 0) // define odd line
{
string l1 = line[i];
richTextBox2.AppendText(Environment.NewLine + l1);
}
else
{
richTextBox2.Text += "\n";
}
}