开发者

Where to add a notify icon in a multiple form based application c#

I am making a multiple form based application and have encountered a problem with a notify icon. I have added the notify icon to the first (main) form the user sees but I quickly encountered a problem when the user navigates away from this form and then back to it where a new notify icon is added to the task bar. The way around this i have found is to call ShowDialog() on forms loaded from the main menu and then just Ilosing these forms when the user navigates back. Is this really the only way to do this? I have a very deep application with multiple forms deep do I always have to keep the first form in memory and on screen to maintain the notify icon and ensure new notify ic开发者_开发问答ons are not added to the taskbar?

Thanks


As with other WinForms components, this one doesn't need to be placed in a form to work correctly. You can instantiate it, set properties and bind events on another class that isn't a Form. For example, this is a class that could manage a NI control:

namespace WinformsTesting {

    using System;
    using System.Windows.Forms;
    using System.Drawing;

    public class NotifyIconManager {

        private NotifyIcon _ni;

        public void Init() {

            _ni = new NotifyIcon();
            _ni.MouseDoubleClick += new MouseEventHandler(_ni_MouseDoubleClick);
            _ni.Text = "This is my notify icon";

            Icon icon = new Icon(@"C:\temp\myicon.ico");
            _ni.Icon = icon;
            _ni.Visible = true;

        }

        void _ni_MouseDoubleClick(object sender, MouseEventArgs e) {
            MessageBox.Show("Hello");
        }
    }
}

The only problem here is interaction with the rest of your application, but depending on how you're using the NI control this might be a good starting point.


I would keep a dictionary-type collection at a level higher than your form. Keys would be formId/reference and bool for whether the notify icon was showing or not. Before I would show the notifyIcon, I'd check to see if the Form already has one showing. I'd also register events for the notifyIcon so that when it closes, if changes the dictionary value.

Just an idea to get you started.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