简体   繁体   English

Windows窗体按钮:Image或ImageList + ImageIndex?

[英]Windows Forms Button : Image or ImageList + ImageIndex?

Context 上下文

In a C# Windows Form project, there are two ways to have a Button show an image: 在C#Windows Form项目中,有两种方法可以使Button显示图像:

Question

  • Why two solutions ? 为什么要两种解决方案? I'm suspecting ImageList is there to stay close to the underlying Win32 software layer API. 我怀疑ImageList是否位于靠近底层Win32软件层API的位置。
  • Is there any reason to prefer one over the other, for a Button ? 有什么理由比按钮更喜欢另一个吗? (For some other controls you might not have any choice.) (对于其他一些控件,您可能别无选择。)

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 支持赞成或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. 对于您从Visual Studio设计器中设置的小型图像集合,它提供了一个ImageList编辑器来指导您并将相关图像保留在源代码中,而不是单独维护集合。
  • 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. 如果其他控件仍然需要您维护ImageList,则您已经付出了代价,您也可以将其用于Buttons。

Argument in favor or Images 赞成观点或形象

When you have a choice: 当您有选择时:

The ImageList case ImageList案例

  • ImageList needs you to somehow manages indexes to the images (integers or strings -- and string keying is buggy ? ), either explicitly or implicitly. ImageList需要您以某种方式显式或隐式管理图像的索引(整数或字符串- 字符串键是否有bug? )。
  • 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. 您可以使用单个对象在Image属性中进行设置。
  • 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. 如果使用Enum为集合建立索引,则编译器可以在编译时进行检查,并在源代码编写时为您提供帮助(Intellisense)。

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. 我希望使用“图像”选项,并在需要时使用枚举来处理我自己的收藏集,但我会说出个人喜好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM