6

my app is for chatting, and i think if someone needs to hide it quick, but doesn't want to close it, i came up with this:

private void button6_Click(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Minimized; 
}

however, instead of going to the taskbar, i want it to appear (no popup) in the tray, just the apps icon, and when someone clicks it it needs to set this

this.WindowState = FormWindowState.Normal; 

Is this possible, how?

Also by system tray i mean the one in the bottom right corner, next to the time

I still can't get this to work, nothing appears in the notification bar if I do what you guys said (btw: this is the full code to minimise)

private void button6_Click(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Minimized;


}

private void Form_Resize(object sender, EventArgs e)
{
    if (WindowState == FormWindowState.Minimized)
    {
        this.Hide();
    }


}

private void notifyIcon_Click(object sender, EventArgs e)
{
    this.Show();
    this.WindowState = FormWindowState.Normal;
}

Why isn't this working?

Freelancer
  • 9,008
  • 7
  • 42
  • 81
Connor
  • 657
  • 1
  • 7
  • 23
  • 1
    There is no such thing as a "system tray". It's called the "notification area". That should help you to find the correct component for implementing this, the `NotifyIcon`. – Cody Gray - on strike Apr 22 '13 at 06:40

2 Answers2

8

Handle the form’s Resize event. In this handler, you override the basic functionality of the Resize event to make the form minimize to the system tray and not to the taskbar. This can be done by doing the following in your form’s Resize event handler:

  1. Check whether the form’s WindowState property is set to FormWindowState.Minimized. If yes, hide your form, enable the NotifyIcon object, and show the balloon tip that shows some information.
  2. Once the WindowState becomes FormWindowState.Normal, disable the NotifyIcon object by setting its Visible property to false.
  3. Now, you want the window to reappear when you double click on the NotifyIcon object in the taskbar. For this, handle the NotifyIcon’s MouseDoubleClick event. Here, you show the form using the Show() method.

In the form resize event, do the check there and hide the form

 private void Form_Resize(object sender, EventArgs e)
    {
        if (WindowState == FormWindowState.Minimized)
        {
            this.Hide();
        }
    }

Then when clicking on the taskbar icon just restore it.

private void notifyIcon_Click(object sender, EventArgs e)
{
    this.Show();
    this.WindowState = FormWindowState.Normal;
}

Refer:
How do I minimize a WinForms application to the notification area?
minimize app to system tray

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • SO should have an option to mark duplicate answers too! http://stackoverflow.com/a/6317078/762730 – Sandeep Apr 22 '13 at 06:31
  • @Sandeep: he want to hide the popup also not exactly the same question otherwise i always happy to do this work first rather than writing here.. well thanks for your suggestion. – Niranjan Singh Apr 22 '13 at 06:33
  • ok I must be missing something, i don't know what it is so if you could spell it out fully for me, because im stupid, what is the full code i put in, also i want to Get the app to the the noticications bar first and i don't want it to have a popup, or to hide the popup – Connor Apr 22 '13 at 07:00
  • so what they are doing is hiding your form, and showing the notify icon. This makes it appear that your form is in the tray, but rather it is just hiding the form. this is the closest you can get to putting a program in the tray. – Cullub Aug 01 '14 at 13:14
5

Use following code:

if (WindowState == FormWindowState.Minimized)
        {
            this.Hide();
        }

When you minimize the form, simply hide it. You will have to implement above code in Form_Resize event.

Then on clicking taskbar icon just restore its state as follows:

private void notifyIcon_Click(object sender, EventArgs e)
    {
        this.Show();
        this.WindowState = FormWindowState.Normal;
    }

You will need to use notifyIcon_Click event for this purpose.

Hope its helpful.

Freelancer
  • 9,008
  • 7
  • 42
  • 81