问题描述
我有一个窗体(Form1)在其上有一个NotifyIcon的。我还有一个窗体(Form),我想从改变的NotifyIcon的图标。每当我使用此代码,我得到一个额外的图标在系统托盘中显示出来,而不是改变目前的图标:
Form1中(ICO是NotifyIcon的名称):
公共字符串DisplayIcon
{
集合{ico.Icon =新图标(的System.Reflection .Assembly.GetExecutingAssembly()GetManifestResourceStream(Alerts.Icons+值)); }
}
窗体2:
Form1中Form1中=新Form1的();
form1.DisplayIcon =on.ico;
我怀疑是有事情做与创建的Form2 Form1的一个新实例,但我不知道如何访问DisplayIcon没有这样做。谢谢
UDPATE:我在表格上写2自定义属性有点困惑,这将是这样的:
$ b $,$ b
公共窗体Form1
{}
集合{值} p $ p>
解决方案 我假设Form1中在一个点上创建窗体2。在这一点上,你可以通过Form1的到窗体2的引用,以便窗口2可以访问Form1的DisplayIcon财产。
所以,你最终会与像
//某处在Form1
公共无效btnShowFormTwoClick的代码(对象发件人,EventArgs五)
{
Form2的窗体2 =新Form2的();
form2.Form1 =这一点; //如果不Form1的代码中完成的,你不会用这个,但Form1的实例变量
form2.Show();
}
//某处窗口2
公共Form1的Form1的{获取的代码;集;} //要创造一个在Form1参考storred财产。
this.Form1.DisplayIcon =on.ico;
I have a form (Form1) that has a NotifyIcon on it. I have another form (Form2) that I would like to change the NotifyIcon's icon from. Whenever I use this code, I get an extra icon that shows up in the system tray, instead of changing the current icon:
Form1 (ico is the name of the 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";
I suspect is has something to do with creating a new instance of Form1 on Form2, but I'm not sure how to access "DisplayIcon" without doing this. Thanks.
UDPATE: I'm a little confused on writing the custom property on Form 2, would it be something like:
public Form Form1
{
set {value;}
}
解决方案 I assume form1 at one point creates form2. At that point you can pass a reference of form1 to form2 so form2 can access the DisplayIcon property of form1.
So you would end up with something like
//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";
这篇关于更改NotifyIcon的在不同形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!