简体   繁体   English

WPF中RichTextBox内的游标位置

[英]Cursor position inside a RichTextBox in WPF

I am developing a small WPF application, which has multiple tabs in it. 我正在开发一个小型WPF应用程序,其中包含多个选项卡。 I have a status bar in the bottom; 我的底部有一个状态栏; the requirement is to show linenumber and column of the cursor. 要求是显示光标的亚麻和列。 So when the user changes the cursor position, linenumber and column has to get updated automatically. 因此,当用户更改光标位置时,亚麻和列必须自动更新。 Here is the code where I add RichTextBox; 这是我添加RichTextBox的代码; the code which calculates the linenumber and column is in the KeyDown event handler, but this event never gets called. 计算linenumber和列的代码位于KeyDown事件处理程序中,但此事件永远不会被调用。 Which event should I handle to get the cursor linenumber and column? 我应该处理哪个事件来获取光标亚麻布和列?

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();        
}

There is a standard example to your task at MSDN ( http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.caretposition%28v=vs.110%29.aspx ). MSDN上的任务有一个标准示例( http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.caretposition%28v=vs.110%29.aspx )。 Please note, that in this context 'cursor' is called a 'caret'. 请注意,在此上下文中,“光标”称为“插入符号”。 Sample from MSDN follows: 来自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;

use this code to find Line and position 使用此代码查找行和位置

WPF WPF

  "<Label x:Name="LabellineNr" Content="line#" BorderThickness="1" BorderBrush="DarkGray" />
  "<Label x:Name="LabelColumnNr" Content="Column#" BorderThickness="1" BorderBrush="DarkGray"/>

C# code 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;
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM