Results 1 to 9 of 9

Thread: Some trouble starting an application minimized to the tray

  1. #1
    ikantspelwurdz is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 3.5
    Join Date
    Dec 2009
    Posts
    49
    Reputation
    91

    Some trouble starting an application minimized to the tray

    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:
    Code:
        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:
    Code:
    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?

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,083
    Reputation
    2803
    Just set the three initial states correct in designer, no need for code in Load handler.
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  3. #3
    ikantspelwurdz is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 3.5
    Join Date
    Dec 2009
    Posts
    49
    Reputation
    91
    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.

  4. #4
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,468
    Reputation
    1015
    Quote Originally Posted by ikantspelwurdz View Post
    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:
    Code:
        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:
    Code:
    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?
    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?
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.

  5. #5
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,083
    Reputation
    2803
    Quote Originally Posted by ikantspelwurdz View Post
    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.
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  6. #6
    ikantspelwurdz is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 3.5
    Join Date
    Dec 2009
    Posts
    49
    Reputation
    91
    Quote Originally Posted by JohnH View Post
    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.

    Quote Originally Posted by JuggaloBrotha View Post
    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.

  7. #7
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,083
    Reputation
    2803
    Quote Originally Posted by ikantspelwurdz View Post
    If I don't do this, then it will show up when I Alt+Tab. I don't want that.
    True, I didn't notice that now.
    How would I go about starting the application without the form? Under my application settings, the "Startup form" field is mandatory.
    See for example this: Create a System Tray Application in VB.NET - CodeProject
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  8. #8
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,468
    Reputation
    1015
    Quote Originally Posted by ikantspelwurdz View Post
    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.
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.

  9. #9
    ikantspelwurdz is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 3.5
    Join Date
    Dec 2009
    Posts
    49
    Reputation
    91
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •