-1

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 .

2 Answers2

2

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 {
   ...
}
Hazrelle
  • 758
  • 5
  • 9
A.Bahrami
  • 134
  • 7
1

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 to Content.
  • Set file's Copy to Output Directory to Copy Always or Copy if newer.
  • Load icon using new Icon(iconFilename).

For embedded resources:

  • Add icon file to project (Add Existing Item)
  • Set file's Build Action to Embedded 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.

Corey
  • 15,524
  • 2
  • 35
  • 68