Microsoft wrote a webpage about this. It gives an example of using the ApplicationContext
. Basically instead of having a forms application, you have an app that runs Main()
and Main
then opens the forms.
http://msdn.microsoft.com/en-us/library/Aa984417
You lose a lose of functionality that way, however, because you have to disable the "application framework". It'll make your Windows ugly.
Here's a different solution, almost a hack but not too bad. When Windows starts your form app and set Visible
to true, that causes a call to SetVisibleCore
. You can override that function. On the first time that SetVisibleCore
is called, set it false. From then out, just pass through.
Keep in mind that Form.Load
won't trigger when your app starts if the form isn't showing so move all the code there into Sub New()
.
Here's the whole thing:
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
config.LoadFromRegistry() 'this gets config.StartMinimized from the registry
' Code that needs to run at start, even if the form isn't showing,
' should be here. Form.Load will only happen when the Form is actually
' visible for the first time.
End Sub
Dim FirstSetVisible As Boolean = True
Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
If config.StartMinimized And FirstSetVisible Then
MyBase.SetVisibleCore(False) 'ignore Windows attempt to set Visible
FirstSetVisible = False 'never do this again
Else
MyBase.SetVisibleCore(value)
End If
End Sub