将更改应用于mainforms Form.Icon在运行时应用于、mainforms、Icon、Form

2023-09-04 12:08:47 作者:你快滚,趁我没说舍不得

我有一个System.Windows.Forms.Form中,并想改变Form.Icon在运行时显示状态。我已经成功地从项目加载图标ressources:

I have a System.Windows.Forms.Form and want to change the Form.Icon at runtime to display a status. I've managed to load the icon from the projects ressources:

Type type = this.GetType();
System.Resources.ResourceManager resources =
    new System.Resources.ResourceManager(type.Namespace + ".Properties.Resources", this.GetType().Assembly);
this.Icon = (System.Drawing.Icon)resources.GetObject(
    type.Namespace + ".Icons." + statusText + ".ico");

但显示的图标保持不变(设计时间图标)所有的时间。我必须调用一个方法告诉给应用更改表格?有什么问题我的使用Form.Icon的?

But the displayed icon stays the same (design time icon) all the time. Do I have to call a method to tell the Form to apply changes? Something wrong about my usage of Form.Icon?

推荐答案

好了,西瓦和汉斯在哪里右:GetObject的返回null,因为的ressource的名字是不正确的。除了以下的改变它的工作原理:

Ok, Siva and Hans where right: GetObject returned null, because the name of the ressource wasn't right. With the following change it works:

Type type = this.GetType(); 
System.Resources.ResourceManager resources = 
new System.Resources.ResourceManager(type.Namespace + ".Properties.Resources", this.GetType().Assembly);

// here it comes, call GetObject just with the resource name, no namespace and no extension
this.Icon = (System.Drawing.Icon)resources.GetObject(statusText); 

感谢您的帮助。

Thanks for all your help.