By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 1,966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

Hiding main window at startup

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
"Tony Vitonis" <no***@nowhere.com> wrote in message
news:MP************************@news.md.comcast.gi ganews.com... 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.

Nov 20 '05 #2

P: n/a
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.

-----Original Message-----
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 userselects "Settings..." from the icon's context menu, to display awindow 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 Loadevent, but apparently that code runs before the command can beeffective. I moved the line to the Activate event, which hid the format startup just fine; however, when I tried to set Me.Visible to Truein another part of the code, I saw the Form.Activate event runningagain, re-hiding the form.

On my second try, I created a form-level variable, WindowVisible, putMe.Visible = WindowVisible in the Form.Activate event, and setWindowVisible in the code where necessary. This works fine, but itseems a little kludgy to me, as if I'm trying to work around somethingI 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 areraised, and for what reasons?

Thanks a lot.
.

Nov 20 '05 #3

P: n/a
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
-----Original Message-----
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 userselects "Settings..." from the icon's context menu, to display awindow 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 Loadevent, but apparently that code runs before the command can beeffective. I moved the line to the Activate event, which hid the format startup just fine; however, when I tried to set Me.Visible to Truein another part of the code, I saw the Form.Activate event runningagain, re-hiding the form.

On my second try, I created a form-level variable, WindowVisible, putMe.Visible = WindowVisible in the Form.Activate event, and setWindowVisible in the code where necessary. This works fine, but itseems a little kludgy to me, as if I'm trying to work around somethingI 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 areraised, and for what reasons?

Thanks a lot.
.

Nov 20 '05 #4

P: n/a
* "Shawn Regan" <an*******@discussions.microsoft.com> scripsit:
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.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.