1

I'm working on a code editor and I just want to know how to do codes in counting Lines and Columns in richtextbox. Particularly something like this one in actual code editor:

enter image description here

Let's just say count will transfer in a ListBox.

Is there a fast way I can do it?

halfer
  • 19,824
  • 17
  • 99
  • 186
Elegiac
  • 366
  • 10
  • 25
  • IF I understand correctly you want to code a feature (like your VS2010 screeshot) in C#. So `(1)` the question doesn't have anything to do with visual studio as such. => Which is why I've removed the tag. `(2)` Are you talking about windows form or WPF? – gideon Apr 25 '13 at 10:55
  • possible duplicate of [Current line and column numbers in a RichTextBox in a Winforms application](http://stackoverflow.com/questions/2425847/current-line-and-column-numbers-in-a-richtextbox-in-a-winforms-application) and this : http://stackoverflow.com/questions/657635/displaying-line-number-in-rich-text-box-c-sharp – gideon Apr 25 '13 at 11:00
  • @gideon, im talking with windows forms sir . – Elegiac Apr 25 '13 at 11:08

2 Answers2

2

You can do this :

//This to get lines number.
int index = richTextBox.SelectionStart;
int li = richTextBox.GetLineFromCharIndex(index);

// This to get columns number.
int firstChar = richTextBox.GetFirstCharIndexFromLine(li);
int col = index - firstChar;

Good luck!

Obama
  • 2,586
  • 2
  • 30
  • 49
0

This will do it, you just have to call the code inside a timer:

int line = 1 + richTextBox1.GetLineFromCharIndex(richTextBox1.GetFirstCharIndexOfCurrentLine());
int column = 1 + richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexOfCurrentLine();
label1.Text = "line: " + line.ToString() + ", column: " + column.ToString();
Sébastien Garmier
  • 1,263
  • 9
  • 16