Click here to Skip to main content
15,884,237 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

 
GeneralListViewItem's losing BackColor and ForeColor was fixed! Pin
Hideki Oga2-Sep-08 20:43
Hideki Oga2-Sep-08 20:43 
GeneralAdditional information Pin
Crafter772-Nov-07 10:56
Crafter772-Nov-07 10:56 
GeneralGracias!! Pin
omtzr18-Sep-06 14:08
omtzr18-Sep-06 14:08 
GeneralThanks! Pin
Fred Fredburger21-Jul-06 19:45
Fred Fredburger21-Jul-06 19:45 
GeneralRe: Thanks! Pin
Fred Fredburger21-Jul-06 19:45
Fred Fredburger21-Jul-06 19:45 
GeneralRe: Thanks! Pin
Fred Fredburger21-Jul-06 19:46
Fred Fredburger21-Jul-06 19:46 
QuestionNew issue in .NET 2.0 with visual styles... Pin
xlouk3-Jul-06 4:51
xlouk3-Jul-06 4:51 
AnswerRe: New issue in .NET 2.0 with visual styles... Pin
xlouk11-Jul-06 1:22
xlouk11-Jul-06 1:22 
GeneralThanks Pin
HakunaMatada12-Dec-05 23:48
HakunaMatada12-Dec-05 23:48 
GeneralSimple Button And Imagelist Pin
.::CenoByte::.28-Jul-05 20:24
.::CenoByte::.28-Jul-05 20:24 
GeneralA Different Solution to Missing Icons Pin
SmittyPhilly29-Jun-05 4:41
SmittyPhilly29-Jun-05 4:41 
GeneralRe: A Different Solution to Missing Icons Pin
Ben Shemmeld21-Feb-06 18:29
Ben Shemmeld21-Feb-06 18:29 
JokeRe: A Different Solution to Missing Icons Pin
Ben Shemmeld23-Feb-06 10:10
Ben Shemmeld23-Feb-06 10:10 
GeneralRe: A Different Solution to Missing Icons Pin
mliddekee20-Mar-06 11:07
mliddekee20-Mar-06 11:07 
GeneralStill Buggy Pin
Anonymous22-May-05 15:20
Anonymous22-May-05 15:20 
GeneralMonthCalendar bug when using XP Styles Pin
cmaroto16-May-05 3:55
cmaroto16-May-05 3:55 
GeneralRe: MonthCalendar bug when using XP Styles Pin
Don Kackman16-May-05 8:11
Don Kackman16-May-05 8:11 
GeneralTry this Pin
hravn22-Feb-05 7:43
hravn22-Feb-05 7:43 
General' System.Windows.Forms.Application.EnableVisualStyles() Pin
spratticus30-Nov-04 16:30
spratticus30-Nov-04 16:30 
GeneralWorks Pin
Anonymous3-Nov-04 18:44
Anonymous3-Nov-04 18:44 
GeneralRe:Enable Visual Styles : solution Pin
Anonymous30-Nov-04 5:40
Anonymous30-Nov-04 5:40 
GeneralEnableVisualStyles doesn't work Pin
afinnell20-Oct-04 12:33
afinnell20-Oct-04 12:33 
GeneralRe: EnableVisualStyles doesn't work Pin
Don Kackman22-Oct-04 8:29
Don Kackman22-Oct-04 8:29 
GeneralRe: EnableVisualStyles doesn't work Pin
The_Mega_ZZTer22-Aug-07 5:21
The_Mega_ZZTer22-Aug-07 5:21 
GeneralDon't be too haisty Pin
RodgerB16-Aug-04 6:00
RodgerB16-Aug-04 6:00 

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.