Hiding main window at startup

  • Thread starter Thread starter Tony Vitonis
  • Start date Start date
T

Tony Vitonis

Hello. I've written an app that I want to "live" in the system tray.
I want it to start up with just a tray icon showing, and if the user
selects "Settings..." from the icon's context menu, to display a
window that will allow him to change settings. When he hits "OK" or
"Cancel", the window should hide again.

At first, I tried putting a "Me.Visible = False" in the form's Load
event, but apparently that code runs before the command can be
effective. I moved the line to the Activate event, which hid the form
at startup just fine; however, when I tried to set Me.Visible to True
in another part of the code, I saw the Form.Activate event running
again, re-hiding the form.

On my second try, I created a form-level variable, WindowVisible, put
Me.Visible = WindowVisible in the Form.Activate event, and set
WindowVisible in the code where necessary. This works fine, but it
seems a little kludgy to me, as if I'm trying to work around something
I don't understand.

My questions are:

1. Is there a better way to hide a form at startup?

2. Is there a better way to approach the whole problem?

3. Where can I read a summary of the order in which form events are
raised, and for what reasons?

Thanks a lot.
 
Tony,
2. Is there a better way to approach the whole problem?
Yes.

Matthew MacDonald's book "Microsoft Visual Basic .NET Programmer's Cookbook"
has a topic on creating a system tray program.

Basically: Rather than using a Form as the startup object, use a Component
instead.

Create a new Component class (use Project - Add Component). Add a NotifyIcon
to the component designer. Also add a ContextMenu object for the
NotifyIcon. When you click the menu, create and show the form. Remember to
put an Exit option on the menu.

Make the Component the startup object, adding a Shared Sub Main to the
component.

Public Class Component1
Inherits System.ComponentModel.Component

' Component designer generated code omitted.

Public Shared Sub Main
Dim app as New Component1
Application.Run()
End Sub

Private Sub menuOptions_Click(...) Handles menuOptions.Click
' this would be your settings dialog.
Dim dialog as New OptionsDialog
dialog.ShowDialog()
dialog.Dispose()
End Sub

Private Sub menuExit_Click(...) Handles menuExit.Click
Me.Dispose()
Application.Exit()
End Sub

End Sub

The problem is you cannot edit the menu from the Component Designer. What I
do is use cut & paste from a Form Designer onto the Component Designer to
get the menu to the component...

The call to Me.Dispose enables the icon to be removed from the system tray
right away rather then waiting.

Hope this helps
Jay
 
Here is what I know that works....
1 - first of all use the notifyicon control if you want
it to run in the system try
2 - then on the form itself set its showintaskbar
property to false and/or min and max properties to false
as well (if you want)
3 - to hide the form put the code in
Protected Overloads Sub OnVisibleChanged(ByVal e As
System.EventArgs)

'other code if any....
'then...
if Me.Visible = True Then Me.Visible = False

'Other code if any...
End Sub

4 -
you can use "Protected Overrides Sub OnClick(ByVal e As
System.EventArgs)" to fire up the items in the context
menu.
5 - Use "Protected Overrides Sub OnLoad(ByVal e As
System.EventArgs)" instead of Form_load event.

This should work.
 
I ran into the same problem the other day. I found that if
you set a form as the startup you cannot hide it on
startup. So i defined in that form a Sub Main and set that
sub main as my startup.

Then I was able to hide it on startup with Me.Hide()

Shawn Regan
 
* "Shawn Regan said:
I ran into the same problem the other day. I found that if
you set a form as the startup you cannot hide it on
startup. So i defined in that form a Sub Main and set that
sub main as my startup.

Just call 'Application.Run' in a 'Sub Main' (set as startup object) and
show the window by instantiating it and calling its 'Show' method.
 
Back
Top