How to programmatically set an icon that is in resources in C#

Spamzilla 41 Reputation points

I'm trying to programmatically set my program's icons in code using an icon in the resources in C#. This code here doesn't work:

System.ComponentModel.ComponentResourceManager resources2 = new System.ComponentModel.ComponentResourceManager(typeof(TLS_FormMain));
this.Icon = (Icon)(resources2.GetObject("TLS_Icon_Green.ico"));

Neither does this code:

Icon programIcon = new Icon(typeof(TLS_FormMain), "TLS_Icon_Green.ico");
this.Icon = programIcon;

Any solutions?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,730 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,448 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 79,821 Reputation points

    With an icon resource named Test2, this simple line works for me :

     this.Icon = Properties.Resources.Test2;  

0 additional answers

Sort by: Most helpful