-1

I store the icons for my applications inside several image lists.

Have one to:

X16

X24

X48

X32

Both TActions and direct access to place ICOs in a TButton, or TImage came from this several Image Lists.

My problem is that when I need to remove one its nightmare

I thought of setting a CONST value to everyone, but as some are used in TActions it’s not a complete solution.

How are you guys doing this and what solutions do you have to solve or at least improve this? I use DELPHI 2007.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Jlouro
  • 4,515
  • 9
  • 60
  • 91
  • 3
    I use constants to store indexes like David's answer below, but in your case, if I wanted to remove an image, I think I would load a "blank" image there and reuse it the next time around. – Marcus Adams Dec 21 '11 at 15:38
  • @marcus I use variables rather than constants so I have that flexibility. It also means that I can share code between different projects and not be bound by the same numbers in all projects. – David Heffernan Dec 21 '11 at 15:41
  • 1
    I have no idea what this question is asking. Jlouro, could you please clarify? Or maybe @David can, since he at least understood it well enough to answer it. – Rob Kennedy Dec 21 '11 at 15:45
  • @rob I take it that when you remove an icon from an image list you then have to modify the image index for all actions, buttons etc. that use the image list – David Heffernan Dec 21 '11 at 15:48
  • @Rob, understanding questions is not mandadory requisite for answering :-) – OnTheFly Dec 21 '11 at 17:15
  • I reversed engendered the Q, by reading David's answer. doing the same in my application. setting the indexes at runtime. – kobik Dec 21 '11 at 23:30

2 Answers2

4

What I do is add all the icons at runtime by loading from then from resources. When I add them I save the index of the added icon to a global variable. I also assign the ImageIndex property of each action at runtime by referring to these global variables.

This allows flexibility to add and remove icons to the project without having numbering problems. The approach caters for runtime icon size decisions based on font scaling. The drawback is that you don't get to see the images at design time which is a drawback. If you want to have all the flexibility outlined above I don't see a better solution. In an ideal world the images would be identified by a name or an ID rather than a contiguous index into an array. But to achieve that you would need to implement a lot of code on top of the VCL.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • By resource you mean from the .res file, not from a folder ? – Jlouro Dec 21 '11 at 16:19
  • Yes I use .res files linked to the executable. You could load from a folder as an alternative. – David Heffernan Dec 21 '11 at 16:22
  • +1. The WinForms ImageList in .NET allows a string name to each image. Then you can do il.Images.IndexOfKey("myicon") to get the index or even directly on the target component cmp.SelectedImageKey="myicon". It would help if the Delphi ImageList was extended with this functionality. – Ville Krumlinde Apr 05 '12 at 07:38
3

There are several solutions to this problem.

If you want to use constants, but you don´t want to change them all each time you remove an image. You can do the following:

const
  idImgA   =   0;
  idImgB   =   idImgA + 1;
  idImgC   =   idImgB + 1;
  idImgD   =   idImgC + 1;
  idImgE   =   idImgD + 1;
  idImgF   =   idImgE + 1;
  idImgG   =   idImgF + 1;

When you want to remove image D, you only need to change two lines:

const
  idImgA   =   0;
  idImgB   =   idImgA + 1;
  idImgC   =   idImgB + 1;
  idImgE   =   idImgC + 1;
  idImgF   =   idImgE + 1;
  idImgG   =   idImgF + 1;

Another way is to work with enum types:

type
  TImgEnum = (imgA, imgB, imgC, imgD, imgE, imgG);

You can use the Ord operator to get the image index:

index := Ord(Enum);
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202