Winforms MultiLine TextBox Background Colour Is Dark Grey On Vista Only

Scenario

I am maintaining a Winforms app. My DEV machine is running XP, but my user is running Vista. Within a GroupBox I added a multiline TextBox and styled it to look like a Label by setting BackgroundColor of the Textbox to System.Control in the Visual Studio Properties of the Textbox and making the TextBox borderless. The Textbox rendered nicely in XP but on Vista the Background colour was rendered as Dark Grey instead of the colour System.Control.

To try and force Vista to render the TextBox background as the colour System.Control, I found out what the RGB Values are for the colour System.Control and set the TextBox Background to that value in the Page Load event. This did not work.

After some Googling I located a couple of posts on the issue which indicated that there is some bug relating to rendering of Multiline Textboxes in Winforms:

Black Background Appearing In ReadOnly TextBox For An Application In Vista Machine

Multiline Textbox With Vertical ScrollBar In A SplitContainer On A TabControl In Vista

This self-explanatory post from Code Project, How To Make A TextBox/RichTextBox Transparent was reported by some correspondents to help them work around the Vista rendering issue.

Demetres Zarkadoulas in this Windows Developer Centre Forum post crystallized the issue: Problem With TextBoxes And Transparent Group Boxes In Vista

This seems to be a problem of the textbox in any transparent container in Vista.

Armed with hints from the links above I added a few different multiline TextBoxes with varying Properties to my Form to see which styles would render the Background Colour correctly under Vista. I found there were a several ways to solve the problem:

  1. Move the Multiline TextBox out of the GroupBox
  2. Give the TextBox a border (BorderStyle 3DBorder will do it)
  3. Use TransparentRichTextBox (add it to your GroupBox dynamically in Code-Behind)
  4. Put the TextBox on top of a Panel (This is what I ended up doing.)

So, if you’ve got a MultiLine TextBox inside a GroupBox on Vista and the Background Colour renders Dark Grey or doesn’t render to the shade you want, do any of the above.

Advertisements

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: