473,498 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# minmize/close to system tray

46 New Member
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
22 17602
joedeene
583 Contributor
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
rottmanj
46 New Member
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
3,256 Recognized Expert Specialist
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
rottmanj
46 New Member
I have tried this.ShowInTaskbar = false; and still shows in the task bar.
Nov 3 '08 #5
rottmanj
46 New Member
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
583 Contributor
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
3,256 Recognized Expert Specialist
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
rottmanj
46 New Member
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
3,256 Recognized Expert Specialist
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
rottmanj
46 New Member
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
7,872 Recognized Expert Expert
Why do you change the ShowIntaskBar property, you really want to be hide/unhide'ing the form.
Nov 3 '08 #12
Curtis Rutland
3,256 Recognized Expert Specialist
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
rottmanj
46 New Member
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
3,256 Recognized Expert Specialist
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
rottmanj
46 New Member
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
3,256 Recognized Expert Specialist
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
rottmanj
46 New Member
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
3,256 Recognized Expert Specialist
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, 426 views)
Nov 3 '08 #19
rottmanj
46 New Member
Yes I have the event setup, with in the events panel.
Nov 3 '08 #20
Curtis Rutland
3,256 Recognized Expert Specialist
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
3,525 Recognized Expert Specialist
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
7,872 Recognized Expert Expert
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

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

Similar topics

1
2691
by: Manoj Nair | last post by:
Hi, The problem : Have a system tray application.This has a menu item 'Exit'. On click of this a differnt application with a UI which runs in the background should close. The other application...
3
2715
by: Shawn | last post by:
Hi. I've created an application that can run in the system tray. The way it works now I have to click a button to "send" the application to the system tray, but I want the application to run in...
1
1225
by: rdi | last post by:
I was able to get my utility to work using parts of the system tray example. If you right click on "Show Form" my form pops up--but only if that form is not already open. What I'd like to...
0
1083
by: Sidney | last post by:
Dear, I am writting a system which opened at the system tray. But every before I shut down my computer, I have to close the system first. However, it should be automatically close when I shutting...
5
9803
by: bipi | last post by:
Dear everyone, In my application, I kill process by: private bool KillProcess(String processName) { bool result = false; Process process = Process.GetProcessesByName(processName); for (int...
2
5361
by: flaper87 | last post by:
Hi everybody!!!! I am developing an aplication, and i want it to stay on the system tray, i found out how to put the icon, but i can minimize it there, What do i have to do?, I'm using python(of...
3
7907
by: Patrick Dugan | last post by:
I am using VS2005 (vb) and I have a program that starts when Windows boots up. Occasionally the icon that should appear in the system tray does not show up. The program is still running in memory...
3
1708
by: holaboxdotcom | last post by:
Hey I'm pretty new to C# (started few hours ago) but i learn extremly fast, i have previous knowledge of php, javascript and flash. I have made my first simple application the way i want it,...
5
3624
by: Lawyno | last post by:
Hi there, we are creating some automated backup solution for some user application (let's say "UserApp" ;)). Now, the backup service is working perfectly. But there's a problem: the backups...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.