Click here to Skip to main content
6,910,860 members and growing! (16,725 online)
Email Password   helpLost your password?
General Programming » Bugs & Workarounds » .NET issues     Intermediate

Application.EnableVisualStyles Bug

By Don Kackman

Calling Application.EnableVisualStyles prevents images from an ImageList from appearing on Windows Common Controls
C++/CLI, C#, VB.NET1.1, WinXP, Visual-Studio, Dev
Posted:25 Oct 2003
Views:128,263
Bookmarked:33 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
39 votes for this article.
Popularity: 7.19 Rating: 4.52 out of 5
2 votes, 5.1%
1

2
1 vote, 2.6%
3
3 votes, 7.7%
4
33 votes, 84.6%
5

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:

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

About the Author

Don Kackman


Member
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.
Occupation: Team Leader
Company: Starkey Laboratories
Location: United States United States

Other popular Bugs & Workarounds articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 44 (Total in Forum: 44) (Refresh)FirstPrevNext
GeneralListViewItem's losing BackColor and ForeColor was fixed! PinmemberHideki Oga21:43 2 Sep '08  
GeneralAdditional information PinmemberCrafter7711:56 2 Nov '07  
I just figured i would add this little tidbit (dont know if anyone else already has.)

The reason the DoEvents is resolving this issue seems to be that EnableVisualStyles sends the rendering to queue to be processed but waits for the windows form to be drawn first before continuing with the visual styles ...

After the form is drawn the rendering occurs therefore if u move this Application.DoEvents(); into the Initialize component just prior to the ImageList ... you end up with the same result but the visualstyles rendering is done just before the imagelist instead of before everything else .. incase there are any concerns of the visual styles being applied too early.

I have not looked deeper into this to see if the visualstyles apply everywhere regardless of when they are rendered however ...
GeneralGracias!! Pinmemberomtzr15:08 18 Sep '06  
GeneralThanks! PinmemberFred Fredburger20:45 21 Jul '06  
GeneralRe: Thanks! PinmemberFred Fredburger20:45 21 Jul '06  
GeneralRe: Thanks! PinmemberFred Fredburger20:46 21 Jul '06  
QuestionNew issue in .NET 2.0 with visual styles... Pinmemberxlouk5:51 3 Jul '06  
AnswerRe: New issue in .NET 2.0 with visual styles... Pinmemberxlouk2:22 11 Jul '06  
GeneralThanks PinmemberBikash Rai0:48 13 Dec '05  
GeneralSimple Button And Imagelist Pinmember.::CenoByte::.21:24 28 Jul '05  
GeneralA Different Solution to Missing Icons PinsussSmittyInPhilly5:41 29 Jun '05  
GeneralRe: A Different Solution to Missing Icons PinmemberBen Shemmeld19:29 21 Feb '06  
JokeRe: A Different Solution to Missing Icons PinmemberBen Shemmeld11:10 23 Feb '06  
GeneralRe: A Different Solution to Missing Icons Pinmembermliddekee12:07 20 Mar '06  
GeneralStill Buggy PinsussAnonymous16:20 22 May '05  
GeneralMonthCalendar bug when using XP Styles Pinmembercmaroto4:55 16 May '05  
GeneralRe: MonthCalendar bug when using XP Styles PinmemberDon Kackman9:11 16 May '05  
GeneralTry this Pinmemberhravn8:43 22 Feb '05  
General' System.Windows.Forms.Application.EnableVisualStyles() Pinmemberspratticus17:30 30 Nov '04  
GeneralWorks PinsussAnonymous19:44 3 Nov '04  
GeneralRe:Enable Visual Styles : solution PinsussAnonymous6:40 30 Nov '04  
GeneralEnableVisualStyles doesn't work Pinmemberafinnell13:33 20 Oct '04  
GeneralRe: EnableVisualStyles doesn't work PinmemberDon Kackman9:29 22 Oct '04  
GeneralRe: EnableVisualStyles doesn't work PinmemberThe_Mega_ZZTer6:21 22 Aug '07  
GeneralDon't be too haisty PinmemberRodgerB7:00 16 Aug '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 25 Oct 2003
Editor: Nishant Sivakumar
Copyright 2003 by Don Kackman
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project