You receive an "unhandled exception of type 'System.Runtime.InteropServices.SEHException'" error message when you call the Application.EnableVisualStyles method in Visual Basic 2005 or in Visual Basic .NET

Symptoms

In Microsoft Visual Basic 2005 and in Microsoft Visual Basic .NET, when you call the Application.EnableVisualStyles method from the Form_Load event or from any other event, you receive an error message that is similar to the following:
An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in system.windows.forms.dll
Note This issue occurs when the following conditions are true:
  • The event calls the Application.EnableVisualStyles method again.
  • The event calls the Application.EnableVisualStyles method when a form loads or after a form loads.

Workaround

To work around this issue, use one of the following methods:
  • Reorganize your code so that you only call the EnableVisualStyles method one time before you load any form. The following code example illustrates how to organize your code:
    Public Sub Main()
    Application.EnableVisualStyles()
    Application.Run(New frmMain) 'Note: The frmMain form is the first form to open.

    End Sub
  • Create and then add an XML manifest file as a resource that enables your application to use visual styles. To do this, follow these steps:
    1. Link to ComCtl32.lib, and then call the InitCommonControlsEx function.
    2. Add a file that is named YourApp.exe.manifest that has the XML manifest format to the source tree. The source tree must be similar to the following code example:
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
      <assemblyIdentity
      version="1.0.0.0"
      processorArchitecture="X86"
      name="CompanyName.ProductName.YourApplication"
      type="win32"
      />
      <description>Type your application description here.</description>
      <dependency>
      <dependentAssembly>
      <assemblyIdentity
      type="win32"
      name="Microsoft.Windows.Common-Controls"
      version="7.0.0.0"
      processorArchitecture="X86"
      publicKeyToken="6595b64144ccf1df"
      language="*"
      />
      </dependentAssembly>
      </dependency>
      </assembly>
    3. Add the XML manifest to the resource file of your application by using the following code example:
      CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "YourApp.exe.manifest"
      Note You must add this line of code on one line. Alternatively, you can put the XML manifest file in the same folder as the executable file of your application. The operating system loads the XML manifest from the file system. Then, the operating system examines the resource section of the executable file. The file system version takes precedence.

More Information

You may receive the same error message that is discussed in the "Symptoms" section when you use the ShowDialog method to open a form. To work around this behavior, use the Show method instead of the ShowDialog method.
Propriedades

ID do Artigo: 897298 - Última Revisão: 6 de dez de 2006 - Revisão: 1

Comentários