我有一个表单 (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/