By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,289 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

C# minmize/close to system tray

P: 46
I am working on an app that I need to minimize/close to the system tray.

More or less I have the minimize to the system tray working, save caveat. When it is minimized the application still appears in the task bar. Is there a way to remove the application from the task bar?

Here is my current code for minimize.
Expand|Select|Wrap|Line Numbers
  1.  
  2. private void Form1_Resize(object sender, System.EventArgs e)
  3. {            
  4.      if (FormWindowState.Minimized == WindowState) 
  5.      Hide();         
  6. }
  7.  
Now when it comes to closing to the system tray, everything I have tried just closes the application in stead of closing it to the system tray.

Here is my current code for closing.
Expand|Select|Wrap|Line Numbers
  1.  
  2. private void Form1_FormClosing(object sender, FormClosingEventArgs e)         {             
  3.      if (e.CloseReason == CloseReason.UserClosing)
  4.           {                 
  5.                e.Cancel = true;
  6.                Hide();             
  7.           }         
  8. }
  9.  
Nov 3 '08 #1
Share this Question
Share on Google+
22 Replies


joedeene
100+
P: 583
You are using the NotifyIcon, aren't you? Can you see your icon in the system tray after being minimized? Or, when your form loads do you see your icon at all in the system tray? Maybe you have not given it an icon reference or set it's .Visible Property to true.

joedeene
Nov 3 '08 #2

P: 46
I do see the notifyicon. The issue with minimize is it shows up in the task bar as well as the system tray when minimized. I do not want it to show in the task bar if it is minimized. I only want it to show in the system tray.
Nov 3 '08 #3

Curtis Rutland
Expert 2.5K+
P: 3,256
Forms have a "ShowInTaskbar" attribute.

You can do something like this:
Expand|Select|Wrap|Line Numbers
  1. if (FormWindowState.Minimized == WindowState) 
  2. {
  3.      Hide();
  4.      this.ShowInTaskbar = false;
  5. }
  6.  
Nov 3 '08 #4

P: 46
I have tried this.ShowInTaskbar = false; and still shows in the task bar.
Nov 3 '08 #5

P: 46
Ok got the minimize figured out, if I set the default property on the form to ShowInTaskBar to false, it does exactly what I want it to.

But I am still a bit stuck on the whole close to the system tray issue.
Nov 3 '08 #6

joedeene
100+
P: 583
Ok got the minimize figured out, if I set the default property on the form to ShowInTaskBar to false, it does exactly what I want it to.

But I am still a bit stuck on the whole close to the system tray issue.
Well how are you closing the form, try taking the Close Reason If Statement out and then see, if it works experiment which ones work...

joedeene
Nov 3 '08 #7

Curtis Rutland
Expert 2.5K+
P: 3,256
Expand|Select|Wrap|Line Numbers
  1. namespace TestShowInTaskbar
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         private void Form1_Resize(object sender, EventArgs e)
  11.         {
  12.             if (this.WindowState == FormWindowState.Minimized)
  13.             {
  14.                 this.ShowInTaskbar = false;
  15.             }
  16.             else
  17.             {
  18.                 this.ShowInTaskbar = true;
  19.             }
  20.         }
  21.  
  22.         private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  23.         {
  24.             this.WindowState = FormWindowState.Normal;
  25.         }
  26.  
  27.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  28.         {
  29.             if (e.CloseReason != CloseReason.TaskManagerClosing && 
  30.                 e.CloseReason != CloseReason.WindowsShutDown)
  31.                 e.Cancel = true;
  32.             this.WindowState = FormWindowState.Minimized;
  33.         }
  34.     }
  35. }
  36.  
This works just fine for me. Of course, you would have to provide some mechanism for your user to exit, but that shouldn't be too hard.
Nov 3 '08 #8

P: 46
Well there are several ways I want to blocking closing.

Close button in the title window, system menu close, and alt+ f4.

Every example that I have seen uses the code that I posted in my original post.
Nov 3 '08 #9

Curtis Rutland
Expert 2.5K+
P: 3,256
The way I posted blocks all but Task Manager closings and Windows shutdown.

Hide() is probably what is killing you.

Try it my way and see what happens.
Nov 3 '08 #10

P: 46
Copied your code exactly, and still does the same thing. Here is a full breakdown of my code.

