说明:我希望将文本行输出到RichTextBox中相同的“位置”,替换前一行。
在C#中,Windows应用程序试图使用RichTextBox显示消息。大多数消息都是附加的,所以这很好,但是在程序中有一个计数器,显示处理的行数。例如:
处理: 001记录。
等
嗯..。我不需要它来填充RichTextBox中这样的数千行代码:
处理: 001记录。 处理: 002记录。
相反,我试图把卡雷特移到行的开头,然后再写一遍。可能需要删除RichTextBox中的前一行。无法理解如何始终在RichTextBox中写入相同的最后一行。
我尝试使用不起作用的SelectionStart和ScrollToCaret()。
发布于 2015-06-17 20:55:21
您可以这样做(rtb是您的RichTextBox变量)
// Get the index of the last line in the richtextbox
int idx = rtb.Lines.Length - 1;
// Find the first char position of that line inside the text buffer
int first = rtb.GetFirstCharIndexFromLine(idx);
// Get the line length
int len = rtb.Lines[idx].Length;
// Select (Highlight) that text (from first to len chars)
rtb.SelectionStart = first;
rtb.SelectionLength = len;
// Replace that text with your update
rtb.SelectedText = "Processed: " + recordCount + " Records.";
没有添加错误处理,但可以添加一些检查以确保保留在文本缓冲区内。
发布于 2015-06-17 20:45:45
一种解决方案是在开始处理之前存储当前文本:
string oldText = richTextBox.Text;
for (int i = 0; i < X; i++)
{
// process stuff
richTextBox.Text = oldText + Environment.NewLine + "Processed: " + i + " Records.";
}
我相信这个方法忽略了RTF数据,所以您可以使用RichTextBox.Rtf
。
https://stackoverflow.com/questions/30902008
复制相似问题