The WinForms Notepad Project (2022): Fix the Themes Bug (Premium)

Here’s another quick fix for one of the bugs in .NETpad: It doesn’t correctly identify which theme the user selected.

I listed this issue in The WinForms Notepad Project (2022): Fork Me: as originally published, the app allows the user to select one of four themes---each with its own foreground and background color---or choose a specific foreground and/or background color. These choices are saved so that the app remembers them on the next run, which is nice. But what it doesn’t do is identify which theme is selected by checking that choice in the menu.

So let’s fix that. This one is easy.

First, we need to check the correct theme choice when the application first loads; if the user chose a custom foreground and/or background color, however, we won’t check a theme. Open Form1.cs and locate the Form1_Load() event handler. Near the top, you will see the following two lines of code that read the MyTextColor and MyBackgroundColor settings and apply them to the rich text box’s foreground and background colors:
richTextBox1.ForeColor = Properties.Settings.Default.MyTextColor;
richTextBox1.BackColor = Properties.Settings.Default.MyBackgroundColor;
We need to add some code right below that to test whether this combination of colors matches one of the four themes, and, if it does, check the appropriate menu item. There is probably a more efficient way to do this, but I came up with the following:
if ((richTextBox1.ForeColor == Color.Black) & (richTextBox1.BackColor == Color.White))
    blackOnWhiteDefaultToolStripMenuItem.Checked = true;
if ((richTextBox1.ForeColor == Color.Black) & (richTextBox1.BackColor == Color.LightGray))
    blackOnLightGrayToolStripMenuItem.Checked = true;
if ((richTextBox1.ForeColor == Color.Orange) & (richTextBox1.BackColor == Color.Black))
    amberOnBlackToolStripMenuItem.Checked = true;
if ((richTextBox1.ForeColor == Color.LightGreen) & (richTextBox1.BackColor == Color.Black))
    greenOnBlackToolStripMenuItem.Checked = true;
Next, we have to make sure that the six theme-related event handlers correctly check/uncheck the theme menu items when the user makes a theme-related choice. If they select a specific theme, we’ll check that menu item and uncheck all the others. If they select a custom foreground or background color, we will uncheck all of the theme menu items.

It’s a lot of code, but it’s all very simple. When we’re done, those six event handlers will look like so:
private void blackOnWhiteDefaultToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.ForeColor = Color.Black;
richTextBox1.BackColor = Color.White;

// Check/uncheck the themes
blackOnWhiteDefaultToolStripMenuItem.Checked = true;
blackOnLightGrayToolStripMenuItem.Checked = false;
amberOnBlackToolStripMenuItem.Checked = false;
greenOnBlackToolStripMenuItem.Checked = false;
}

private void blackOnLightGrayToolStripMenuItem_Click(o...

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