Changing an icon in NotifyIcon in VB.NET

1,745 views
Skip to first unread message

Kevin Hunter

unread,
Aug 29, 2008, 12:00:49 PM8/29/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
OK, here is my problem

i am currently making a program that involves a NotifyIcon. i can
manly change the icon (before the program runs in the properties
window) with no problem, but what i need is a way to have the program
change its icon mid-stream.

i have already make to icon files: let's call them icon1.ico and
icon2.ico. i also have a (working) NotifyIcon, and we'll call that
NotifyIcon1 (these are all default names).

i have tried other things like NotifyIcon1.add("The Icon Location"),
but that just ADDS the new icon to the notification area, and will not
replace the previous one.

so in short: is there a way to code a program to replace on icon in an
NotifyIcon with another?

sallushan

unread,
Sep 6, 2008, 9:41:16 AM9/6/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
A simple solution is that, put your icons in Resources. Then on
runtime change the icon, for example,

NotifyIcon1.Icon = My.Resources.Resources.Icon2

Or you can directly load from file,

NotifyIcon1.Icon = New Icon("E:\Icons\Icon2.ico")

BTW I am talking about VS2005, in which we don't have the "Add()"
method. But I guess above will be valid for VS2008 too.

Regards,
Arsalan Tamiz

mrkoconnell

unread,
Sep 6, 2008, 12:51:39 PM9/6/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi,

I was experimenting with this icon problem and had a very difficult
time attempting to reload another icon file for a NotifyIcon control.
My problem was that I kept getting involved with the resouce file
which is very extensive and written in xml. After about an hour of
messing with this, I can up with a much simpler solution.

I added two NotifyIcon controls and simply swapped the visible state
(true or false) between the two icons. This worked very well.

If you find another way involving reassigning the icon to a simple
control, I am very interested in hearing how you did it.

Thanks,

Kevin O'Connell

Joeizy

unread,
Sep 7, 2008, 4:10:43 PM9/7/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I was able to swap Icons, both using the Resources example and this
approach:

string str1 = @"C:\Data\My Stuff\Images\Cars\untitled.bmp";
string str2 = @"C:\Data\My Stuff\Documents\ebay.txt";
bool flip;
void button1_Click(object sender, EventArgs e) {
if (flip)
notifyIcon1.Icon =
System.Drawing.Icon.ExtractAssociatedIcon(str1);
else
notifyIcon1.Icon =
System.Drawing.Icon.ExtractAssociatedIcon(str2);
flip = !flip;
}

Of course you could just load up the icon using the constructor, but I
didn't have any icon files available. The important part is that I'm
doing it when you click the button, and I'm setting the Icon property
of the NotifiyIcon object.

On Sep 6, 10:51 am, mrkoconnell <mrkcoconn...@yahoo.com> wrote:
> Hi,
>
> I was experimenting with thisiconproblem and had a very difficult
> time attempting to reload anothericonfile for aNotifyIconcontrol.
> My problem was that I kept getting involved with the resouce file
> which is very extensive and written in xml.  After about an hour of
> messing with this, I can up with a much simpler solution.
>
> I added twoNotifyIconcontrols and simply swapped the visible state
> (true or false) between the two icons.  This worked very well.
>
> If you find another way involving reassigning theiconto a simple
> control, I am very interested in hearing how you did it.
>
> Thanks,
>
> Kevin O'Connell
>
> On Aug 29, 9:00 am, Kevin Hunter <mathwiz...@gmail.com> wrote:
>
> > OK, here is my problem
>
> > i am currently making a program that involves aNotifyIcon. i can
> > manly change theicon(before the program runs in the properties
> > window) with no problem, but what i need is a way to have the program
> > change itsiconmid-stream.
>
> > i have already make toiconfiles: let's call them icon1.ico and
> > icon2.ico. i also have a (working)NotifyIcon, and we'll call that
> > NotifyIcon1 (these are all default names).
>
> > i have tried other things like NotifyIcon1.add("TheIconLocation"),
> > but that just ADDS the newiconto the notification area, and will not

mrkoconnell

unread,
Sep 9, 2008, 4:35:40 PM9/9/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Thanks for the tip. I am using Visual Studio 2003 with framework 1.4
so I didn't find the "ExtractAssociatedIcon" function. I used:
System.Drawing.Icon micon = new Icon(str1);
this.notifyIcon1.Icon=micon;
and this worked fine.
> > >NotifyIconwith another?- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages