Set same icon for all my Forms

前端 未结 8 1507
一向
一向 2020-12-08 19:31

Is there any way to set the same icon to all my forms without having to change one by one? Something like when you setup GlobalAssemblyInfo for all your project

相关标签:
8条回答
  • 2020-12-08 20:13

    It is an old question, but this method worked for me to get the same icon in all forms without having to add it in every form's properties.

    First I added a new icon in the 'Resources' node under 'Properties' (add resource => New Icon).

    By default some icon is created and stored in your resources location (look for the 'Filename' property on the icon).

    Assuming you have a .ico file ready, you can copy it to that folder.

    Delete the one Visual Studio created in that folder and rename your own to the exact same name.

    Visual Studio will prompt you if you want to reload the resource since it was modified. (click 'Yes')

    That way you have your Logo available under resources.

    Now I have made a general method to change the form title to my default and set the form icon and call that in every form right after InitializeComponent();

    How that looks in the form cs (m is the class that holds the general method):

    m.set_form_defaults(this, "Title here");
    

    And this is the method itself:

        public void set_form_defaults(Form frm,string frmTitle)
        {
    
            frm.Icon = ((System.Drawing.Icon)(Properties.Resources.Logo_V2));
            frm.Text = frmTitle + " " + show_current_server();
    
        }
    

    Of course you don't need to add the form title here, but that is just because I want to show some stored properties to the user (current server)

    0 讨论(0)
  • 2020-12-08 20:15

    I wasn't sure if I wanted to use inheritance here, so I used an extension method:

    public static class MyExtensionMethods
    {
        public static void SetAppIcon(this Form form)
        {
            form.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
        }
    }
    

    Then in any form's constructor:

    this.SetAppIcon();
    

    Note: this will cause a crash if you try to run the application from a network location

    0 讨论(0)
提交回复
热议问题