I want some Moore

Blog about stuff and things and stuff. Mostly about SQL server and .Net
posts - 218, comments - 2281, trackbacks - 33

My Links

Advertisement

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer.

I also speak at local user group meetings and conferences like SQLBits and NT Conference
Welcome to my blog.
SQL Server MVP

My Books

SQL Server MVP Deep Dives 2
The Red Gate Guide to SQL Server Team based Development Free e-book

My Blog Feed via Email
Follow MladenPrajdic on Twitter


Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

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

    public void GoTo(int line, int column)
    {
        if (line < 1 || column < 1 || this.Lines.Length < line)
            return;
        
        this.SelectionStart = this.GetFirstCharIndexFromLine(line - 1) + column - 1;
        this.SelectionLength = 0;
    }

    public int CurrentColumn
    {
        get { return this.SelectionStart - this.GetFirstCharIndexOfCurrentLine() + 1; }
    }
 
    public int CurrentLine
    {
        get { return this.GetLineFromCharIndex(this.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

Print | posted on Friday, October 19, 2007 3:40 PM | Filed Under [ .Net ]

Feedback

Gravatar

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

public int CurrentColumn
{
get { return textBox1.SelectionStart - textBox1.GetFirstCharIndexOfCurrentLine() + 1; }
}

?
10/19/2007 5:41 PM | Andrej Tozon
Gravatar

# 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!
10/19/2007 5:45 PM | Mladen
Gravatar

# 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!
4/11/2008 12:55 PM | solid
Gravatar

# 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

7/28/2009 5:42 PM | EachOnSet
Gravatar

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

@EachOnSet
thanx for letting me know!
7/28/2009 5:58 PM | Mladen
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET