Link to home
Start Free TrialLog in
Avatar of fifo123
fifo123

asked on

Hide a windows form when application runs first

Hi all,
I am working on a windows form application.  I want the form to be invisible when the applicatioon first run and place a notify icon on the system tray.  When the user clicks on the icon on the system tray, I then want the form to display.  This has been harder to implement than I had expected.  I did try to add a separate startup class but I can't get access to the icon from this class.  Here is the framework...

any help will be appreciated.

namespace myApp
{
   public class frmmyapp : System.Windows.Forms.Form
  {
        private System.Windows.Forms.TextBox txtPath;
        private System.Windows.Forms.Button btnRun;
        private System.Windows.Forms.NotifyIcon notifyIcon1;

       public frmmyapp ( )
      {
         InitializeComponent();
         notifyIcon1.Icon = new Icon("App.ico");
         notifyIcon1.Text = "Application Manager";
         notifyIcon1.Visible = true;
      }
     private void notifyIcon1_Click(object sender, System.EventArgs e)
    {
           this.show()
    }
  }

  public class AppStart
  {
      public static void Main()
     {      
          frmmyapp x = new frmmyapp ( );
      }
  }
}

Avatar of RealMrTea
RealMrTea

I am not exactly sure what you are trying to do but to get access to your notification icon from your AppStart class all you need to do is make notifyIcon1 public instead of private (The simplest with what you got.):

public System.Windows.Forms.NotifyIcon notifyIcon1;

Hope that helps,
Eric
Avatar of fifo123

ASKER

What I'm trying to do is hide the form when the application first starts and show the form when the user clicks on the icon.

When I said "I can't get access to the icon" what I meant to say was when I click on the icon nothing happens...even though I have code for the onClick event of the icon.  The code that I have above creates the icon on the sys tray but nothing happens when a user clicks on the icon.

NOTE:  I have set the startup for this project to the class AppStart.
I got you...ok, then I do not see anywhere in your code where you are subscribing to the Click event of the Notify Icon unless it is not shown above.

this.notifyIcon1.Click += new System.EventHandle(this.notifyIcon1_Click);

When you debug it and put a break point at the notifyIcon1_Click method, does it stop?
I see your problem...you need to say Application.Run(x); in your Main of your AppStart class.  Without it the application is just ending and x is going out of scope and your application is closing.
Avatar of fifo123

ASKER

I do have code to subscribe to the click event; I just didn't show it here.

When I do Application.Run(x), the form gets launched.  So, this kind of defeats the purpose of what I'm trying to do.
Check out the following...I took the notify icon out of the frmmyapp and put it in AppStart.  I then made AppStart opacity = 0 so you won't see it.  Since it contains the notify icon you can then launch frmmyapp from the notify icon.  You will need to include a context menu or something to shut it down (or have your frmmyapp signal AppStart to shut down since the user will not be able to see AppStart.)

namespace myApp
{
      public class frmmyapp : System.Windows.Forms.Form
      {
            public frmmyapp()
            {
                  InitializeComponent();
            }

            private void InitializeComponent()
            {
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(440, 262);
                  this.Name = "frmmyapp";
                  this.Load += new System.EventHandler(this.frmmyapp_Load);
            }
      }

      public class AppStart : System.Windows.Forms.Form
      {
            private System.Windows.Forms.NotifyIcon notifyIcon1;
            private System.ComponentModel.IContainer components;

            public AppStart()
            {
                  this.Opacity = 0;

                  this.components = new System.ComponentModel.Container();
                  System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmmyapp));
                  this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

                  this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
                  this.notifyIcon1.Text = "notifyIcon2";
                  this.notifyIcon1.Visible = true;
                  this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);      
            }

            private void notifyIcon1_Click(object sender, System.EventArgs e)
            {
                  frmmyapp x = new frmmyapp();
                  x.Show();
            }

            public static void Main()
            {
                  Application.Run(new AppStart());
            }
      }
}

THat should work,
Eric
Avatar of fifo123

ASKER

Thanks Eric,

That works like a charm.  I did make this observation though.  If I changed the code like below, it still works.  So, I was wondering what the advantage is in using the container object.  

 public class AppStart : System.Windows.Forms.Form
     {
          private System.Windows.Forms.NotifyIcon notifyIcon1;

          public AppStart()
          {
               this.Opacity = 0;

                this.notifyIcon1 = new NotifyIcon();
      this.notifyIcon1.Icon = new Icon("App.ico");
      this.notifyIcon1.Text = "App Manager";
      this.notifyIcon1.Visible = true;
      this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);  
          }

          private void notifyIcon1_Click(object sender, System.EventArgs e)
          {
               frmmyapp x = new frmmyapp();
               x.Show();
          }

          public static void Main()
          {
               Application.Run(new AppStart());
          }
     }


ASKER CERTIFIED SOLUTION
Avatar of RealMrTea
RealMrTea

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I know this question is already answered and all  butttt.

this.Hide() will hide the window until this.Show() is called again, and disposes all visible controls thus not using any system resources until it is shown again, rather than having all controls loaded and not visible with opacity.

Just depends on what your looking to accomplish, figured Id point out an alternate too :)

-jaynus