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?

Developer technologies | Windows Forms
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 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.