I am working on a project where my requirement is that i have to use a same icon on all my windows forms . I have atleast 50 windows forms and many more had to be added . So is there a way to set a default icon for all windows forms rathee than doing manually on each form page .
-
That link It provides the code to set icon from executable file but I haven't set any icon for my executable file yet – Shaifali Goel Oct 19 '21 at 08:08
-
@StephanBauer It's an old question with old answers, some of which are no longer useful. – Corey Oct 19 '21 at 08:19
-
Check this: [link](https://stackoverflow.com/questions/189031/set-same-icon-for-all-my-forms). add In your Program Main method. – Seyed Mohammad Ayyoubzadeh Oct 19 '21 at 08:28
-
@ShaifaliGoel Well it doesn't really matter where your icon comes from. The accepted answer described multiple possibilities how to set the same icon for all forms. – Stephan Bauer Oct 19 '21 at 08:51
-
1@Corey I'd rephrase this to "It's an old question with old answers, some of which are still useful." ;-) – Stephan Bauer Oct 19 '21 at 08:55
-
@StephanBauer You're a better man than I am then :P – Corey Oct 19 '21 at 09:05
-
Aww, they closed it while I was still typing. Darn it. – Corey Oct 19 '21 at 09:08
2 Answers
you have multiple solution for this problem best idea would be to inherit from a common base-Form that sets the Icon in the constructor. from : Form or : System.Windows.Forms.Form to : MyCustomForm
and then just change MyCustomForm icon with write this line in MyCustomForm Cunstrunctor
this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
then you just change project icon from project properties > Application > Icon and Manifest > browse for a *.ico file and add it there. this approach can change other property in all from for example font,size,anchor,...
If you have many forms like:
public class MyAppForm1 : Form {
...
}
Then instead of deriving from Form
, then you create an intermediate MyIconForm
:
public class MyIconForm : Form {
public MyIconForm() : base() {
this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
}
}
Then you just have to update all your forms to:
public class MyAppForm1 : MyIconForm {
...
}
-
@ A. Bahrami, Can u pls share a link . I am new to windows forms . I still don't get how to use your code – Shaifali Goel Oct 19 '21 at 08:45
-
The short answer is no, there is no central location where you can set the icon to be used by all forms in your project. You have to do it yourself. There are a bunch of methods you can use, including using reflection to set the backing field for Form.DefaultIcon
during program initialization.
First step: get an icon.
There are a couple of options here. You can load an icon from a file, a resource or an embedded resource (yes, two different types of resource). Or the application icon, but you said you don't have one.
For standard resources:
- Open project properties
- Click
Add Resource
,Add Existing File
and browse to your icon. - Icon is added to
Properties.Resources
with a property named after the file.
For content files (distributed with the application):
- Add icon file to project (Add Existing Item)
- Set file's
Build Action
property toContent
. - Set file's
Copy to Output Directory
toCopy Always
orCopy if newer
. - Load icon using
new Icon(iconFilename)
.
For embedded resources:
- Add icon file to project (Add Existing Item)
- Set file's
Build Action
toEmbedded Resource
. - Use
Assembly.CurrentAssembly.GetManifestResourceStream
to open the resource as a stream.
Of the three I'd choose standard resource for most things.
Making it work 1: Form Constructor
Add the appropriate loading code to every form's constructor:
public partial class Form1 : Form
{
public Form1()
{
// Using standard resource method
Icon = Properties.Resources.FormIcon;
InitializeComponent();
}
}
Making it work 2: Base Class
This is a fairly simple option, assuming you have control over the source for all of your forms and are OK with going through all of them to change their base class. The base class simply sets the form's icon during construction:
public class DefaultIconForm : Form
{
// Using content file method
private static readonly Icon _defaultIcon = new Icon("FormIcon.ico");
public DefaultIconForm()
{
Icon = _defaultIcon;
}
}
public partial class Form1 : DefaultIconForm
{
public Form1()
{
InitializeComponent();
}
}
You'll need to change all of your forms and remember to inherit from DefaultIconForm
for all future forms.
Making it work 3: modify Form.DefaultIcon
This one is a slightly nasty trick that relies on reflection and could fail on you at some point, but it means not having to change any other code in your application.
Open your Program.cs
file and add this method to the Program
class:
private static void SetDefaultFormIcon()
{
var field = typeof(Form).GetField("defaultIcon", BindingFlags.Static | BindingFlags.NonPublic);
// And for completeness, this is the Embedded Resource method
using (var stream = typeof(Program).Assembly.GetManifestResourceStream($"{Application.ProductName}.FormIcon.ico"))
{
var ico = new Icon(stream);
field?.SetValue(null, ico);
}
}
Now call SetDefaultFormIcon()
from main()
to initialize.
As written it works on both .NET Framework and .NET 5 WinForms applications. There's no guarantee that the defaultIcon
hidden static field won't change in future, so be prepared for it to break at some point.

- 15,524
- 2
- 35
- 68