CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    [RESOLVED] change icon, based on given path

    hey everyone,

    so i have this program i am workikng on, and i created a custom icon for it. i know that in visual studio i can just change the icon setting on the form to whatever icon i select and its changed. but i want to do it from within the program itself. that way if i feel like changing the icon to something different, i just write the path in the config file and it changes itself.

    ideas?
    here is the config key
    Code:
    <add key="icon" value="E:\Users\Owner\Documents\Visual Studio 2010\Projects\CS_CheckInCheckOut\CS_CheckInCheckOut\Icon.ico"/>
    i tried this:
    Code:
            string iconpath = ConfigurationSettings.AppSettings["icon"];
            public frmMain()
            {
                InitializeComponent();
                this.KeyDown += new KeyEventHandler(frmMain_KeyDown);
                this.PreviewKeyDown += new PreviewKeyDownEventHandler(frmMain_PreviewKeyDown);
                textBox1.Text = "Last Name";
                textBox2.Text = "First Name";
                frmMain.Icon = iconpath;
            }
    but it gave me this:
    Code:
    Error	2	An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Form.Icon.get'	E:\Users\Owner\documents\visual studio 2010\Projects\CS_CheckInCheckOut\CS_CheckInCheckOut\Form1.cs	28	13	CS_CheckInCheckOut
    i am going to search around the internet for answers, but please help me understand this.

    thanks in advance,

    rockking

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: change icon, based on given path

    I'm not sure it can work, but your compilation error is caused by the line frmMain.Icon = iconpath;, because from the syntaxt it seems that frmMain is a class (maybe descendant of a form) and it has not static property Icon. You have to change it to
    Code:
    this.Icon = new Icon(iconpath)
    because you have to set an Icon to the property, not just the path.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: change icon, based on given path

    hey

    thanks for the help boudino, that worked perfectly and is great for using several icons in one project. thank you

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured