Connecting Tech Pros Worldwide Forums | Help | Site Map

possible to get the exact handle of Icon which is inserted into ImageList

Newbie
 
Join Date: Nov 2008
Location: Kathmandu
Posts: 28
#1: Nov 19 '08
In C#, Is it possible to get the exact handle of Icon which is inserted into ImageList from ImageList?

Note: ImageList_GetIcon() returns not exact handle

Eg.
------------------------------------
Icon x=new Icon(....);
_imageList.Images.Add(x);
IntPtr iHnd=x.Handle;
------------------------------------

And I want from _imageList so that ......
_imageList.something.Handle = x.Handle is satisfied

nukefusion's Avatar
Expert
 
Join Date: Mar 2008
Location: Essex, UK
Posts: 197
#2: Nov 19 '08

re: possible to get the exact handle of Icon which is inserted into ImageList


I wouldn't have thought it was possible to do exactly what you want, in the way you want. Here's why:

When you add an Icon to the ImageList, it is first converted to a bitmap, which is a whole new object with a whole new handle. Because of this, the handle of the Icon you added to the ImageList initally will never equal the handle of the corresponding image in the ImageList. They are different objects.
If you want to be able to marry up the original icon with an item in the list at a later date I would suggest using the overload of ImageList.Add that allows you to add a key. You could even use the original handle as a key. See this following example.

Expand|Select|Wrap|Line Numbers
  1.    Icon icon = new Icon(); // Construct your icon here
  2.             IntPtr p1 = icon.Handle;
  3.             string key = p1.ToString();
  4.             imageList1.Images.Add(key, icon);
  5.  
You can then find this image later using:

Expand|Select|Wrap|Line Numbers
  1.   IntPtr p1 = icon.Handle;
  2.             string key = p1.ToString();
  3.             Image i = imageList1.Images[key];
Newbie
 
Join Date: Nov 2008
Location: Kathmandu
Posts: 28
#3: Nov 23 '08

re: possible to get the exact handle of Icon which is inserted into ImageList


Hey nukefusion,
Its working thnx.
Reply