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 ...

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

Please check our Community Guidelines before commenting

Windows Intelligence In Your Inbox

Sign up for our new free newsletter to get three time-saving tips each Friday

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Thurrott © 2024 Thurrott LLC