The WinForms Notepad Project: A Little Formatting (Premium)

With the basics of our Windows Forms-based Notepad clone up and running, let’s start adding some of the small but important features and functionality this thing needs to be taken seriously. We still have a long way to go, of course, but you have to start somewhere. So let’s start with the foundation of Notepad, the text editor.

As you may recall, we used a RichTextBox control for this. But as its name suggests, the RichTextBox control works with rich text, which includes formatting like bold, italics, and even things like bulleted list items, and it can load and save to RTF files in addition to plain text (TXT) files. That’s all very useful. But Notepad isn’t a rich text editor, it’s a plain text editor.

You can see the problem if you run the application, paste in or write some text, and then select part or all of it and use a keyboard shortcut like CTRL + B to apply the bold style. (If you paste in rich text, you’ll get the formatting with it, too, including URLs.) That’s no good.

The solution, then, is to restrict the RichTextBox control to plain text, if possible, or use another control---one reader recommended the TextBox control---that only supports plain text.

As an aside, I mentioned last time that I had worked up a lot of this application ahead of time, but one of the things I hadn’t fixed was the rich text capabilities of the editor. I had chosen RichTextBox instead of TextBox in part because it was so easy to dock it to its parent container---that is, the part of the form window not displaying the menu or the status bar. That kind of thing is the entire point of using Visual Studio and a modern framework like WinForms; it gives you so much functionality automatically, eliminating the need to constantly reinvent the wheel and write tons of code.

But as it turns out, you can dock TextBox too. And since I never did figure out a way to limit RichTextBox to plain text, let’s just swap it out. It should be easy enough since we’ve only just started.

To do so, display Form1.vb [Design] in Visual Studio, select the RichTextBox control in the designer and then type DELETE. Then, find and select the TextBox control in the Toolbox---remember, Toolbox search is your friend if you can’t find it---and drag it into the middle of the form. It appears as a smallish, one-line text box.

As you may recall, we docked the RichTextBox control using the Tasks context menu that’s available from the arrow/caret on that control. But when you select this item on the TextBox, you’ll see that there’s only one task available, called Multiline. Actually, we need to enable that, too, since our editor must support multiple lines and not just a single line. So check that.

To dock the TextBox control to the form, we’ll need to access the Properties pane in Visual Studio because Dock is a property of that control. With the TextBox control selected, scan through the list of properties until you see Dock in 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