Find the Line Number in a TextBox
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?


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?
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
I haven’t tried, but couldn’t you just count the vbCrs between the beginning and the Cursor.Position?
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.
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
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
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.
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!!
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?
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…
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
how to put numbers only in text box,visuals
basic
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