The WinForms Notepad Project: Text Cursor (Caret) Position (Premium)

As it turns out, the little code experiment we engaged in yesterday can be added to our project as-is to display the caret position.

Right, I goofed.

After figuring out how to implement displaying the text cursor (caret) position with a RichTextBox control in The WinForms Notepad Project: An Experiment (Premium), I started looking at what it would take to replace the TextBox control we’re using with a RichTextBox. It wasn’t pretty, actually, and I was starting to think I had run into a problem that I could probably solve but would have a hard time explaining.

And then something funny (and embarrassing) happened.

While messing around with some code in the working (TextBox-based) version of the program, I discovered that the TextBox control actually has one of the properties I was using in the RichTextBox-based version of the program to calculate the caret position. It seemed unlikely that TextBox would provide only one of the properties I was using for this, so I checked and … sure enough. It has the other properties I used as well. I could use the same code to calculate the caret position with the TextBox control too. Doh.

So let’s implement that.

First up, we need to rename the status bar field that will display this information. And we need to seed that field with some starter text representing the original caret position.

As you may recall, we had created a status bar with five fields, which, from left to right are named ToolStripStatusLabel1, ToolStripStatusLabel2, ZoomStripStatusLabel, ToolStripStatusLabel4, and ToolStripStatusLabel5. As its name implies, ZoomStripStatusLabel is used to display the current zoom level. But we’re not really using the other fields.

The caret position will be displayed in the second field, which is currently named ToolStripStatusLabel2. To find it, open Form1.vb [Design] and click on the second field in the status bar in the application window. Then, in the Properties pane, change the following two properties:

Name: PositionToolStripStatusLabel

Text: “Ln 1, Col 1” (with no quotes)

Now, we need to add the caret position display code that I had created in the previous experiment to the TextBox1_TextChanged event handler. The body of this event handler current looks like this:

If TextHasChanged = False Then
    Me.Text = "*" + Me.Text
    TextHasChanged = True
End If

So add the following code after the End If statement:

PositionToolStripStatusLabel.Text =
    "Ln " +
    (TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart) + 1).ToString() +
    ", Col " +
    (TextBox1.SelectionStart - TextBox1.GetFirstCharIndexFromLine(TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart)) + 1).ToString()

That works as it did in the experiment. But we need to make sure that we change the display of that field as needed, and not just when the text changes. For example, the user can change the position of the text cursor (caret) by moving it with the arrow keys or by clicking arbitrarily within the text box.

Fortunately, the TextBox control has events—like KeyUp and Click—it can handle that will cover these eventualities. To find them, open Form1.vb (if it’s not already opened) and make sure that TextBox1 is selected in the middle drop-down at the top of the editor window (if it’s not already selected). Then, using the third (rightmost) drop-down, find the KeyUp event.

When you select that, a blank TextBox1_KeyUp event handler will appear in Form1.vb. Add the same code you added above.

Next, do this again for TextBox1_Click by locating Click in that third drop-down. (That is, add the same code from above here as well.)

Now, run the application and observe how the new status bar field updates with the correct text cursor (caret) position as you type and move the caret around with the arrow keys and/or mouse.

(Random aside: These are meeting notes from Ignite. Which I took using this application.)

I think that mostly covers the usual eventualities, but if you discover a time when the caret position is not correctly changed, let me know, and we can add/fix that.

More soon.

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Thurrott