如何在多个表单之间使用1 notifyIcon? [英] How can I use 1 notifyIcon between multiple forms?

查看:149
本文介绍了如何在多个表单之间使用1 notifyIcon?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个notifyIcon添加到我的项目的主要形式。我有其他形式的项目,我想要能够使用notifyIcon虽然这是证明很困难。在多个表单之间使用1 notifyIcon的最好方法是什么?我读了一个线程,不添加到一个窗体,但实例化它在自己的类,这对我没有意义。

I have a notifyIcon added to my main form of a project. I have other forms in the project that I want to be able to use the notifyIcon though which is proving difficult. What would be the best way to use 1 notifyIcon between multiple forms? I read a thread about not adding it to a form but instantiating it in its own class which made no sense to me. Thoughts?

推荐答案

只需在主窗体上公开一个属性,就可以返回对NotifyIcon的引用。你甚至可以使它静态,因为只有一个:

Just expose a property on your main form to return a reference to the NotifyIcon. You can even make it static since there is only ever one:

public partial class MainForm : Form {
    public MainForm() {
        InitializeComponent();
        notifier = this.notifyIcon1;
        this.FormClosed += delegate { notifier = null; };
    }

    public static NotifyIcon Notifier { get { return notifier; } }

    private static NotifyIcon notifier;
}

其他类中的代码现在可以使用MainForm.Notifier。

Code in other classes can now simply use MainForm.Notifier.

这篇关于如何在多个表单之间使用1 notifyIcon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