How to hide the first form when the application starts

I have an application with a form, with two text boxes as an input of a username and a password. I would like to do that, if the user check a checkbox called "Remember me", at the start, the form isn't shown, but only the systray icon. I got the systray icon to work correctly, but i can't hide the form when the application starts. I tried to associate at the Form_Load event the code this.hide() or this.visible=false, but nothing, the form is still here.
Can you help me



Answer this question

How to hide the first form when the application starts

  • xjay

    is this in reference to the initial question 2 different users.... (confused)

    you should be doing:

    this.Show(); //this is to show the form and should be used

    or

    this.Activate(); //then to activate it

    or

    this.BringToFront(); //and bring it to the front of the other windows open

    not the Form. object



  • Derek Smyth

    qmatteoq wrote:

    my problem is that, if i choose to save username and password and do the autologin, i want the Form1 hidden. I tried to associate the code this.Hide() to the event "Form Load", but it doesn't work. Form1 is shown every time the application starts, even if the autologin is activated.

    something is seriously wrong there in that case. What else are you doing on the form_load events or even in a global scope level Because I can hide the form no problem....



  • KatyG

    ahmedilyas wrote:

    is this in reference to the initial question 2 different users.... (confused)

    you should be doing:

    this.Show(); //this is to show the form and should be used

    or

    this.Activate(); //then to activate it

    or

    this.BringToFront(); //and bring it to the front of the other windows open

    not the Form. object

    Sorry Ilyas I was so excited to get it working becuase when I had to do this thing I searched a lot but never got it working then I used my tricky way to set WindowState to Minimized and ShowInTaskBar to false, but that sucked. This is a really good way that the other man explained and sorry for reposting it with edition 3 different time. I checked it and It works like a Charm!!! Thank you so much! I learnd a new thing today (A)

    Best Regards,



  • h3mp

    SetVisibleCore is the method called when you use Visible property or the Show/Hide methods. From the MSDN docs:

    "Sets the control to the specified visible state. "

    "You would typically override this method to change the visibility behavior of the control."

    "Notes to Inheritors When overriding SetVisibleCore in a derived class, be sure to call the base class's SetVisibleCore method to force the visibility of the control to change. "

    Since Application.Run makes the main form visible all you can do is to lie to the base class and say you don't want it to be visible.

    Altough this method is documented I still consider its usage in this case to be more like a hack. I'd say it's better to not create the Form at all and call the Application.Run() without parameters.


  • Deffie

    Application.Run forces your form to be visible. The best thing to do would be to not create the form at all and just call Application.Run() (with no parameters) in the Program.Main function.

    If you placed the NotifyIcon on the Form this might not work (the icon won't be shown) and you either need to move the NotifyIcon creation in Program.Main just before calling Application.Run() or you can hack the form a bit:

    protected override void SetVisibleCore(bool value)

    {

    if (we don't want the form to be visibile at startup)

    base.SetVisibleCore(false);

    else

    base.SetVisibleCore(value);

    }


  • madenci

    1) Setting Visible property to false in constructor:

    It does not work because later Application.Run(form) will set it back to true.

    2) Setting Visible property to false in the Load event:

    It does not work because Load event is raised as a result of setting the Visible property to true (see above). In terms of Win32 API what happens is that the form receives WM_SHOWWINDOW message, raised the Loaded even as a result and in it (so while processing WM_SHOWWINDOW) it calls ShowWindow(hWnd, SW_HIDE) API. But since WM_SHOWWINDOW is still being processed the window is not yet visible and ShowWindow(hWnd, SW_HIDE) will do nothing, it is simply ignored by the window manager.

    A note:

    In case someone does not know already, calling the Close method in the Load event is a bad idea (if it works at all). It is documented that it can cause memory leaks and the reason and the reason should be something similar. You are trying to destroy a window that is in process of being created.

    Even if Visible would work in the Load event you'll probably get a bad user experience because the form will be visible for a very short period and then disappear.

    3) I consider using SetVisibleCore (in the this way, other uses may be valid) to be a hack because you are altering it's behavior. It makes the Visible property behaves odd. You set it to true but then you check its value later you get false. What happens if a future version of Application.Run relies on the fact that it has already made the form visible

    Besides, why creating a form to hide it a second later Better not create it at all from the start ! (Of course 3) is just my personal opinion )


  • Scott Chang

     Mike Danes wrote:

    Application.Run forces your form to be visible. The best thing to do would be to not create the form at all and just call Application.Run() (with no parameters) in the Program.Main function.

    If you placed the NotifyIcon on the Form this might not work (the icon won't be shown) and you either need to move the NotifyIcon creation in Program.Main just before calling Application.Run() or you can hack the form a bit:

    protected override void SetVisibleCore(bool value)

    {

    if (we don't want the form to be visibile at startup)

    base.SetVisibleCore(false);

    else

    base.SetVisibleCore(value);

    }

    Great!!!!!!! I never found this trick ever even on MSDN! Can you guide a little that what Basically SetVisibleCore()  does



  • tongkusat

    I'll try to explain myself better. I have a normal application window, with a form (called Form1) that is the form shown when the application starts. In this form, the user type his username and password. If the user checks the checkbox "Remember me next time", i want the program to autologin. My problem isn't how to save these informations (i can use a text or a xml file): my problem is that, if i choose to save username and password and do the autologin, i want the Form1 hidden. I tried to associate the code this.Hide() to the event "Form Load", but it doesn't work. Form1 is shown every time the application starts, even if the autologin is activated.


  • kawano1h

    Mike Danes wrote:

    SetVisibleCore is the method called when you use Visible property or the Show/Hide methods. From the MSDN docs:

    "Sets the control to the specified visible state. "

    "You would typically override this method to change the visibility behavior of the control."

    "Notes to Inheritors When overriding SetVisibleCore in a derived class, be sure to call the base class's SetVisibleCore method to force the visibility of the control to change. "

    Since Application.Run makes the main form visible all you can do is to lie to the base class and say you don't want it to be visible.

    Altough this method is documented I still consider its usage in this case to be more like a hack. I'd say it's better to not create the Form at all and call the Application.Run() without parameters.

    You said that SetVisibleCore when we use Show/Hide Methods or change Visible property then why we cant Hide Form at start up like in Form Constructor using this.Visible = false, According to your statement it calls SetVisibleCore then we should get the same affects from both the ways, Why its not like that

    Secondly why do you prefere to to Start Application with Run method without parameters and leaving this Tweak what makes you think that the later method is better.

    Its interesting, I hope to get answers for these two questions!

    Thaaaaaanks alot!



  • mr4100

    there must be something causing it to still show even if you did this.Hide(). Are you meaning that you wish to save the setting next time the application starts up so if the user clicks "remember me", that it should hide the form the next time it shows up and automatically log in if so, you need to look at something like application settings, and storing the user credentials in say, an xml file or something (encrypted of course for security then decrypt it the next time you need to authenticate)

    Can you explain further about the whole "form still doesnt hide"



  • How to hide the first form when the application starts