Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Column number of the cursor in a TextBox in C#?

I've got a multiline textBox that I would like to have a label on the form displaying the current line and column position of, as Visual Studio does.

I know I can get the line # with GetLineFromCharIndex, but how can I get the column # on that line?

(I really want the Cursor Position on that line, not 'column', per se)

like image 362
RyanE Avatar asked Sep 15 '08 20:09

RyanE


2 Answers

int line = textbox.GetLineFromCharIndex(textbox.SelectionStart);
int column = textbox.SelectionStart - textbox.GetFirstCharIndexFromLine(line);
like image 176
DamienG Avatar answered Nov 06 '22 13:11

DamienG


textBox.SelectionStart -
textBox.GetFirstCharIndexFromLine(textBox.GetLineFromCharIndex(textBox.SelectionStart))
like image 36
Omer van Kloeten Avatar answered Nov 06 '22 13:11

Omer van Kloeten