Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply changes to mainforms Form.Icon at runtime

Tags:

c#

.net

winforms

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?

like image 441
Hinek Avatar asked Oct 10 '10 07:10

Hinek


People also ask

How do I change the icon on a Windows Form?

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.

How do I change the position of the form during design time?

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.

How do I show a form in Visual Studio?

In Visual Studio, find the Project Explorer pane. Right-click on the project and choose Add > Form (Windows Forms).


1 Answers

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.

like image 84
Hans Passant Avatar answered Sep 19 '22 14:09

Hans Passant