11 Replies - 1473 Views - Last Post: 14 April 2012 - 11:32 AM Rate Topic: -----

#1 RandomlyKnighted  Icon User is offline

  • Microsoft Senior Student Partner
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,318
  • Joined: 14-January 10

Minimize To System Tray?

Posted 12 April 2012 - 11:44 PM

Hello everyone,

I'm currently trying to get my program to minimize to the system tray, because the software need to be able to perform a task in the background. Currently, the program does create the icon in the system tray. The only problem I'm having right now is the program wants to stay in both the taskbar and the system tray when minimized. I'm not sure what else to do. Can someone please look it over and tell me if I'm missing something?

        #region Minimize to System Tray
        public void Form1_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == this.WindowState)
            {
                notifyIcon1.Visible = true;
                ShowInTaskbar = false;
            }

            else if (FormWindowState.Normal == this.WindowState)
            {
                notifyIcon1.Visible = false;
                this.ShowInTaskbar = true;
            }
        }

        #endregion



I'm not quite sure how it's even creating the icon in the system tray, because I can't seem to figure out how to tell the program to use this function when I press the minimize button. Am I misunderstanding something? Any help will be greatly appreciated.

Is This A Good Question/Topic? 1
  • +

Replies To: Minimize To System Tray?

#2 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Minimize To System Tray?

Posted 13 April 2012 - 05:17 AM

We use very similar code and it works as you described, as in it doesn't really.
Tried to resolve it myself while back but I never got around to implementing a solution, as it was not that important in my case.
(//TODO:...)

I did find some good explanations on the article/comments about why ShowInTaskbar doesn't really work though. May be useful if you didn't stumble across it yourself :)
http://blogs.msdn.co.../20/483300.aspx

This post has been edited by negligible: 13 April 2012 - 05:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: Minimize To System Tray?

Posted 13 April 2012 - 06:18 AM

Hi, the following code below works for me:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Minimized)
        {
            ShowInTaskbar = false;
            notifyIcon1.Visible = true;
        }
        else
        {
            ShowInTaskbar = true;
            notifyIcon1.Visible = false;
        }
    }

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



Hope it helps :)
Was This Post Helpful? 0
  • +
  • -

#4 RandomlyKnighted  Icon User is offline

  • Microsoft Senior Student Partner
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,318
  • Joined: 14-January 10

Re: Minimize To System Tray?

Posted 13 April 2012 - 07:44 AM

I've updated my code to this:

        #region Minimize to System Tray
        public void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                notifyIcon1.Visible = true;
                ShowInTaskbar = false;
            }

            else
            {
                notifyIcon1.Visible = false;
                this.ShowInTaskbar = true;
            }
        }

        #endregion



I tried using this.close() instead of setting the ShowInTaskbar to false and it still didn't work. It's like it's completely ignoring this entire function.
Was This Post Helpful? 0
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Minimize To System Tray?

Posted 13 April 2012 - 09:02 AM

If you use this.Close(), then the entire application will close and this is not what you want.
You want to hide the form really, so why not use:
if (FormWindowState.Minimized == this.WindowState)
{
    this.notifyIcon1.Visible = true;
    this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
    this.notifyIcon1.Visible = false;


Hide() will also take care to hide the taskbar icon so don't worry about that.
Now, I'm pretty sure that ShowInTaskbar property is runtime read-only, and you shouldn't mess with it.
Was This Post Helpful? 0
  • +
  • -

#6 RandomlyKnighted  Icon User is offline

  • Microsoft Senior Student Partner
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,318
  • Joined: 14-January 10

Re: Minimize To System Tray?

Posted 13 April 2012 - 10:43 AM

View Postsarmanu, on 13 April 2012 - 11:02 AM, said:

If you use this.Close(), then the entire application will close and this is not what you want.
You want to hide the form really, so why not use:
if (FormWindowState.Minimized == this.WindowState)
{
    this.notifyIcon1.Visible = true;
    this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
    this.notifyIcon1.Visible = false;


Hide() will also take care to hide the taskbar icon so don't worry about that.
Now, I'm pretty sure that ShowInTaskbar property is runtime read-only, and you shouldn't mess with it.


Yes, I know the purpose what this.close() does to the application. I believe you might be missing my point. Even though I used this.close() it still DID NOT close the application at all. It's like it's completely overlooking this entire function.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1956
  • View blog
  • Posts: 8,700
  • Joined: 29-May 08

Re: Minimize To System Tray?

Posted 13 April 2012 - 03:55 PM

this.Close closes the instance of the form the this is in. It doesn't necessarily close the application, depends if the application framework is being used and which shutdown mode has been selected.
Was This Post Helpful? 0
  • +
  • -

#8 RandomlyKnighted  Icon User is offline

  • Microsoft Senior Student Partner
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,318
  • Joined: 14-January 10

Re: Minimize To System Tray?

Posted 14 April 2012 - 02:16 AM

View PostAdamSpeight2008, on 13 April 2012 - 05:55 PM, said:

this.Close closes the instance of the form the this is in. It doesn't necessarily close the application, depends if the application framework is being used and which shutdown mode has been selected.

Thanks for the clarification, however, it still does not explain why I can't get the program be removed from the taskbar when minimized. I can share my project if that helps.
Was This Post Helpful? 0
  • +
  • -

#9 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,053
  • Joined: 17-July 10

Re: Minimize To System Tray?

Posted 14 April 2012 - 04:27 AM

Quote

It's like it's completely ignoring this entire function.

If you put a breakpoint inside the function, does your application stop there? If not, take a look at the event handler, maybe points to the wrong method or it is not set at all.
Was This Post Helpful? 0
  • +
  • -

#10 RandomlyKnighted  Icon User is offline

  • Microsoft Senior Student Partner
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,318
  • Joined: 14-January 10

Re: Minimize To System Tray?

Posted 14 April 2012 - 11:21 AM

Nope, it doesn't hit the breakpoint when I minimize the application.

This post has been edited by RandomlyKnighted: 14 April 2012 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

#11 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,053
  • Joined: 17-July 10

Re: Minimize To System Tray?

Posted 14 April 2012 - 11:30 AM

Then add to the contructor
this.Resize += new EventHandler(Form1_Resize);



or add the function from designer->events
Was This Post Helpful? 1
  • +
  • -

#12 RandomlyKnighted  Icon User is offline

  • Microsoft Senior Student Partner
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,318
  • Joined: 14-January 10

Re: Minimize To System Tray?

Posted 14 April 2012 - 11:32 AM

Wow you're fast. I was just about to post that I added that line of code. As soon as I did it worked.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1