I want some Moore

Blog about stuff and things and stuff...
mostly about SQL server and .Net
posts - 161, comments - 1487, trackbacks - 33

My Links

SQLTeam.com Links

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'm also a MCP and MCTS for SQL Server. 
Welcome to my blog.

Search this Blog
 

My Blog Feed via Email


Get your Google PageRank
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

Feedback

# 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

# 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

# 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

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 5 and type the answer here:

Powered by: