The WinForms Notepad Project: Themes (Premium)

While we’re still missing several difficult-to-implement Notepad features, I decided to take a look at adding some features that aren’t available in the real app. I don’t want to get too crazy here, but I feel like there might be some small number of useful features we could add that retain the look, feel, and spirit of the real Notepad while making it a little bit more useful too. And the first and most obvious is basic “theme” support.

In this context, I’m referring only to the foreground (text) and background colors used by the TextBox control that’s at the heart of our application. To change these colors, we’ll add a new Theme menu item under View. Selecting it will open a new submenu with a few pre-made choices (Black on white (the default), Black on light gray, Amber on black, and so on) plus two items for changing the text and background colors. The user’s theme selection will be saved and used each time the application is loaded.

The first step, of course, is to build out the menu. I assume by now you know how to do that, so here’s what my Theme menu looks like:

Changing the TextBox’s foreground (text) and background colors couldn’t be easier: This control has ForeColor and BackColor properties for this very purpose. So the event handlers for the pre-set theme choices look like so:
Private Sub BlackOnWhitedefaultToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles WhiteOnBlackdefaultToolStripMenuItem.Click
   TextBox1.ForeColor = Color.Black
   TextBox1.BackColor = Color.White
End Sub

Private Sub BlackOnLightGrayToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles WhiteOnLightGrayToolStripMenuItem.Click
   TextBox1.ForeColor = Color.Black
   TextBox1.BackColor = Color.LightGray
End Sub

Private Sub AmberOnBlackToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AmberOnBlackToolStripMenuItem.Click
   TextBox1.ForeColor = Color.Orange
   TextBox1.BackColor = Color.Black
End Sub

Private Sub GreenOnBlackToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GreenOnBlackToolStripMenuItem.Click
   TextBox1.ForeColor = Color.LightGreen
   TextBox1.BackColor = Color.Black
End Sub
If you run the application now and try the pre-made theme choices, you should find that they work as expected.

Windows Forms supplies a stock Color dialog that we can display if the user chooses one of the final two options. To add this, display Form1.vb [Design], open the Toolbox, and search for color. Double-click ColorDialog to add one to the project.

ColorDialog works like other Windows Forms dialogs. So we can simply create If-Then loops that make sure the user selected the OK button in the dialog and then apply the selected color accordingly. Our final two event handlers look like so:
Private Sub SelectTextColorToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SelectTextColorToolStripMenuItem.Click
...

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