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");
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?
You can change the icon in Visual Studio by opening the properties window for the form. You can then upload a new icon image to the icon field.
The second way you can resize the form while the designer is open, is through the properties pane. Select the form, then find the Properties pane in Visual Studio. Scroll down to size and expand it. You can set the Width and Height manually.
In Visual Studio, find the Project Explorer pane. Right-click on the project and choose Add > Form (Windows Forms).
It is unclear to me why you are doing this the hard way. Just add the icon to your resources. Project + Properties, Resource tab, arrow on Add Resource button, Add Existing File. Then you'd use it like this at runtime:
private void button1_Click(object sender, EventArgs e) {
this.Icon = Properties.Resources.Mumble;
}
Where Mumble is the name of the icon.
If you are 100% sure that GetObject() doesn't return null then try setting the Icon property in the designer. If it still doesn't show up then there's something wrong with the icon format. Make sure that it doesn't have too many colors, 256 works on XP.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With