Mladen Prajdić Blog

Blog about stuff and things and stuff. Mostly about SQL server and .Net

C# : Get current Caret Line and Column in a multiline Windows Forms TextBox

I can't believe these properties aren't already natively implemented. Come on Microsoft... we do live in the year 2007 you know!!

I know that you can do this using P/Invoke and SendMessage but i wanted a nice managed way of finding

the current line and column of the Caret position in a multiline TextBox.

 

After a bit of experimenting i've come up with this extended class:

 

public class TextBoxEx : TextBox
{
    public TextBoxEx()
    { }
<span class="kwrd">public</span> <span class="kwrd">void</span> GoTo(<span class="kwrd">int</span> line, <span class="kwrd">int</span> column)
{
    <span class="kwrd">if</span> (line &lt; 1 || column &lt; 1 || <span class="kwrd">this</span>.Lines.Length &lt; line)
        <span class="kwrd">return</span>;
    
    <span class="kwrd">this</span>.SelectionStart = <span class="kwrd">this</span>.GetFirstCharIndexFromLine(line - 1) + column - 1;
    <span class="kwrd">this</span>.SelectionLength = 0;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> CurrentColumn
{
    get { <span class="kwrd">return</span> <span class="kwrd">this</span>.SelectionStart - <span class="kwrd">this</span>.GetFirstCharIndexOfCurrentLine() + 1; }
}

<span class="kwrd">public</span> <span class="kwrd">int</span> CurrentLine
{
    get { <span class="kwrd">return</span> <span class="kwrd">this</span>.GetLineFromCharIndex(<span class="kwrd">this</span>.SelectionStart) + 1; }
}

}

 

Hope you find it usefull. If anyone knows a better way do let me know.

UPDATE: With a bit of help from Andrej this is a better and shorter version with set method too.

 

kick it on DotNetKicks.com

Legacy Comments


Andrej Tozon
2007-10-19
re: C# : Get current Caret Line and Column in a multiline Windows Forms TextBox
public int CurrentColumn
{
get { return textBox1.SelectionStart - textBox1.GetFirstCharIndexOfCurrentLine() + 1; }
}

?

Mladen
2007-10-19
re: C# : Get current Caret Line and Column in a multiline Windows Forms TextBox
see i knew there should be a better way.
it just never occured to me to do it like this.

Thanx Andrej!

solid
2008-04-11
re: C# : Get current Caret Line and Column in a multiline Windows Forms TextBox
!Hello!!I writing tool where i use TextBox.
So above description of how to get current line is good!But with column i have next problem:
So when user writes some text the textBox1.SelectionStart is equal to last entered symbol. But when user uses arrows or mouse to set currsor somewhere between entered text the textBox1.SelectionStart is still equal to last entered symbol. So here is the question: how to get position of current cursor position in line when user sets cursor by mouse or arrows somewhere in the middle of entered text??

Thans for any answers or propositions!

EachOnSet
2009-07-28
re: C# : Get current Caret Line and Column in a multiline Windows Forms TextBox
Someone copied this article : http://my.opera.com/thahuong/blog/2008/03/20/c


Mladen
2009-07-28
re: C# : Get current Caret Line and Column in a multiline Windows Forms TextBox
@EachOnSet
thanx for letting me know!