Expand|Select|Wrap|Line Numbers
  1.         private void Form1_Resize(object sender, EventArgs e)
  2.         {
  3.             if (this.WindowState == FormWindowState.Minimized)
  4.              {
  5.                 this.ShowInTaskbar = false;
  6.              }
  7.              else
  8.                 {
  9.                  this.ShowInTaskbar = true;
  10.                 }
  11.          }
  12.  
  13.         private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  14.         {
  15.             Show();
  16.             WindowState = FormWindowState.Normal;
  17.  
  18.         }
  19.  
  20.  
  21.          private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  22.          {
  23.              if (e.CloseReason != CloseReason.TaskManagerClosing && 
  24.                  e.CloseReason != CloseReason.WindowsShutDown)
  25.                  e.Cancel = true;
  26.             this.WindowState = FormWindowState.Minimized;
  27.         } 
  28.  
Nov 3 '08 #11

Plater
Expert 5K+
P: 7,872
Why do you change the ShowIntaskBar property, you really want to be hide/unhide'ing the form.
Nov 3 '08 #12

Curtis Rutland
Expert 2.5K+
P: 3,256
Just curious, did you wire up all the events, or just paste the functions in?

@Plater, that's the way it's always worked for me.
Nov 3 '08 #13

P: 46
I want both to occur. I want to hide the application and not show it in the task bar.
Nov 3 '08 #14

Curtis Rutland
Expert 2.5K+
P: 3,256
Well, try something like this:
Expand|Select|Wrap|Line Numbers
  1. namespace TestShowInTaskbar
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         private void Form1_Resize(object sender, EventArgs e)
  11.         {
  12.             if (this.WindowState == FormWindowState.Minimized)
  13.             {
  14.                 this.Hide();
  15.             }
  16.         }
  17.  
  18.         private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  19.         {
  20.             this.Show();
  21.             this.WindowState = FormWindowState.Normal;
  22.         }
  23.  
  24.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  25.         {
  26.             if (e.CloseReason != CloseReason.TaskManagerClosing && 
  27.                 e.CloseReason != CloseReason.WindowsShutDown)
  28.                 e.Cancel = true;
  29.             this.Hide();
  30.         }
  31.     }
  32. }
  33.  
Both should work perfectly, or there's something else happening in your code. I've tried and tested both, and they both work just fine for me.
Nov 3 '08 #15

P: 46
I am not exactly where the issue would be in my app. I have trimmed everything not relating to the form.

So, I will just show you everything. This is all the functioning code that I am using at this time.

Program.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using System.Threading;
  6.  
  7. namespace qbLinx
  8. {
  9.     static class qbLinx
  10.     {
  11.         /// <summary>
  12.         /// The main entry point for the application.
  13.         /// </summary>
  14.         [STAThread]
  15.         static void Main()
  16.         {
  17.              bool ok;
  18.              Mutex m = new Mutex(true, "iqbLinx", out ok);
  19.              if (!ok)
  20.              {
  21.                  MessageBox.Show("Another instance is already running.");
  22.                  return;
  23.              }
  24.  
  25.             Application.EnableVisualStyles();
  26.             Application.SetCompatibleTextRenderingDefault(false);
  27.             Application.Run(new Form1());
  28.             GC.KeepAlive(m);
  29.  
  30.  
  31.             //sqlConnect sqlCnn = new sqlConnect();
  32.  
  33.             //sqlCnn.mysqlAddQuery();
  34.         }
  35.     }
  36. }
  37.  

