Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the startup form initially invisible or hidden

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, all the other forms must be hidden.

I can hide all the other forms, but I cannot hide the startup form.so that my application's icon in the System Tray.

For example, firewall/antivirus and instant messaging applications do this so as to run in the background and still be accessible to the user from the System Tray.

like image 954
amexn Avatar asked Jul 20 '10 09:07

amexn


2 Answers

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.

like image 157
djdd87 Avatar answered Oct 24 '22 05:10

djdd87


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
like image 39
Eyal Avatar answered Oct 24 '22 07:10

Eyal