2014-06-25 64 views
4

我正在開發一個小型的WPF應用程序,它有多個選項卡。我在底部有一個狀態欄;要求顯示光標的行號和列。所以當用戶更改光標位置時,必須自動更新繡線數量和列數。這裏是我添加RichTextBox的代碼;計算linenumber和column的代碼位於KeyDown事件處理程序中,但該事件永遠不會被調用。我應該處理哪個事件來獲取光標繡線和列?WPF中的RichTextBox中的光標位置

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

回答

3

在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; 
    } 
+0

我想這個綁定到一個文本框,並當用戶移動光標/按下/向上箭頭鍵時,布料數量應該改變。那麼我什麼時候應該包含這個代碼,我的意思是應該處理哪個事件? – savi

+0

嗨Savi我修改了代碼以適合你。如果您對代碼感到滿意,請接受爲答案 – Sam1970