Question

My current WinForms project needs to launch from Sub Main(); in order to do this, one must un-check the 'Enable Application Framework' option in the Application tab of 'My Project'.

The Sub Main that I have looks like this...

<System.STAThread()>
Public Shared Sub Main()

    Application.EnableVisualStyles()
    'Application.SetCompatibleTextRenderingDefault(True) 
     Application.SetCompatibleTextRenderingDefault(False) 'Fix from Hans Passant
    'Application.DoEvents() 'removed per suggestion

    Application.Run(New p2Login(p2user))

    If p2user.isValidated Then

        Application.Run(New frmMAIN)

    End If

End Sub

The specific problem that I am facing is that using this configuration causes subtle control layout differences. For example, some of my labels are now slightly overlapping their controls, and in-line checkboxes seem to shift slightly to the left. I'm sure there are other differences that aren't immediately apparent.

Points: 1) Visual Style does appear on controls such as buttons. 2) The layout problems go away if I re-check the "Enable Application Framework", and set a startup form.

Issue Example Screen Shot

Was it helpful?

Solution

Application.SetCompatibleTextRenderingDefault(True)

That's the troublemaker. Compatible text rendering enables the text rendering that was used in .NET 1.x, in particular using the Graphics.MeasureString() method to calculate the size of autosized controls like Label. The only reason SetCompatibleTextRenderingDefault exists is to keep text rendering consistent if you have old .NET 1.x components that still draw with Graphics instead of the .NET 2.0 TextRenderer class. A problem that existed 7 years ago when .NET 2.0 was released, surely you don't actually have that problem.

Pass False to fix your problem. That's what the WindowsFormsApplicationBase class does.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top