Results 1 to 10 of 10

Thread: [RESOLVED] Form hide on startup

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Resolved [RESOLVED] Form hide on startup

    hi guys! I put this code
    Code:
    this.hide();
    under Form_load to make my form hide when it is loaded but it didn't work. Is there something I missed or it is not the right way to implement it?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Form hide on startup

    The issue is that the form is Visible by default. So when you load a form it has to show before it can do anything else. Upon completion of the load event you can use another event and call your hide code.

    Perhaps placing your code in the Form1_Activated or Form1_VisibleChanged events.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: Form hide on startup

    hide isn't the thing to use. try this.Visible = false;

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

    Re: Form hide on startup

    It is not possible to hide a form, either by calling Hide or by setting Visible to false, in the form's Load event handler. That's because once the Load event is raised, the form is going to be visible when it completes no matter what. When I say "visible" I mean that it's Visible property will be true. In C# 2005 you would use this code:
    Code:
    private void Form1_Load(object sender, EventArgs e)
    {
        // Minimise the form so that the user can't see it.
        this.WindowState = FormWindowState.Minimized;
    
        // Don't show the form in the task bar.
        this.ShowInTaskbar = false;
    }
    
    // The Shown event is raised immediately after the Load event completes and the form becomes visible.
    private void Form1_Shown(object sender, EventArgs e)
    {
        // Now it is possible to hide the form for real.
        this.Visible = false;
    
        // Set the WindowSate back to Normal here if desired.
    }
    Note that the WindowState and ShowInTaskbar properties can be set in the designer rather than using the Load event handler. In previous versions of C# you would have to use the Activated event rather than Shown, which is new in .NET 2.0. In that case you would require a class-level boolean variable to remember whether this was the first time the form was activated.
    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

  5. #5

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Form hide on startup

    Sorry guys i think i have not explain it well. anyway, I set from registry to run my application automatically when windows start, what im trying to do is to hide the form when it is loaded when windows start but it still can be shown when the users double click the icon from the system tray. In other words, what im trying to do is like MSN messenger which load it self into system tray with out showing the main form.

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

    Re: Form hide on startup

    Quote Originally Posted by daimous
    Sorry guys i think i have not explain it well. anyway, I set from registry to run my application automatically when windows start, what im trying to do is to hide the form when it is loaded when windows start but it still can be shown when the users double click the icon from the system tray. In other words, what im trying to do is like MSN messenger which load it self into system tray with out showing the main form.
    No problem. You simply use the code I provided. I have done exactly the same thing in a previous app of my own. My app had the option to run at startup and if that was selected then there was an option to run minimised. If the user selected the option to run at startup I would add a value to the Run key in the registry. If the user also selected the minimised option then I'd add a "-min" argument to the commandline stored in the registry. In my app I used Environment.GetCommandLineArgs to check whether the commandline included the "-min" switch. If it did then I executed code much like I posted previously. My app was in VB.NET 2003, so I didn't have the Shown event and had to use the Activated event instead. With Shown it's easier.
    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

  7. #7

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Form hide on startup

    oh I see..but, does "-min" argument hide my form or just minized it to taskbar?

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

    Re: Form hide on startup

    "-min" doesn't do anything. It's just a string. You can pass any commandline argument you like to any executable you like. It's how the code inside handles those arguments that gives them significance. If an application has no code to handle commandline arguments then they will simply be ignored, no matter their number or value. If you want the "-min" swicth to be significant then it's up to you to write the code to make it so. You use the GetCommandLineArgs method get the commandline arguments for the application. If you find "-min" then you execute the code that performs the function that you want to perform. If you just want to minimise the app then just minimise it. If you want to display it only in the system tray then only display it in the system tray. You know how to do both.
    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

  9. #9

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Form hide on startup

    Ok..Thanks a bunch!

  10. #10

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Form hide on startup

    Thanks for the inputs guys. Now it is working the way i want it.

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