0

I have a WinForms app that has a little setup program that writes to Properties.Settings. The user needs to choose his notifyIcon icon from his hard drive. I can't just change it with

notifyIcon1.Icon = Properties.Settings.Default.userIcon;

because it throws up

"Cannot convert from "string" to "System.Drawing.Icon".

Can somebody correct me?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Igor
  • 13
  • 2
  • Try checking this thread - https://stackoverflow.com/questions/74466/how-do-i-use-an-icon-that-is-a-resource-in-wpf Even though it's WPF, it explains how to get System.Drawing.Icon object when given path. – Hanan Hazani May 26 '20 at 09:31
  • 1
    `Icon icon = new Icon(@"Properties.Settings.Default.userIcon", [The Icon Size, Eventually]);` – Jimi May 26 '20 at 10:13
  • Jimi, your solution worked! Thanks so much! – Igor Jun 09 '20 at 17:20

1 Answers1

0

"The user needs to choose his notifyIcon icon from his hard drive".

Is the icon in a filename somewhere? If the user selects an icon from the hard driver, does he in fact select a file that contains an icon?

If that is the case, you should define Properties.Settings.Default.UserIcon as a string and save the name of the filename. Give your window a property that gets and sets the UserIcon.

private string UserIconFileName
{
    get => Properties.Settings.Default.UserIcon;
    set => properties.Settings.Default.UserIcon = value;
}

private Icon LoadUserIcon
{
    string userIconFileName = this.UserIconFileName
    if (!File.Exists(userIconFileName))
    {
        // TODO: decide what to do if there is no such file
    }
    else
    {
        return new Icon(userIconFileName);
    }
}

Don't forget to Save your properties when closing the program:

private void OnFormClosed(object sender, FormClosedEventArgs e)
{
    Properties.Settings.Default.Save();
}
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116