alternating icon files in image display

have two conditions, where i would like to display two different icons

Page 1 of 1

4 Replies - 1791 Views - Last Post: 18 February 2010 - 05:34 AM Rate Topic: -----

#1 Guest_skyla007*


Reputation:

alternating icon files in image display

Posted 17 February 2010 - 04:45 AM

Goodday fellow programmers,

I am new to C# programming, especially the gui part, so please bear with me...

here is my question: i have written an application that acts much like a navigation bar. The idea is that i will have the name of a service and along side it, there is a green circle icon, indicating that the service is running, and when the service is not running, i would like to display another icon, same circular image, but only coloured in red.

I thought about having both icons on top of each other, and then controlling the visibility by reading the states of my services.

Ideally however, the solution will be something where i have the two icon files as part of my project, i have the location where it should be printed, and programatically i will then - during runtime put the correct icon in the place it should be displayed... Could you please help me with this part?

I hope my explanation makes sense.

Is This A Good Question/Topic? 0

Replies To: alternating icon files in image display

#2 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: alternating icon files in image display

Posted 17 February 2010 - 05:09 AM

I have an idea how to do this, but could you please post the sample code that you have tried so far.

I have an idea how to do this, but could you please post the sample code that you have tried so far.
Was This Post Helpful? 0
  • +
  • -

#3 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: alternating icon files in image display

Posted 17 February 2010 - 05:21 AM

You could try to put your 2 icons in an ImageList. It is a Class especially designed for this kind of thing:
http://msdn.microsof...ist(VS.71).aspx
You can store multiple images in it and then easily tell other objects which one of the images to display.
The MSDN page contains example code on how to use it!
Happy Coding!
Was This Post Helpful? 1
  • +
  • -

#4 Guest_skyla007*


Reputation:

Re: alternating icon files in image display

Posted 18 February 2010 - 03:25 AM

Hi All,

I read up on the solution suggested by Ferencn, however I found the following:

ImageList is a Windows.Forms concept and not a WPF concept - ( the project I am working on is WPF )
you can use Windows.Forms utilities in a WPF project by telling VS explicitly that that you'll be using this construct. I tried doing all of this, and could programmatically load my icon files into the imagelist. But this is how far I got with this specific approach, as I cannot seem to get an Image component to use the imageList...

I now have the following code:

...
System.Windows.Controls.Image service_running_icon;
System.Windows.Controls.Image service_stopped_icon;
...

string[] iconFiles = Directory.GetFiles("D:\\Workspace\\icons", "*.ico");
// this dir contains two icon files for running and for stopped
foreach (string iconFile in iconFiles)
{
// create a new icon and add to to the image list
Icon newIcon = new Icon(iconFile);
imageList1.Images.Add(newIcon);
}


I would like to know how I can change the source image of service_running_icon programmatically, so that it would point to the icon file? is this possible?
Was This Post Helpful? 0

#5 Guest_Guest*


Reputation:

Re: alternating icon files in image display

Posted 18 February 2010 - 05:34 AM

Hello Guys and Girls,

I am glad to report that I got it working in WPF, without using an ImageList. Here is how I did it:

first declare two variables to hold the two images that needs to be displayed, in my case it is two .ico files, but I am sure other supported image files will work fine, such as .png etc.

System.Windows.Controls.Image service_running_icon;
System.Windows.Controls.Image service_stopped_icon;

private void init_icons()
{
service_running_icon = new System.Windows.Controls.Image();
service_stopped_icon = new System.Windows.Controls.Image();

Uri running_uri = new Uri("C:\\Workspace\\ServiceRunning.ico", UriKind.Absolute);
Uri stopped_uri = new Uri("C:\\Workspace\\ServiceStopped.ico", UriKind.Absolute);

ImageSource imgSource1 = new BitmapImage(running_uri);
ImageSource imgSource2 = new BitmapImage(stopped_uri);

service_running_icon.Source = imgSource1;
service_stopped_icon.Source = imgSource2;

}

now, in your xaml file you can place different Image components in the location you want, without specifying the source... During runtime, the application can now logically decide what source image to display. You accomplish this by calling the following:

image1.Source = service_running_icon.Source;

Hope this post will help someone!
Was This Post Helpful? 0

Page 1 of 1