Home Search Contact us About us
Title EnableVisualStyles and SEHExceptions
Summary EnableVisualStyles is a nasty little command that can cause completely unrelated errors. Here is an account of the troubles it can cause.
Contributor John McTainsh
Published 29-Aug-2004
Last updated 29-Aug-2004
Page rating   73% for 29 votes Useless Brilliant

Introduction

If you only read the first line of this. DO NOT use Application.EnableVisualStyles(). However if you feel you must. Call Application.DoEvents() directly after it.

This is not an article on how to do stuff. This one is about what not to do and what caused an SEHException to appear after moving a splitter or closing a ShowDialog. What made this more frustrating was that it only occurred if the dialog was opened from the tool bar after the tool bar had been reconfigured via a socket request. I pored 4 solid days of my life into this.

In the end I resorted to (do not try this at home);

  • Not calling the tool bar button event handler directly. Instead starting a 1ms timer to call the tool bar handler. This solver 75% of the problems
  • Removing all splitters form dialogs created from the tool bar
  • Wrapping every ShowDialog call in an Exception handler
  • All I had to do was delete the line Application.EnableVisualStyles()

Other peoples comments

The rest of this article was blatantly stolen from Jeff Key's site. It is an excellent account of the problem.

EnableVisualStyles: Part of Evil Axis?

If you're using v1.1 of the Framework and encounter an SEHException and have no idea what's causing it, check to see if you called Application.EnableVisualStyles().¢ÎApparently that is the source for a number of problems that appear as SEHExceptions. My problem in particular was happening when I tried to show a form with ShowDialog().¢ÎIf I used Show() instead, no problems.¢ÎAll of my other forms had no problems with ShowDialog, yet this one would crash the app. After a bit of Googling, it appears that this error can come up almost anywhere, so keep this in mind as it's almost always the solution. Interestingly, using a manifest file to get the same results does NOT< cause the app to crash.¢ÎGo figure.

Comments Date
TRUE 26-Oct-2004 hernan
yes, it`s true, and it worked like a charm after reading your page. thank you!
EVIL! 15-Feb-2005 Malcolm
This is *the* evil! I was getting the SEHException when I was calling showdialog(). BUT the SEHException was *only* thrown on the second time around (called from a while loop), and *only* when I was collecting the dialog result! To make it worse, the exception didn`t occur when I steped through the application line by line in the debugger.

What a crazy thing to happen!
31-Mar-2005 bahadir
After you put the line Application.EnableVisualStyle , you should also add
Application.DoEvents() to get more functions of the XP
ShowDialog() is evil too 29-Apr-2005 Simon
We had the Problem, when we were creating a dll.
We fixed the Problem with this line of code before ShowDialog()

m_ShapeChoice.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;

We don`t know if it works with other FormBorderStyles the FixedDialog.


:O 15-Nov-2005 Lee Houghton
Excellent, Thank you :)

I was getting SEHExceptions after exiting a block of code that called DoEvents (if I didn`t call DoEvents, it worked fine). It seems to work fine if I just call DoEvents after EnableVisualStyles.
HATTR 15-Feb-2009 dfgdfg
hattttr
HATTR 15-Feb-2009 dfgdfg
hattttr
hattttr
hattttr
hattttr
hattttr
hattttr
hattttr
hattttrhattttr
hattttrhattttr
hattttr
hattttr
hattttrhattttr
hattttrhattttr
hattttrhattttr
hattttrhattttr
hattttrhattttr
hattttrhattttr
hattttrhattttr
hattttrhattttr
hattttrhattttr
hattttr
Home Search Contact us About us