Some trouble starting an application minimized to the tray

ikantspelwurdz

Well-known member
Joined
Dec 8, 2009
Messages
49
Programming Experience
1-3
I wrote an application that by design, when minimized, goes to the tray, and will only show up as a normal form when you doubleclick the icon in the tray.

To do this, I added a NotifyIcon, and added these functions:
VB.NET:
    Private Sub Form1_Resize(sender As System.Object, e As System.EventArgs) Handles MyBase.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            NotifyIcon1.Visible = True
            ShowInTaskbar = False
            Me.Hide()
        End If
    End Sub

    Private Sub NotifyIcon1_DoubleClick(sender As System.Object, e As System.EventArgs) Handles NotifyIcon1.DoubleClick
        Me.Show()
        Me.WindowState = FormWindowState.Normal
        NotifyIcon1.Visible = False
        ShowInTaskbar = True
    End Sub

This works fine, but I want the program to start minimized to the tray. So I added this line to the very end of Form1_Load:
VB.NET:
Me.WindowState = FormWindowState.Minimized

When I run my program in debug mode within Visual Studio, this works fine.

When I run the exe directly, the form does not hide properly. It "minimizes" by resizing to a very small size, and looks like this, fixed in the lower-left corner of my screen:
minwin.png

If I click the restore button, it restores and the restore button turns into a minimize button. If I click the minimize button, it minimizes to the tray correctly. But I want it to start that way. What is going wrong and how do I fix it?
 
Just set the three initial states correct in designer, no need for code in Load handler.
 
Which are the three?

I think I got it working as I want it, but I needed code in the load handler and the designer. In the designer, I set these properties on the form:
ShowInTaskbar=False
WindowState=Minimized

And in Form1_Load, I changed the last line to this:
Me.Hide()

If I don't do that last thing, then the program is visible when I Alt+Tab, and I don't want that.
 
I wrote an application that by design, when minimized, goes to the tray, and will only show up as a normal form when you doubleclick the icon in the tray.

To do this, I added a NotifyIcon, and added these functions:
VB.NET:
    Private Sub Form1_Resize(sender As System.Object, e As System.EventArgs) Handles MyBase.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            NotifyIcon1.Visible = True
            ShowInTaskbar = False
            Me.Hide()
        End If
    End Sub

    Private Sub NotifyIcon1_DoubleClick(sender As System.Object, e As System.EventArgs) Handles NotifyIcon1.DoubleClick
        Me.Show()
        Me.WindowState = FormWindowState.Normal
        NotifyIcon1.Visible = False
        ShowInTaskbar = True
    End Sub

This works fine, but I want the program to start minimized to the tray. So I added this line to the very end of Form1_Load:
VB.NET:
Me.WindowState = FormWindowState.Minimized

When I run my program in debug mode within Visual Studio, this works fine.

When I run the exe directly, the form does not hide properly. It "minimizes" by resizing to a very small size, and looks like this, fixed in the lower-left corner of my screen:
View attachment 4348

If I click the restore button, it restores and the restore button turns into a minimize button. If I click the minimize button, it minimizes to the tray correctly. But I want it to start that way. What is going wrong and how do I fix it?
I can see two ways to resolve this, so I'm going to start with asking is there any reason you need the program to run from the "form"?

As in, could this be a program that runs from Sub Main(), not as a console app though, but it shows the NotifyIcon from Sub Main and if the users double clicks on the icon, if the main "form" doesn't exist yet, it builds it and displays it at that time?

Or are you doing something that involves having the form existing, like processing happens in a background worker, is timer based, and/or something that would be too difficult to convert off of running from the form?
 
Which are the three?

I think I got it working as I want it, but I needed code in the load handler and the designer. In the designer, I set these properties on the form:
ShowInTaskbar=False
WindowState=Minimized

And in Form1_Load, I changed the last line to this:
Me.Hide()
Me.WindowState
NotifyIcon1.Visible
ShowInTaskbar

No need for Me.Hide in Load.
 
No need for Me.Hide in Load.
If I don't do this, then it will show up when I Alt+Tab. I don't want that.

I can see two ways to resolve this, so I'm going to start with asking is there any reason you need the program to run from the "form"?
Maybe not. How would I go about starting the application without the form? Under my application settings, the "Startup form" field is mandatory.
 
If I don't do this, then it will show up when I Alt+Tab. I don't want that.


Maybe not. How would I go about starting the application without the form? Under my application settings, the "Startup form" field is mandatory.
Uncheck the "Enable application framework" option, then you can have a Windows Forms application starting from Sub Main.

It sounds like you've got the initial issue with your post figured out though and what I'm suggesting would be a big paradigm shift.
 
Yeah, I've got it working like I want it by including Me.Hide in the form loader.

I did try that approach of not using the application framework, but I want to do stuff like have the application close when you close the form, and it already does that. Easier just to put Me.Hide in the form loader than try to move a bunch of stuff that already works out of the form class.
 

Latest posts

Back
Top