Results 1 to 20 of 20

Thread: [RESOLVED] [2005] Me.visible = false not working on form load

  1. #1

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Resolved [RESOLVED] [2005] Me.visible = false not working on form load

    Hi there,

    I am very new to VB.net but not programming per se. However, something has me stumped and I have been as yet unable to find a solution.
    I created a very simple form that when loaded should not be visible but have a little systray icon only. The systray icon part works, however the form is not being hidden. This is the code (all of it) that *should* work:

    Code:
    Public Class Form1
        Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
            MsgBox("You clicked on the notification icon")
            Me.Visible = True
        End Sub
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Me.Visible = False
            NotifyIcon1.Visible = True
        End Sub
    End Class
    So why is this not working? Am I missing something here? I took the Me.Visble = False trick from a piece of code I found on the web (link)

    If someone could point me in the right direction that would be great!

    Thanks in advance.
    Mighor

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Me.visible = false not working on form load

    Use the Form Shown event of the form instead
    Code:
    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
            Me.Visible = False
            NotifyIcon1.Visible = True
        End Sub

  3. #3

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] Me.visible = false not working on form load

    I tried the above code but what happens now is that the form shows for a millisecond and then disappears quickly. What I basically want is the form to not show at all when it is first started up.
    I need to make it invisible before it is show, if you know what I mean

  4. #4
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2005] Me.visible = false not working on form load

    Put 'Me.WindowState = FormWindowState.Minimized' in the form_load event. If you also don't want it in the taskbar, use Me.ShowInTaskbar = False.
    VB 2005, Win Xp Pro sp2

  5. #5

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] Me.visible = false not working on form load

    Thanks, that did the trick!
    I now have the following code and it seems to do exactly what I need it to do:
    Code:
    Public Class Form1
        Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
            MsgBox("You clicked on the notification icon")
            Me.Visible = True
            Me.ShowInTaskbar = True
            Me.WindowState = FormWindowState.Normal
        End Sub
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Me.Visible = False
            Me.ShowInTaskbar = False
            Me.WindowState = FormWindowState.Minimized
        End Sub
    
        Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
            Me.Visible = False
            NotifyIcon1.Visible = True
        End Sub
    End Class

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Me.visible = false not working on form load

    Don't forget to make your project as a Single Instance application, other wise you will see minimized main form instances. Also you don't need to set the Visible property if you use ShowInTaskbar property.

  7. #7

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] Me.visible = false not working on form load

    Quote Originally Posted by VBDT
    Don't forget to make your project as a Single Instance application, other wise you will see minimized main form instances.
    My application will only have one single window (form). Is this what you meant by Single Instance application?

  8. #8
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Me.visible = false not working on form load

    I mean single instance application. For example when you start your app by clicking the app shortcut icon on your desktop and after it opens you click the shortcut the secont time it will start your app but a diferent instance so you will see two of your forms.

  9. #9

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] Me.visible = false not working on form load

    Quote Originally Posted by VBDT
    I mean single instance application. For example when you start your app by clicking the app shortcut icon on your desktop and after it opens you click the shortcut the secont time it will start your app but a diferent instance so you will see two of your forms.
    I see what you mean now. I am not sure how to achieve this. How is this done under Windows and more specifically in Visual Basic? There should indeed be no more than one instance of this application.

  10. #10
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Me.visible = false not working on form load

    Go to your project properties and check the checkbox for "Make Single Instance Application".

  11. #11

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] Me.visible = false not working on form load

    Quote Originally Posted by VBDT
    Go to your project properties and check the checkbox for "Make Single Instance Application".
    Haha, ok that was a lot easier than I thought it would be I was already googling for some example code to achieve this and I came across a lot of talk about mutexes and other IPC stuff. I am glad a tickbox is all that is required.

  12. #12
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2005] Me.visible = false not working on form load

    One thing in case you decide to expand your project later on - there is some unexpected behaviour when a form is not shown in the taskbar. For example such a form won't fire the FormClosing event last time I checked.

    If you notice strange things happening, first test your thing with ShowInTaskbar set to true.
    VB 2005, Win Xp Pro sp2

  13. #13
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Me.visible = false not working on form load

    Quote Originally Posted by Half
    One thing in case you decide to expand your project later on - there is some unexpected behaviour when a form is not shown in the taskbar. For example such a form won't fire the FormClosing event last time I checked.

    If you notice strange things happening, first test your thing with ShowInTaskbar set to true.
    Yes that is true. It all depends how you exit the application. If you use Application.Exit than it will not fire the FormClosing event, instead use Me.Close.

  14. #14

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] Me.visible = false not working on form load

    Quote Originally Posted by VBDT
    Yes that is true. It all depends how you exit the application. If you use Application.Exit than it will not fire the FormClosing event, instead use Me.Close.
    Thanks, I will keep that in mind as I expand on this project.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Me.visible = false not working on form load

    Quote Originally Posted by VBDT
    Also you don't need to set the Visible property if you use ShowInTaskbar property.
    Not true. If you have a minimised form that is not in shown in the task bar it will still appear in the Alt+Tab list. Selecting the form using Alt+Tab will restore it. Not what you want.

    What you should do is get rid of the entire Form.Load event handler. You should be setting the WindowState and ShowInTaskbar properties of the form and the Visible property of the NotifyIcon in the designer. All that's left to do then is set the Visible property of the form to False in the Shown event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Me.visible = false not working on form load

    Quote Originally Posted by VBDT
    Yes that is true. It all depends how you exit the application. If you use Application.Exit than it will not fire the FormClosing event, instead use Me.Close.
    Also, not true I'm afraid. Application.Exit does not raise the Closing and Closed events but it does raise the FormClosing and FormClosed events. FormClosing provides the e.CloseReason property so you know how the closure was instigated in the first place. ApplicationExitCall is one of those reasons.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Me.visible = false not working on form load

    Quote Originally Posted by jmcilhinney
    Not true. If you have a minimised form that is not in shown in the task bar it will still appear in the Alt+Tab list. Selecting the form using Alt+Tab will restore it. Not what you want.

    What you should do is get rid of the entire Form.Load event handler. You should be setting the WindowState and ShowInTaskbar properties of the form and the Visible property of the NotifyIcon in the designer. All that's left to do then is set the Visible property of the form to False in the Shown event handler.
    I just tested it and you are right! I didn't notice that.

  18. #18
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Me.visible = false not working on form load

    Quote Originally Posted by jmcilhinney
    Also, not true I'm afraid. Application.Exit does not raise the Closing and Closed events but it does raise the FormClosing and FormClosed events. FormClosing provides the e.CloseReason property so you know how the closure was instigated in the first place. ApplicationExitCall is one of those reasons.
    It is true if the app's main window has its ShowInTaskbar property set to true but if the property is set to false and we call Application.Exit method then for some reason the FormClosing and FomClosed events doesn't fire.

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Me.visible = false not working on form load

    Quote Originally Posted by VBDT
    It is true if the app's main window has its ShowInTaskbar property set to true but if the property is set to false and we call Application.Exit method then for some reason the FormClosing and FomClosed events doesn't fire.
    That I didn't know. One all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  20. #20

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] Me.visible = false not working on form load

    I am not sure how to do this but as far as I am concerned this thread can be marked as [RESOLVED]. Thanks for all the insights, tips and tricks, people!

Posting Permissions

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



Click Here to Expand Forum to Full Width