?
Solved

Hide a windows form when application runs first

Posted on 2005-03-08
9
Medium Priority
?
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

765 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