1. Home
  2. Computing & Technology
  3. Visual Basic
photo of Dan Mabbutt
Dan's Visual Basic Blog

By Dan Mabbutt, About.com Guide to Visual Basic since 2002

Find the Line Number in a TextBox

Sunday February 1, 2009

A reader asked the question, "How can I find the line number of a line of text in a TextBox control?"

Here's what I came up with:

Private Sub TextBox1_MouseClick( ...
lblCursorLoc.Text = _
"Line Number: " & _
(Math.Round(( _
Cursor.Position.Y - Me.Location.Y - _
TextBox1.Location.Y - 36) / 13)) _
.ToString
End Sub

The problem with this solution is that the numbers 36 and 13 are "magic numbers" that have to be determined experimentally. (36 relates to the size of the heading of the form. 13 relates to the size of the font being used.)

Anybody know a better way?

Comments
February 1, 2009 at 6:26 pm
(1) Rich says:

Hello Dan

First of all.. I’m a big fan of your site! So keep up the good work!!

I’m a bit of a vb.NET newbie, though I’ve had a play around with the problem you posed. I think the 13 may be TextBox1.Font.Height (i.e. the line spacing of the font ued in the tetbox) and 36 could be Me.Height-Me.ClientSize (effectively the height of the form’s title bar).

What do you think?

February 2, 2009 at 2:30 am
(2) Rich says:

A few more thoughts:

1. Do we need to adjust for the border size…
borderSize = (Me.Width – Me.ClientSize.Width) / 2
titleSize = Me.Height – Me.ClientSize.Height – borderSize

2. I also tried SystemInformation.CaptionHeight, although it does not seem to give the same answer?!

3. The textbox seems to have a small unused area – an invisible borde a few pixels wide inside the real border. This will throw out the mahs. How can we find out more about how the tetbox is drawn?

Take it easy
Rich

February 3, 2009 at 8:15 am
(3) Mikey says:

I haven’t tried, but couldn’t you just count the vbCrs between the beginning and the Cursor.Position?

February 3, 2009 at 2:54 pm
(4) visualbasic says:

Rich,

Good catch on this. I knew there were some properties there. Thanks for taking the time to remind me.

Yes, there does seem to be a small, uncounted value, but I’m not sure it’s a border inside the Textbox. It seems to me to be “leading” between the lines themselves. When you add enough lines (say 20 or 25), then the only way I can make the last ones count accurately is by finding a “magic number” again and subtracting it from the size of the font.

(TextBox1.Font.Height – 1)

Try it with different fonts and different sizes.

February 3, 2009 at 3:42 pm
(5) visualbasic says:

Mikey …

You can find the number of CR’s (or LF’s) all right, but that doesn’t tell you where the cursor is in the TextBox.

By the way, the code to count the number of characters isn’t obvious either. Here’s “a” way to do that:

Dim byteArray() As Byte
Dim i As Long
Dim count As Long
Dim encoding As New System.Text.UTF8Encoding()
byteArray = encoding.GetBytes(TextBox1.Text)
For i = 0 To UBound(byteArray)
If byteArray(i) = &HA Then count = count + 1
Next
Debug.WriteLine(”Count: ” & count)

Or … you could use some method based on the technique in “Amazing Splits”
http://visualbasic.about.com/od/learnvb6/l/blvbsplit.htm

February 8, 2009 at 3:50 pm
(6) RogerSmets says:

Hello Dan,

I came up with the following solution.
I guess it works only from vb2005 and later

Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown

lblCursorLoc.Text = “Line Number: ” & _
TextBox1.GetLineFromCharIndex(TextBox1.GetCharIndexFromPosition(New Point(e.X, e.Y)))

End Sub

February 8, 2009 at 9:16 pm
(7) visualbasic says:

Indeed! That works. Congratulations.

Every release of .NET brings new methods and properties. It’s simply mind boggling. I’ll have to look harder next time.

Thanks for posting.

March 4, 2009 at 1:07 pm
(8) ThisIsMe says:

im havin trouble making sumthin work… its bugging me!!
i have to creat a new program that uses two forms and 1 module. The first form the user will input a word that is at least 10 characters long. when an OK button is clicked, that form will be hidden and a new form will show. The second form should automatically display the word entered on the first form. This form should have a button to display the word reversed and a button to display the 5th character of the word.\
i’ve been on the assignment for more than 2 long months!! so if sum1 can help me on this that would b GREAT!!

thanks!!

March 4, 2009 at 3:35 pm
(9) visualbasic says:

That looks like a fairly standard application. (In fact, I think I’ve done something very similar in a couple of articles on the site.) But since you state that this is a homework assignment, I’m a little reluctant to just code it for you. I’m not sure you would learn anything about programming that way.

I will, however, explain anything that is confusing you. What is the first problem you’re having?

March 6, 2009 at 12:46 pm
(10) ThisIsMe says:

the only problem im havin is sending the word that the user types in to the 2nd form. also, im having problems with gettin a piece of code to work, which is:

FifthCharacter = strTypedIn.Chars(4)

one of the teachers that we have also doesn’t kno y this code isn’t working as well…

March 16, 2009 at 1:54 pm
(11) SmetsRoger says:

As VisualBasic says your program is a typical homework assignment, it’s only purpose is to acquire programming skills. Therefore it is of no use to write the code for you. Since you have access to the internet a little googling will give you all the answers to your questions. I myself wrote the entire program in about an hour (thanks to google).

I don’t see a problem with your line of code
FifthCharacter = strTypedIn.Chars(4)
since it does exactly what it must do (that is it gets the fifth character from a string

Working with multiple forms in vb.net is indeed a little complicated and if you really can’t work it out i’d like to help you.

greetingd

July 10, 2009 at 3:30 am
(12) mohamnmad says:

how to put numbers only in text box,visuals

basic

July 10, 2009 at 5:33 pm
(13) visualbasic says:

I might not understand your question, but if you just want to put the line number itself in the textbox, that’s quite easy. Just Add numbers with a loop.

Dim Number As Integer = 0
Do Until Number = 10
Number += 1
ListBox1.Items.Add(Number)
Loop

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss
Community Forum
Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.