我有一个表单(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:我在表单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";