locked
How to start a form in hidden mode? RRS feed

  • Question

  • I want to make a program which could be started in the hidden mode.ie the form should not be visible as the program loads, which can be made visible later on if needed. I tried to make this happen by making the visible property of the form to false in the constructor of the form, but this is not helping. The code snippet is:
    //constructor
    public Form1()
    {
       this.Visible=false;
    }
    //Main() method
    Application.Run(new Form1());

    This code displays the form. So can anybody suggest me the way to hide the form as the program loads??
    Thanks!
    Friday, December 2, 2005 7:17 PM

All replies

  • //Main() method
    Form1 f;

    f = new Form1();
    f.Initialize();
    Application.Run(f);
    Friday, December 2, 2005 9:27 PM
  • what is that Initialize() method?
    I dont think it is a built in function. Wot does tht function do in this code?
    Monday, December 5, 2005 6:42 PM
  • a quick and dirty method is to just start with the form position off screen. (with show in taskbar set to false - try (-2000,-2000) etc)

    when you show it move it back on screen

    after that just use hide/show.


    Tuesday, December 6, 2005 2:10 PM
  • Keep the line this.Visible = false in the form's constructor.

    Change

    Application.Run(new Form1());

    To

    Form1 form = new Form1();

    Application.Run();

    Friday, March 2, 2007 12:57 PM
  • Hi,

    - set the WindowState to Minimized on the Form properties

    - add the Form's Load event handler, in which you hide the form as follow:

     

    private void MainForm_Load(object sender, EventArgs e)

    {

    Hide();

    }

     

    regards,

    Cipri

    Monday, December 17, 2007 9:49 AM
  • Hi,

    - set the WindowState to Minimized on the Form properties

    - add the Form's Load event handler, in which you hide the form as follow:

     

    private

     

    void MainForm_Load(object sender, EventArgs e)

    {

    Hide();

    }

     

    regards,

    Cipri



    Another way, I think, is to...

    - set the WindowState to Minimized on the Form properties

    - set the ShowInTaskbar to "false" on the Form properties

    ...and that works too, it seems to me.

    I think it may not have the exact same effect as using Hide(); but, for me, it is great because I can still use Alt+Tab to find the window if I want it, which is exactly what I am trying to achieve because I do not ever want my application to be entirely invisible.

    HTH.

    Thank you.

    -- Mark Kamoski



    mkamoski
    Thursday, December 17, 2009 5:04 PM
  • private bool mShowAllowed;
    protected override void SetVisibleCore(bool value) {
      if (!mShowAllowed) value = false;
      base.SetVisibleCore(value);
    }
    • Proposed as answer by PowerMosfet Wednesday, October 27, 2010 3:12 PM
    Thursday, December 17, 2009 9:42 PM
  • When you start the program with Application.Run(new Form1()) the form always shows.

    You need to use the overload form with ApplicationContext.  Add the following code to your program.cs

    class HiddenContext : ApplicationContext
    {
      public HiddenContext()
      {
        Form1 form1 = new Form1();
        form1.Visible = false;
        form1.FormClosed += new System.Windows.Forms.FormClosedEventHandler(form1_FormClosed);
        form1.FormClosing += new FormClosingEventHandler(form1_FormClosing);
      }
    
      void form1_FormClosing(object sender, FormClosingEventArgs e)
      {
        if (MessageBox.Show("Are you sure you want to close the application?", "Application Closing", MessageBoxButtons.YesNo) == DialogResult.No)
          e.Cancel = true;
      }
    
      void form1_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
      {
        this.ExitThread();
      }
    }
    
    

    Now replace the run code as follows

    Application.Run(new HiddenContext());
    

    You may want to find a different way to open the form when needed, such as from a notification icon or timer event. 

    • Proposed as answer by PowerMosfet Wednesday, October 27, 2010 3:11 PM
    Thursday, August 26, 2010 2:49 AM
  • private bool mShowAllowed;
    protected override void SetVisibleCore(bool value) {
     if (!mShowAllowed) value = false;
     base.SetVisibleCore(value);
    }
    
    Perfect! Clean Solution!
    Thursday, April 21, 2011 12:54 PM