WPF中RichTextBox的光标位置

4

我正在开发一款小型WPF应用程序,其中包含多个选项卡。 底部有一个状态栏; 要求显示光标的行号和列号。 因此,当用户更改光标位置时,行号和列号必须自动更新。 这是我添加RichTextBox的代码; 计算行号和列号的代码位于KeyDown事件处理程序中,但此事件从未被调用。 我应该处理哪个事件来获取光标的行号和列号?

private void AddTabitem(string filePath, mode fileMode)
{
    if (fileMode == mode.openFile)
    {
        if (File.Exists(filePath))
        {
            RichTextBox mcRTB = new RichTextBox();
            mcRTB.KeyDown += new KeyEventHandler(LineNumber);
//rest of the code goes here
        }
    }
}
mcRTB.KeyDown += new KeyEventHandler(LineNumber);

private void LineNumber(object sender, KeyEventArgs e)
{          
    TextPointer tp1 = rtbList[EditorTabcontrol.SelectedIndex].Selection.Start.GetLineStartPosition(0);
    TextPointer tp2 = rtbList[EditorTabcontrol.SelectedIndex].Selection.Start;

    int column = tp1.GetOffsetToPosition(tp2);

    int someBigNumber = int.MaxValue;
    int lineMoved, currentLineNumber;
    rtbList[EditorTabcontrol.SelectedIndex].Selection.Start.GetLineStartPosition(-someBigNumber, out lineMoved);
    currentLineNumber = -lineMoved;
    string LineColumnLabel;

    //LineColumnLabel.Content = "Line: " + currentLineNumber.ToString() + " Column: " + column.ToString();
    LineColumnLabel = "Line: " + currentLineNumber.ToString() + " Column: " + column.ToString();        
}
2个回答

4

您的任务有一个标准示例可在MSDN上找到 (http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.caretposition%28v=vs.110%29.aspx)。请注意,在此上下文中,“光标”被称为“插入符号”。以下是来自MSDN的示例:

// Create a new FlowDocument, and add 3 paragraphs.
FlowDocument flowDoc = new FlowDocument();
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1"))); 
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2"))); 
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
// Set the FlowDocument to be the content for a new RichTextBox.
RichTextBox rtb = new RichTextBox(flowDoc);

// Get the current caret position.
TextPointer caretPos = rtb.CaretPosition;

// Set the TextPointer to the end of the current document.
caretPos = caretPos.DocumentEnd;

// Specify the new caret position at the end of the current document.
rtb.CaretPosition = caretPos;

2
请使用以下代码查找行和位置:

WPF

(请注意:此处为原文内容,无需翻译)
  "<Label x:Name="LabellineNr" Content="line#" BorderThickness="1" BorderBrush="DarkGray" />
  "<Label x:Name="LabelColumnNr" Content="Column#" BorderThickness="1" BorderBrush="DarkGray"/>

C# 代码

    private int privLineID = 1; 
    public int LineID
    {
        get { return privLineID; }
        set 
        { 
            privLineID = value;
            LabellineNr.Content = "Line: " + value;
        }
    }

    private int privColumnID = 1; 
    public int ColumnID
    {
        get { return privColumnID; }
        set 
        { 
            privColumnID = value;
            LabelColumnNr.Content = "Column: " + value;
        }
    }
    private int LineNumber()
    {
        TextPointer caretLineStart = RichTextControl.CaretPosition.GetLineStartPosition(0);
        TextPointer p = RichTextControl.Document.ContentStart.GetLineStartPosition(0);
        int currentLineNumber = 1;

        while (true)
        {
            if (caretLineStart.CompareTo(p) < 0)
            {
                break;
            }
            int result;
            p = p.GetLineStartPosition(1, out result);
            if (result == 0)
            {
                break;
            }
            currentLineNumber++;
        }
        return currentLineNumber;
    }

    private int ColumnNumber()
    {
        TextPointer caretPos = RichTextControl.CaretPosition;
        TextPointer p = RichTextControl.CaretPosition.GetLineStartPosition(0);
        int currentColumnNumber = Math.Max(p.GetOffsetToPosition(caretPos) - 1, 0);

        return currentColumnNumber;
    } 

我想将它绑定到文本框上,当用户移动光标/按下/向上箭头键时,行号应该改变。那么我应该在什么时候包含这段代码,也就是说应该处理哪个事件? - savi
嗨Savi,我修改了代码以适应你的需求。如果你对代码满意,请接受为答案。 - Sam1970

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接