2010-03-11 5 views
7

Jak uzyskać bieżące numery linii i kolumn w RichTextBox w aplikacji WinForm?Bieżąca linia i numery kolumn w RichTextBox w aplikacji WinForm

UWAGA

Folks, Chcę tylko proste rozwiązanie, jeśli jest ona dostępna przez kogoś, a on jest gotów podzielić się z nami, a nie łączy, aby zrobić badania! Po prostu daj mi kod! W przeciwnym razie będę musiał kupić kontrolę ...

Odpowiedz

1

Dla wiersza i kolumny, sprawdź projekt Richer RichTextBox. Jest to rozszerzona wersja RichTextBox, która obsługuje numery wierszy i kolumn. Kod z artykułu:

this.rtb.CursorPositionChanged += 
    new System.EventHandler(this.rtb_CursorPositionChanged); 
this.rtb.SelectionChanged += 
    new System.EventHandler(this.rtb_SelectionChanged); 
. 
. 
. 
private void rtb_CursorPositionChanged(object sender, System.EventArgs e) 
{ 
    int line = rtb.CurrentLine; 
    int col = rtb.CurrentColumn; 
    int pos = rtb.CurrentPosition; 

    statusBar.Text = "Line " + line + ", Col " + col + 
        ", Position " + pos; 
} 

private void rtb_SelectionChanged(object sender, System.EventArgs e) 
{ 
    int start = rtb.SelectionStart; 
    int end = rtb.SelectionEnd; 
    int length = rtb.SelectionLength; 

    statusBar.Text = "Start " + start + ", End " + end + 
        ", Length " + length; 
} 

Aby osiągnąć takie zachowanie, musimy rozszerzyć klasę RichTextBox w następujący sposób:

using System; 
using System.Drawing; 
using System.Windows.Forms; 

namespace Nik.UserControls 
{ 
    public class RicherTextBox2 : System.Windows.Forms.RichTextBox 
    { 
     public event EventHandler CursorPositionChanged; 

     protected virtual void OnCursorPositionChanged(EventArgs e) 
     { 
      if (CursorPositionChanged != null) 
       CursorPositionChanged(this, e); 
     } 

     protected override void OnSelectionChanged(EventArgs e) 
     { 
      if (SelectionLength == 0) 
       OnCursorPositionChanged(e); 
      else 
       base.OnSelectionChanged(e); 
     } 

     public int CurrentColumn 
     { 
      get { return CursorPosition.Column(this, SelectionStart); } 
     } 

     public int CurrentLine 
     { 
      get { return CursorPosition.Line(this, SelectionStart); } 
     } 

     public int CurrentPosition 
     { 
      get { return this.SelectionStart; } 
     } 

     public int SelectionEnd 
     { 
      get { return SelectionStart + SelectionLength; } 
     } 
    } 

    internal class CursorPosition 
    { 
     [System.Runtime.InteropServices.DllImport("user32")] 
     public static extern int GetCaretPos(ref Point lpPoint); 

     private static int GetCorrection(RichTextBox e, int index) 
     { 
      Point pt1 = Point.Empty; 
      GetCaretPos(ref pt1); 
      Point pt2 = e.GetPositionFromCharIndex(index); 

      if (pt1 != pt2) 
       return 1; 
      else 
       return 0; 
     } 

     public static int Line(RichTextBox e, int index) 
     { 
      int correction = GetCorrection(e, index); 
      return e.GetLineFromCharIndex(index) - correction + 1; 
     } 

     public static int Column(RichTextBox e, int index1) 
     { 
      int correction = GetCorrection(e, index1); 
      Point p = e.GetPositionFromCharIndex(index1 - correction); 

      if (p.X == 1) 
       return 1; 

      p.X = 0; 
      int index2 = e.GetCharIndexFromPosition(p); 

      int col = index1 - index2 + 1; 

      return col; 
     } 
    } 
} 

Displaying line number in RichTextBox dla części numer linii.

+2

Nie wiem, czy to działa, kolego, ale zasługujesz na to, aby zostać poddanym pod głosowanie. –

+0

Ponad komplikowanie rzeczy, gdy są niepotrzebne. Po prostu użyj małej rzeczy zwanej Matematyką, aby obliczyć pozycje (patrz odpowiedź Alexa McBride'a). – SoLaR

15

Pomyślałem, że opublikuję nieco prostszy sposób robienia tego.

// Get the line. 
int index = richTextBox.SelectionStart; 
int line = richTextBox.GetLineFromCharIndex(index); 

// Get the column. 
int firstChar = richTextBox.GetFirstCharIndexFromLine(line); 
int column = index - firstChar; 

Pobierz aktualny wybrany indeks, uzyskać bieżącą linię, a następnie dostać się kolumna odjąć wybranego indeksu z indeksu pierwszego charakter tej linii.