C # program Shutdown tray icon does not automatically disappear

Source: Internet
Author: User
Debug the WinForm program when using the tray icon to encounter the shutdown of the program but the tray icon did not disappear, but the mouse over the icon disappeared, so that the software turned off more times will appear in the tray stacked a large number of icons, beginning to think is the operating system refresh problem, Later use the manual release icon resource problem to resolve. Specifically, use the following methods:
Dispose ()

Releases all resources used by the Component. (Inherited from Component. )

Called directly in the closing event of the form This.notifyIcon1.disponse ()You can also refer to the official example
Using System;
Using System.Drawing;

Using System.Windows.Forms;
    public class Form1:System.Windows.Forms.Form {private System.Windows.Forms.NotifyIcon notifyIcon1;
    Private System.Windows.Forms.ContextMenu contextMenu1;
    Private System.Windows.Forms.MenuItem menuItem1;

    Private System.ComponentModel.IContainer components;
    [STAThread] static void Main () {Application.Run (New Form1 ());
        Public Form1 () {this.components = new System.ComponentModel.Container ();
        this.contextmenu1 = new System.Windows.Forms.ContextMenu ();

        This.menuitem1 = new System.Windows.Forms.MenuItem (); Initialize contextMenu1 This.contextMenu1.MenuItems.AddRange (New System.Windows.Forms.Menu

        Item[] {this.menuitem1});
        Initialize menuItem1 this.menuItem1.Index = 0;
        This.menuItem1.Text = "E&xit";

 This.menuItem1.Click + = new System.EventHandler (This.menuitem1_click);       Set up how the form should is displayed. This.
        ClientSize = new System.Drawing.Size (292, 266); This.

        Text = "Notify Icon Example";
        Create the NotifyIcon.

        This.notifyicon1 = new System.Windows.Forms.NotifyIcon (this.components);
        The Icon property sets the icon, that would appear//in the Systray for this application.

        Notifyicon1.icon = new Icon ("Appicon.ico");
        The ContextMenu property sets the "menu that" and "appear" the Systray icon is right clicked.

        Notifyicon1.contextmenu = this.contextmenu1;  The Text property sets the text is displayed,//In a ToolTip, when the mouse hovers over the Systray
        Icon.
        Notifyicon1.text = "Form1 (NotifyIcon example)";

        Notifyicon1.visible = true;
        Handle the DoubleClick event to activate the form.

    Notifyicon1.doubleclick + = new System.EventHandler (This.notifyicon1_doubleclick);

    }protected override void Dispose (bool disposing) {//clean up any components being used. if (disposing) if (components!= null) components.            

        Dispose (); Base.
    Dispose (disposing); } private void Notifyicon1_doubleclick (object Sender, EventArgs e) {//Show the ' form ' when the user Doub

        Le clicks on the Notify icon.
        Set the WindowState to normal if the form is minimized. if (this. WindowState = = formwindowstate.minimized) this.

        WindowState = Formwindowstate.normal;
        Activate the form. This.
    Activate (); 
        } private void Menuitem1_click (object Sender, EventArgs e) {//Close the form, which closes the application. This.
    Close ();
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.