Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When you click on the exit button, i want the application to minimize to the system tray, not to the task bar, kinda like the way your antivirus does.
Posted

To send your application form into the system tray Drag and drop a NotifyIcon control to your form.

C#
private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
{
   e.Cancel = true;
   this.WindowState = FormWindowState.Minimized;
   this.ShowInTaskbar = false;
   notifyIcon1.ShowBalloonTip(2, "Marketplace Retriever", "Marketplace Retriever is still running. If you want to use the tool please double click the icon to resize.", ToolTipIcon.Info);
   notifyIcon1.Visible = true;            
}


Goodluck
 
Share this answer
 
v4
Comments
Raidzen10 3-Nov-11 17:41pm    
thanks, I want to close the form from the context menu of the notifyicon, when I click Exit, Nothing happens, is this because we said "e.cancel = true"?
Orcun Iyigun 3-Nov-11 17:56pm    
Please check my asnwer again. Because of the Hide(); it wasnt working properly. Dont forget to use an icon for the notify icon so you can see it in the tray.
Orcun Iyigun 3-Nov-11 18:37pm    
Check this article : http://www.codeproject.com/KB/cs/trayiconmenu01.aspx You want to keep the application open on close button, you either add a button to your form which basically closes the application or carry your code to your minimize button and close the application in your application [x] button.
Raidzen10 3-Nov-11 18:31pm    
But without hide it you can still see the UI, I want the exact opposite..
C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        e.Cancel = true;
        this.WindowState = FormWindowState.Minimized;
        this.ShowInTaskbar = false;
        this.Hide();
        notifyIcon1.Visible = true;
    }
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
    Application.Exit();
}


Same as the other posters solution but with a couple of modifications.
First you need this.Hide() (or this.visible = false;) to hide the form from the Alt + Tab menu.
Second make sure you call Application.Exit() instead of this.Close() from your context menus exit menu item.
Third the if statement says to only do this if the user closed the app by click the red X or pressing Alt + F4 for all other close reasons i.e. windows is shutting down, closed through the task manager, etc. the application will exit normally.
 
Share this answer
 
Comments
Raidzen10 4-Nov-11 8:21am    
Yeah..that doesn't work, But this did "Environment.Exit(0);". Thanks anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900