我有一个带有 NotifyIcon 的表单(Form1)。我有另一个表单(Form2),我想更改 NotifyIcon 的图标。每当我使用此代码时,我都会在系统托盘中显示一个额外的图标,而不是更改当前图标:
Form1(ico 是 NotifyIcon 的名称):
public string DisplayIcon
{
set { ico.Icon = new Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Alerts.Icons." + value)); }
}
Form2:
Form1 form1 = new Form1();
form1.DisplayIcon = "on.ico";
我怀疑这与在 Form2 上创建 Form1 的新实例有关,但我不确定如何在不这样做的情况下访问“DisplayIcon”。谢谢。
更新:我对在 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";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)