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

Spamzilla 41 Reputation points
2022-10-21T00:55:11.923+00:00

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,889 questions
C#
C#
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.
10,904 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 85,116 Reputation points
    2022-10-21T07:05:06.207+00:00

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

     this.Icon = Properties.Resources.Test2;  
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.