Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在不选择RichTextBox的情况下恢复插入符号位置或更改文本颜色_C#_Winforms_Visual Studio 2013_Richtextbox - Fatal编程技术网

C# 如何在不选择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()

这一直是我的许多应用程序的一个问题,我不知道为什么Windows没有一个优雅的解决方案

我正在VS2013的.NET4.5中使用Winforms

例如,我想更改多行RichTextBox中一行文本的颜色

为此,我需要使用如下方式设置选择

rtb.Select(rtb.GetFirstCharIndexFromLine(r), str.Length);
然后,我会使用

rtb.SelectionColor = Color.Red;
大概,用以下命令取消选择:

rtb.DeselectAll();
现在问题是光标/插入符号已移回行的开头

我试图通过保存以前的插入符号位置来修复它

rtb.CaretPosition
然而,CaretPosition并不是RichTextBox的一种方法,在任何在线的地方,这都是每个人使用的主要方法

我尝试将PresentationFramework添加到我添加的引用和代码中

using System.Windows.Framework;
正如这里所建议的:

但是我仍然没有看到CaretPosition属性,只有ScrollToCaret()方法

我的两个问题是:

  • 如何在RichTextBox中获取CaretPosition属性

  • 如何在不使用选择和影响插入符号位置的情况下更改文本颜色,必须编写复杂的逻辑来为用户恢复

  • 我的应用程序检查序列号,每行一个,如果它们与格式不匹配,则以红色突出显示,如下所示

    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方法时,我也会做同样的事情。至少我希望它滚动到文本的末尾,或者滚动到最后键入的字符的末尾。