1

Context

In a C# Windows Form project, there are two ways to have a Button show an image:

Question

  • Why two solutions ? I'm suspecting ImageList is there to stay close to the underlying Win32 software layer API.
  • Is there any reason to prefer one over the other, for a Button ? (For some other controls you might not have any choice.)
Stéphane Gourichon
  • 6,493
  • 4
  • 37
  • 48

1 Answers1

1

Unless someone has a better rationale, I would say: just do what respects simplicity and good practices such as "don't repeat yourself".

Argument in favor or ImageList

  • For small image collections that you set from the Visual Studio designer, it offers an ImageList editor that guide you and keeps related images together in the source code, rather than maintaining collections separately.
  • If some other Controls needs you to maintain an ImageList anyway, you've already paid the price, you may just as well use that for the Buttons, too.

Argument in favor or Images

When you have a choice:

The ImageList case

  • ImageList needs you to somehow manages indexes to the images (integers or strings -- and string keying is buggy ?), either explicitly or implicitly.
  • The compiler cannot check if indexes or strings are misused anyway.

The Image case

  • You may not need to manage a collection. You can use individual objects to set in the Image property.
  • Even the case when you need a collection is better. You can index the collection using whatever key type you wish, for example a Dictionary. If you index the collection with an Enum, the compiler can check at compile time and help you (Intellisense) at source code writing time.

Bottom line

I'd favor the Image option and handle my own collection if needed using an Enum, but I would name that personal preference.

Community
  • 1
  • 1
Stéphane Gourichon
  • 6,493
  • 4
  • 37
  • 48