Cursor position inside RichTextBox in WPF

I am developing a small WPF application that has several tabs. I have a status bar below; the requirement is to show the linen number and the cursor column. Therefore, when the user changes the cursor position, the name and column should be updated automatically. Here is the code where I add RichTextBox; the code that calculates the row number and column is in the KeyDown event handler, but this event is never called. What event should I process to get the linenumber cursor 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();        
}
+4
source share
2 answers

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;
+3

,

WPF

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

#

    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;
    } 
+2

Source: https://habr.com/ru/post/1546067/


All Articles