How can i create/add an event of contextMenuStrip1 that is connected with notifyIcon?

我如何创建/添加contextmenustrip1连接NotifyIcon事件?

Tags: c# winforms
标签: c# winforms

问题 (Question)

I added a notifyIcon to my designer in Form1 . Then i hid it :

private void Form1_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }

Then added in the designer contextMenuStrip . In the notifyIcon in the property ContextMenuStrip i selected the contextMenuStrip1 .

Then in the contextMenuStrip in the items edit menu i added a new item called it for the test : Close Application .

Now when i'm running my application and resize down hid the Form i see the icon in the tray icons right click on it i see the menu : Close Application

Now the problem is where/how do i create an event for the Close Application so one single click on it with the mouse left button will do something ? (Close the application)

我对我的设计师在Form1添加NotifyIcon 然后我藏:

private void Form1_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }

然后,在设计师的右键菜单添加。在NotifyIcon属性中的右键菜单我选择。

然后在右键菜单中的项目编辑菜单,我添加了一个新的项目称它为测试:关闭程序。

现在,当我运行我的申请和调整下躲我看到图标在托盘中的图标,右键点击它,我看到菜单形式:关闭程序

现在的问题是在哪里/我如何创建用于关闭应用程序的事件,一个单一的点击和鼠标左键将做什么?(关闭应用程序)

最佳答案 (Best Answer)

Double click the "Close Application" menu item to get a stub handler inserted for you. Now just add in the Close() method for the Form:

    private void closeApplicationToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Close();
    }

EDIT: Here's a picture just to make it clearer:

Double Click to get default handler

双击“关闭程序”菜单项,得到一株处理插入你。现在就加入()方法的形式:

    private void closeApplicationToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Close();
    }

编辑:这里是一个图片,只是为了让它更清晰:

双击会默认处理程序

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/16925996