VB MVP KenLin @HKSAR

Loving Microsoft Technologies.

Application.EnableVisualStyle has BUG??????????

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.

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 a 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.

Solution(for C# only??)

After some searching from the google.com, I found some discussion of this issue and a work around that seems to work and hasn't caused any problems in C# applications.

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:

Sub Main() 
  Application.EnableVisualStyles()
  Application.DoEvents()
  Application.Run(Form1())
End Sub

However, I try to inplement this into VB.NET and it does not work at all. However, everyone are saying it works fine if under C#. Interesting.

Also, I read from someone called Michael Harsh who is from MS Team(I guess) and replying with the following message in some public forums.
====================================================
This is indeed a bug and will not be fixed for the final version of 2003. The problem is that the handle for the ImageList is being created prior to the the fusion binding context we setup around the app's main message pump. This is why ImageLists are broken everywhere (TabControl, TreeView, etc).

There are a few decent workarounds. The first is to either manually add a .manifest file to your .exe's directory, or embed one in the Win32 resource section of your .exe.

The other workaround is to move the code which creates your ImageList from InitializeComponent to the Form's Load event. This will delay the handle creation.

Good luck,
====================================================

Posted: Tue, Dec 7 2004 12:12 by kenlin | with 11 comment(s)
Filed under:

Comments

kenlin said:

Thanks for this post, I've been trying everything to figure out why my images weren't showing.
# May 12, 2005 1:04 AM

kenlin said:

Well, I have tried it too. And found this information after searching. Hope this may help you. But VS2005 is coming up, this should be fine in VS2005
# May 12, 2005 10:34 AM

TrackBack said:

^_~,pretty good!csharpsseeoo
# May 18, 2005 6:41 AM

kenlin said:

gr8 work, thnx
# May 19, 2005 7:42 AM

TrackBack said:

Application.EnableVisualStyle has BUG??????????ooeess
# June 16, 2005 2:28 AM

TrackBack said:

Application.EnableVisualStyle has BUG??????????ooeess
# July 21, 2005 6:20 PM

TrackBack said:

Application.EnableVisualStyle has BUG??????????ooeess
# August 2, 2005 7:47 PM

Quintin Willison said:

Just what I needed. Thanks for the post.
I shall fix this by creating the image list on form load as you suggest. A cracking plan.
So, yet ANOTHER visual studio 2003 issue that needs special treatment (I will not even start ranting on user controls in the context of multiple assemblies and grown up namespaces! ;)).
Maybe when I'm brave enough to "switch" to 2005 the world will be a better place... just maybe...
# February 7, 2006 8:02 AM

Oscar Gil said:

Oh my god!!! It work's!!! This is a "very good" Microsoft product...

# December 20, 2006 10:39 AM

David said:

Oh man thx a lot!!!

nice posting!!

# May 24, 2007 1:36 AM

Kenlin creations | Entertainment4 said:

Pingback from  Kenlin creations | Entertainment4

# August 26, 2012 11:31 AM