Windows Develop Bookmark and Share   
 index > Windows Forms General > How do I display the column and line number using TextBox?
 

How do I display the column and line number using TextBox?

I want to keep a label below the TextBox. I couldn't find any commands for finding out the columns number and the line number in the TextBox class. Any ideas how I can display the column and line number of the position of the cursor in the TextBox?
Zed TooCool  Monday, January 01, 2007 1:03 PM

Try this code:

private void UpdateCaretPos()

{

Point pt;

int line, col, index;

// get the current line

index = textBox1.SelectionStart;

line = textBox1.GetLineFromCharIndex(index);

// get the caret position in pixel coordinates

pt = textBox1.GetPositionFromCharIndex(index);

// now get the character index at the start of the line, and

// subtract from the current index to get the column

pt.X = 0;

col = index - textBox1.GetCharIndexFromPosition(pt);

// finally, update the display in the status bar, incrementing the line and

// column values so that the first line & first character position is

// shown as "1, 1"

label1.Text = (++line).ToString() + ", " + (++col).ToString() ;

}

You might want to use RichTextBox which has event called SelectionChanged, that is fired every time the caret is changing.

This is from this link:

http://64.233.183.104/search?q=cache:ZvAJO6B5wA8J:www.microbion.co.uk/developers/C%2520position%2520of%2520caret.pdf+C%23+TextBox+caret+position&hl=iw&ct=clnk&cd=1

Eli Gazit  Tuesday, January 02, 2007 9:00 AM

Hi, Furqan

As long as I know, there is no such a method or command to directly find out the columns number and the rows number of the textbox. But, you can implement by yourself.

Your idea gonna be interesting, so I deal with it for you as an example, and it works well:

            int start = textBox1.SelectionStart;//record the position of the cursor in textbox

            int column = 0, row;

            for (row = 0; row < textBox1.Lines.Length; row++)

            {

                if (start < 0)

                    break;

                else

                    column = start;

                start -= textBox1.Lines[row].Length + 2;//each newline costs 2

            }

            column++;//columns begin with 1

You can put above code to the event SelectionChanged of RichTextBox, that is raised when the caret is changing.

 

Hope it helps

Figo Fei  Tuesday, January 02, 2007 9:04 AM

Try this code:

private void UpdateCaretPos()

{

Point pt;

int line, col, index;

// get the current line

index = textBox1.SelectionStart;

line = textBox1.GetLineFromCharIndex(index);

// get the caret position in pixel coordinates

pt = textBox1.GetPositionFromCharIndex(index);

// now get the character index at the start of the line, and

// subtract from the current index to get the column

pt.X = 0;

col = index - textBox1.GetCharIndexFromPosition(pt);

// finally, update the display in the status bar, incrementing the line and

// column values so that the first line & first character position is

// shown as "1, 1"

label1.Text = (++line).ToString() + ", " + (++col).ToString() ;

}

You might want to use RichTextBox which has event called SelectionChanged, that is fired every time the caret is changing.

This is from this link:

http://64.233.183.104/search?q=cache:ZvAJO6B5wA8J:www.microbion.co.uk/developers/C%2520position%2520of%2520caret.pdf+C%23+TextBox+caret+position&hl=iw&ct=clnk&cd=1

Eli Gazit  Tuesday, January 02, 2007 9:00 AM

Hi, Furqan

As long as I know, there is no such a method or command to directly find out the columns number and the rows number of the textbox. But, you can implement by yourself.

Your idea gonna be interesting, so I deal with it for you as an example, and it works well:

            int start = textBox1.SelectionStart;//record the position of the cursor in textbox

            int column = 0, row;

            for (row = 0; row < textBox1.Lines.Length; row++)

            {

                if (start < 0)

                    break;

                else

                    column = start;

                start -= textBox1.Lines[row].Length + 2;//each newline costs 2

            }

            column++;//columns begin with 1

You can put above code to the event SelectionChanged of RichTextBox, that is raised when the caret is changing.

 

Hope it helps

Figo Fei  Tuesday, January 02, 2007 9:04 AM
Actually...I found that out myself. With a little bit of patience and perseverance, I was able to do the same using RichTextBox in the same way as you guys have done - using the SelectStart property etc. Amazing how intuitive the .NET library is, don't you think? Thnx for replying anyway.
Zed TooCool  Wednesday, January 03, 2007 11:37 AM

You can use google to search for other answers

Custom Search

More Threads

• SMTP set up for Password Recovery
• Disappearing colors
• Reference Using both word 2003 and 2007.
• Datagridview complaints editorial issues
• Design guidelines, MVC?
• How to Find Documentation for ASP.NET and windows 2008 server?
• Tip: Multiline messagebox with localized resource strings
• How to display a form from another one
• Shortcut keys
• ComboBox autocomplete stopping on a "/" error