Form1.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10.  
  11. namespace qbLinx
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.             RegistrySettings regQry = new RegistrySettings();
  19.             txtbhName.Text = regQry.GetRegEntry("hostname");
  20.             txtbuName.Text = regQry.GetRegEntry("uName");
  21.             txtbuPass.Text = regQry.GetRegEntry("uPass");
  22.             txtbdSource.Text = regQry.GetRegEntry("hostDest");
  23.             txtbBrowse.Text = regQry.GetRegEntry("compFile");
  24.  
  25.  
  26.         }
  27.  
  28.         private void btnBrowse_Click(object sender, EventArgs e)
  29.         {
  30.             OpenFileDialog dlgOpen = new OpenFileDialog();
  31.             if (dlgOpen.ShowDialog() == DialogResult.OK)
  32.             {
  33.                 txtbBrowse.Text = dlgOpen.FileName;
  34.             }
  35.         }
  36.  
  37.         private void btnSave_Click(object sender, EventArgs e)
  38.         {
  39.             RegistrySettings regSet = new RegistrySettings();
  40.             regSet.SetRegEntry("hostname", txtbhName.Text);
  41.             regSet.SetRegEntry("uName", txtbuName.Text);
  42.             regSet.SetRegEntry("uPass", txtbuPass.Text);
  43.             regSet.SetRegEntry("hostDest", txtbdSource.Text);
  44.             regSet.SetRegEntry("compFile", txtbBrowse.Text);
  45.         }
  46.  
  47.         private void btnCancel_Click(object sender, EventArgs e)
  48.         {
  49.             Hide();
  50.         }
  51.  
  52.         private void Form1_Resize(object sender, EventArgs e)
  53.         {
  54.             if (this.WindowState == FormWindowState.Minimized)
  55.              {
  56.                 this.ShowInTaskbar = false;
  57.              }
  58.              else
  59.                 {
  60.                  this.ShowInTaskbar = true;
  61.                 }
  62.          }
  63.  
  64.         private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  65.         {
  66.             Show();
  67.             WindowState = FormWindowState.Normal;
  68.  
  69.         }
  70.  
  71.  
  72.          private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  73.          {
  74.              if (e.CloseReason != CloseReason.TaskManagerClosing && 
  75.                  e.CloseReason != CloseReason.WindowsShutDown)
  76.                  e.Cancel = true;
  77.              this.Hide();
  78.         } 
  79.  
  80.  
  81.  
  82.  
  83.     }
  84. }
  85.  
Nov 3 '08 #16

Curtis Rutland
Expert 2.5K+
P: 3,256
Well, part of the problem is you're mixing and matching between two examples. In the Resize event you are using ShowInTaskbar, and everywhere else you are using Hide/Show. Go with one mechanism or the other. Try to use my latest example as your resize event:
Expand|Select|Wrap|Line Numbers
  1. private void Form1_Resize(object sender, EventArgs e)
  2. {
  3.     if (this.WindowState == FormWindowState.Minimized)
  4.     {
  5.         this.Hide();
  6.     }
  7. }
  8.  
Nov 3 '08 #17

P: 46
The minimize now works, but the close does not work. When I click the close button in the title window, system menu close, or alt+f4 it still closes the application.
Nov 3 '08 #18

Curtis Rutland
Expert 2.5K+
P: 3,256
Did you wire up that event or just copy/paste? Make sure that the method is set in the Events tab in the properties box. See the attatchment:
Attached Images
File Type: jpg step7.jpg (33.1 KB, 392 views)
Nov 3 '08 #19

P: 46
Yes I have the event setup, with in the events panel.
Nov 3 '08 #20

Curtis Rutland
Expert 2.5K+
P: 3,256
Well, I don't know what to tell you. The code works just fine on my computer. Make sure again that you are using the code only from my latest sample, and try to put breakpoints in your program and trace through it.

Sorry I can't help anymore.
Nov 3 '08 #21

tlhintoq
Expert 2.5K+
P: 3,525
If it is any help, here is the handler that I use for a couple programs that do the same thing: Don't display in task bar, close to just the notifyicon etc.

Expand|Select|Wrap|Line Numbers
  1.        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  2.         {
  3.             if (e.CloseReason == CloseReason.UserClosing || e.CloseReason == CloseReason.TaskManagerClosing)
  4.             {
  5.                 MainLog(string.Format("Hiding. ({0})",e.CloseReason));
  6.                 e.Cancel = true; // true that we are canceling the close event
  7.                 this.Hide(); // same as saying this.visible = false;
  8.                 this.ShowInTaskbar = false;
  9.                 notifyIcon1.Visible = true;
  10.             }
  11.             else MainLog(string.Format("Closing. ({0})", e.CloseReason));
  12.             notifyIcon1.Visible = false;
  13.         }
  14.  
Nov 4 '08 #22

Plater
Expert 5K+
P: 7,872
If a form is hidden, or visible=false, it's taskbar entry is hidden also (as well as its position in alt+tab and in the "applications" tab of the taskmanager)
That being said, turning off the taskbar entry, ShowInTaskBar=false, is a nifty little trick, but when you minimize, it just acts as an mdichild of the desktop and leaves its little windowbar in the bottom left corner, just above the taskbar.
Nov 4 '08 #23

Post your reply

Sign in to post your reply or Sign up for a free account.