c# - 在单独的表单上更改 NotifyIcon

标签 c# winforms

我有一个表单 (Form1),上面有一个 NotifyIcon。我有另一种形式 (Form2),我想从中更改 NotifyIcon 的图标。每当我使用此代码时,系统托盘中都会显示一个额外的图标,而不是更改当前图标:

Form1(ico 是 NotifyIcon 的名称):

public string DisplayIcon
{
    set { ico.Icon = new Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Alerts.Icons." + value)); }
}

表格 2:

Form1 form1 = new Form1();
form1.DisplayIcon = "on.ico";

我怀疑这与在 Form2 上创建 Form1 的新实例有关,但我不确定如何在不执行此操作的情况下访问“DisplayIcon”。谢谢。

UDPATE:我对在 Form 2 上编写自定义属性有点困惑,它会是这样的吗:

public Form Form1
{
    set {value;}
}

最佳答案

我假设 form1 在某一时刻创建了 form2。此时,您可以将 form1 的引用传递给 form2,以便 form2 可以访问 form1 的 DisplayIcon 属性。

所以你最终会得到类似的东西

//Somewhere in the code of form1
public void btnShowFormTwoClick(object sender, EventArgs e) 
{
    Form2 form2 = new Form2();
    form2.Form1 = this; //if this isn't done within form1 code you wouldn't use this but the form1 instance variable
    form2.Show();
}

//somewhere in the code of form2
public Form1 Form1 { get;set;} //To create the property where the form1 reference is storred.
this.Form1.DisplayIcon = "on.ico";

关于c# - 在单独的表单上更改 NotifyIcon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1351561/

相关文章:

c# - 将变量名设置为类型

c# - HTTP 处理程序的 Post 方法

c# - 在 datagridview 中下拉 Combobox

c# - 发送大量短信时,批量短信应用程序挂起

c# - 为什么我不能使用反射更改 Type.Delimiter?

c# - 将字符串属性作为键添加到字典中会克隆字符串吗?有适当的解决方法吗?

c# - 访问函数内泛型类型的属性

c# - 如何使用 assembly.CreateInstance 调用非默认构造函数

c# - 标签未显示在WinForm应用程序中

c# - Form.ActiveForm 有时有效