Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / Visual Basic
Article

Application.EnableVisualStyles Bug

Rate me:
Please Sign up or sign in to vote.
4.92/5 (40 votes)
25 Oct 20032 min read 221.4K   37   44
Calling Application.EnableVisualStyles prevents images from an ImageList from appearing on Windows Common Controls

Introduction

Version 1.1 of the .NET framework introduced the method System.Windows.Forms.Application.EnableVisualStyles. Calling this method prior to the creation of any Forms or Controls, will cause Windows XP to apply a theme when rendering Windows Common Controls and many of the native .NET controls like Buttons and CheckBoxes.

Using .NET 1.0, MFC, WTL or VB6, it is necessary to include a manifest, either in the same directory as the executable, or compiled into it as a resource, in order to make use of Windows XP visual styles. While not overly difficult, providing a manifest for every executable one creates is tedious, and Visual Studio has very little tool support for helping you to do so.

The introduction of EnableVisualStyles to v1.1 of the framework is a nice addition because it allows WinForms applications to easily adopt the new look and feel of Windows XP styles.

The Bug

The problem is that there is bug in the implementation of EnableVisualStyles that interferes with Images stored in an ImageList component and Window Common Controls, like the TreeView or Toolbar classes. The effect is that if you call EnableVisualStyles, all of the images will disappear from your toolbars, treeviews and listviews.

To reproduce the bug:

  1. Create a WinForms application in VS.NET 2003
  2. Add a Toolbar and ImageList to Form1
  3. Add an image to the ImageList and a button to the Toolbar
  4. Assign the image to the button
  5. In the Main method add a call to Application.EnableVisualStyles just before the call to Application.Run

When you run the app on Windows XP, with a Visual Style active, there will be no image on the toolbar button.

Work Around

After some searching through Google groups I found some discussion of this issue and a work around that seems to work and hasn't caused any problems in my applications. (To read more about the work-around go here)

A call to Application.DoEvents just after EnableVisualStyles, seems to fix the problem. How or why, who knows. Most likely it causes some message that was sent via PostMessage to get flushed out to the correct place, before the creation of the first WinForms based window.

So the work around code looks like this:

C#
void Main() 
{ 
  Application.EnableVisualStyles(); 
  Application.DoEvents(); 
  Application.Run(new Form1()); 
}

As of yet, I haven't seen any ill effect from the work-around and it seems to always work.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10

It only went downhill from there.

Hey look, I've got a blog

Comments and Discussions

 
GeneralDon't be too haisty Pin
RodgerB16-Aug-04 6:00
RodgerB16-Aug-04 6:00 
GeneralThanks! Pin
Daniel Turini2-Jul-04 3:13
Daniel Turini2-Jul-04 3:13 
Generaland another bug with combobox Pin
MrPolite22-Apr-04 21:06
MrPolite22-Apr-04 21:06 
GeneralBug (?) #3 When using only ShowDialog Pin
Niels Penneman4-Jan-04 8:41
Niels Penneman4-Jan-04 8:41 
GeneralRe: Bug (?) #3 When using only ShowDialog Pin
Don Kackman4-Jan-04 8:59
Don Kackman4-Jan-04 8:59 
GeneralRe: Bug (?) #3 When using only ShowDialog Pin
Niels Penneman4-Jan-04 9:35
Niels Penneman4-Jan-04 9:35 
GeneralRe: Bug (?) #3 When using only ShowDialog Pin
Don Kackman4-Jan-04 9:46
Don Kackman4-Jan-04 9:46 
GeneralYou just saved my Life Pin
witzulu6-Dec-03 1:09
witzulu6-Dec-03 1:09 
I am suposed to hand in a project in 3 days and simply could not solve this problem. Now I acttualy stand a chance of passing.

THANK YOU

Smile | :) ;)Big Grin | :-D Roll eyes | :rolleyes: Laugh | :laugh:
GeneralRe: You just saved my Life Pin
Don Kackman10-Dec-03 6:09
Don Kackman10-Dec-03 6:09 
GeneralGreat, thanks, it worked! Pin
Leon van Wyk13-Nov-03 22:31
professionalLeon van Wyk13-Nov-03 22:31 
GeneralYet Another Bug Pin
HanreG29-Oct-03 19:09
HanreG29-Oct-03 19:09 
GeneralRe: Yet Another Bug Pin
Don Kackman30-Oct-03 13:49
Don Kackman30-Oct-03 13:49 
GeneralRe: Yet Another Bug Pin
HanreG30-Oct-03 18:46
HanreG30-Oct-03 18:46 
GeneralRe: Yet Another Bug - Solution 2 Pin
Preky7-Apr-06 2:16
Preky7-Apr-06 2:16 
GeneralMore info on this bug Pin
GIngelmo27-Oct-03 4:38
GIngelmo27-Oct-03 4:38 
GeneralYou're right... Pin
Chris Richner26-Oct-03 5:43
Chris Richner26-Oct-03 5:43 
GeneralRe: You're right... Pin
Carl Mercier26-Oct-03 11:19
Carl Mercier26-Oct-03 11:19 
GeneralRe: You're right... Pin
afs_intel8-Jun-04 11:16
afs_intel8-Jun-04 11:16 
GeneralRe: You're right... Pin
Chris Voon9-Sep-04 5:35
Chris Voon9-Sep-04 5:35 
GeneralRe: You're right... Pin
The Man from U.N.C.L.E.25-Nov-04 3:29
The Man from U.N.C.L.E.25-Nov-04 3:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.