How to make the startup form initially invisible or hidden
I have a GUI project with 2 form and the forms have to be displayed separately. i.e. When form 1 is shown,
set ShowInTaskbar property to false and set the WindowState to minimized
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
I'm guessing that what you're asking is how to make the form not appear in the task bar and only appear in the system tray, just like an IM or an anti virus?
If so, just set the ShowInTaskbar
property of the Form
to false
.
As for making the initial form invisible, you'll have to use an ApplicationContext within Application.Run
instead of the main form.