?
Solved

Hide a windows form when application runs first

Posted on 2005-03-08
9
Medium Priority
?
213 Views
Last Modified: 2009-12-16
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 ( );
      }
  }
}

0
Comment
Question by:fifo123
  • 5
  • 3
9 Comments
 
LVL 7

Expert Comment

by:RealMrTea
ID: 13490014
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
0
 

Author Comment

by:fifo123
ID: 13490189
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.
0
 
LVL 7

Expert Comment

by:RealMrTea
ID: 13490271
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:RealMrTea
ID: 13490389
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.
0
 

Author Comment

by:fifo123
ID: 13490637
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.
0
 
LVL 7

Expert Comment

by:RealMrTea
ID: 13490882
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
0
 

Author Comment

by:fifo123
ID: 13491413
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());
          }
     }


0
 
LVL 7

Accepted Solution

by:
RealMrTea earned 500 total points
ID: 13495939
None...this is accomplishing the same thing.  In this example you have done, AppStart is your container object and frmmyapp is just the form you want to show.  (The same thing.)

Later,
Eric
0
 
LVL 4

Expert Comment

by:jaynus
ID: 13497340
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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question