C# 如何在不选择RichTextBox的情况下恢复插入符号位置或更改文本颜色
这一直是我的许多应用程序的一个问题,我不知道为什么Windows没有一个优雅的解决方案 我正在VS2013的.NET4.5中使用Winforms 例如,我想更改多行RichTextBox中一行文本的颜色 为此,我需要使用如下方式设置选择C# 如何在不选择RichTextBox的情况下恢复插入符号位置或更改文本颜色,c#,winforms,visual-studio-2013,richtextbox,C#,Winforms,Visual Studio 2013,Richtextbox,这一直是我的许多应用程序的一个问题,我不知道为什么Windows没有一个优雅的解决方案 我正在VS2013的.NET4.5中使用Winforms 例如,我想更改多行RichTextBox中一行文本的颜色 为此,我需要使用如下方式设置选择 rtb.Select(rtb.GetFirstCharIndexFromLine(r), str.Length); 然后,我会使用 rtb.SelectionColor = Color.Red; 大概,用以下命令取消选择: rtb.DeselectAll()
rtb.Select(rtb.GetFirstCharIndexFromLine(r), str.Length);
然后,我会使用
rtb.SelectionColor = Color.Red;
大概,用以下命令取消选择:
rtb.DeselectAll();
现在问题是光标/插入符号已移回行的开头
我试图通过保存以前的插入符号位置来修复它
rtb.CaretPosition
然而,CaretPosition并不是RichTextBox的一种方法,在任何在线的地方,这都是每个人使用的主要方法
我尝试将PresentationFramework添加到我添加的引用和代码中
using System.Windows.Framework;
正如这里所建议的:
但是我仍然没有看到CaretPosition属性,只有ScrollToCaret()方法
我的两个问题是:
private void rtb_TextChanged(object sender, EventArgs e)
{
string pattern = @"[A-Z]{2}[A-Z, 0-9]{2}\d{4}";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
TextReader read = new System.IO.StringReader(rtb.Text);
SerialNumbers.Clear();
int selectStart = 0;
for (int r = 0; r < rtb.Lines.Length; r++)
{
string str = read.ReadLine();
if (str != null)
{
selectStart += str.Length;
MatchCollection matches = rgx.Matches(str);
if (matches.Count == 1)
{
SerialNumbers.Add(str);
}
else
{
rtb.Select(rtb.GetFirstCharIndexFromLine(r), str.Length);
rtb.SelectionColor = Color.Red;
rtb.DeselectAll();
}
}
}
}
private void rtb_TextChanged(对象发送方,事件参数e)
{
字符串模式=@“[A-Z]{2}[A-Z,0-9]{2}\d{4}”;
Regex rgx=新的Regex(模式,RegexOptions.IgnoreCase);
TextReader read=新系统.IO.StringReader(rtb.Text);
SerialNumbers.Clear();
int-selectStart=0;
对于(int r=0;r
您应该使用SelectionCaret
(正如@Mangist在评论中提到的),因为您使用的是WinForms而不是WPF。您引用的MSDN文章只适用于WPF,这与WinForms非常不同
例如,我使用以下方法从WinForms应用程序中的任意位置轻松登录到富文本框:
public static void Log(string text, ref RichTextBox rtbLogBox) {
//
if (text == null) return;
var timestamp = DateTime.Now.ToLongTimeString();
var logtext = string.Format("{0} - {1}\r\n\r\n", timestamp, text);
if (rtbLogBox.InvokeRequired) {
var logBox = rtbLogBox;
logBox.Invoke(new MethodInvoker(delegate {
logBox.AppendText(logtext);
logBox.Update();
logBox.SelectionStart = logBox.Text.Length;
logBox.ScrollToCaret();
}));
} else {
rtbLogBox.AppendText(logtext);
rtbLogBox.Update();
rtbLogBox.SelectionStart = rtbLogBox.Text.Length;
rtbLogBox.ScrollToCaret();
}
}
请注意,将SelectionStart
设置为富文本框中文本的长度后,如何调用ScrollToCaret()
。这解决了添加文本后AppendText
不滚动到底部的“问题”
在您的情况下,您只需在使用突出显示设置文本格式之前保存
SelectionStart
值,然后在完成后将其还原。通过保存SelectionStart position修复了此问题
int selectionStart = SNbox.SelectionStart;
SNbox.Select(SNbox.GetFirstCharIndexFromLine(r), str.Length);
SNbox.SelectionColor = Color.Red;
SNbox.DeselectAll();
SNbox.SelectionStart = selectionStart;
SNbox.SelectionLength = 0;
文档是针对WPF RichTextBox的,您正在使用Windows窗体。抱歉,这似乎没有什么帮助。即使我将SelectionStart与ScrollToCaret结合使用,插入符号也总是移动到我正在键入的文本的开头。当我使用Select方法时,我也会做同样的事情。至少我希望它滚动到文本的末尾,或者滚动到最后键入的字符的末尾。