The WinForms Notepad Project: An Experiment (Premium)

Taking a step back for a moment, I wanted to see whether using a RichTextBox control would improve our app. As you may recall, I started out with a RichTextBox but quickly switched to a TextBox control, which only works with plain text. But this may have been a mistake because the RichTextBox control provides additional functionality that we need.

Case in point: The position of the text caret, which is needed for that “Ln 1, Col 1” display in the Notepad status bar. The RichTextBox control provides properties related to the caret position, while TextBox does not.

To test whether obtaining and displaying this information was relatively easy, I did what I often do: I created a new Windows Forms app in Visual Studio and blocked it out with a basic menu, a basic status bar, and, in this case, a RichTextBox. And then I did a bit of Googling to figure out whether I could use the RichTextBox’s unique properties to display the row (line) and column position of the text cursor (caret).

This required a bit of work. The RichTextBox’s SelectionStart property, for example, can be used to get the cursor position relative to its starting position (0) in the RichTextBox. But it has no sense of row (line); instead, it just returns how many characters it is away from the starting position, regardless of the line.

But that was a good starting point. And RichTextBox provides other properties, like GetLineFromCharIndex and GetFirstCharIndexFromLine, which can be used together to calculate both the row (line) and column of the text cursor. So I eventually came up with the following code in the RichTextBox’s SelectionChanged event handler:
ToolStripStatusLabel1.Text =
            "Ln " +
            (RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart) + 1).ToString() +
            ", Col " +
            (RichTextBox1.SelectionStart - RichTextBox1.GetFirstCharIndexFromLine(RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)) + 1).ToString()
Calculating the row (line) is easy: The GetLineFromCharIndex property returns this value.

Getting the column looks more complicated, but it’s just a lot of code: Basically, the column is the number of characters that appear before the caret on the current line.

The “+1”s you see in there are due to Visual Basic using a zero-based positioning scheme, whereas Notepad starts at 1. The starting cursor position, from Visual Basic’s perspective, is 0, 0 (or “Ln 0, Col 0”). But Notepad displays that as “Ln 1, Col 1”. So I set that as the default value of RichTextBox1’s Text property. And then changed its value whenever the selection changed.

That seems to work properly when you just reposition the cursor. It doesn’t work like Notepad does when you select a block of text (by holding down the SHIFT key and using an arrow key to move the text cursor), however. That is probably solvable, but not a big deal in my opin...

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