在哪里使用枚举以及如何模拟messageboxICon枚举
我想知道为什么以及在哪里应该使用枚举器,我有一组图像,我想为每个图像分配数字并将其放入枚举中,我想设置数字而不是 当我想设置图像时的整个文件名,就像 MessageBoxIcon enum
谢谢
I want to know why and where should we use the enumerators,i have a set of images i want to assign numbers to each and put it in a enum and i want to set the number instead of
entire filename when i want to set the image ,just like MessageBoxIcon enum
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您的意思是枚举而不是枚举器。枚举用于指定可能的符号值或含义的离散集合。你确定这是你想要的吗?请注意,枚举是在编译时内置的,之后无法更改,因此如果您的图像集要在运行时更改,您将需要一些其他数据结构。
另请注意,枚举不能包含除其成员名称之外的任何信息,因此您无法将文件名与它们相关联。为此,您需要一个
字典
或类似的东西,例如:An in some method:
I presume you mean enumerations rather than enumerators. Enums are used to specify a discrete collection of possible symbolic values or meanings. Are you sure this is what you want? Note that enums are baked in at compile time and can't be changed afterwards, so if your set of images is going to change at runtime, you'll need some other data structure.
Also note that enums can't contain any information other than the names of their members, so you wouldn't be able to associate file names with them, for example. To do so, you'd need a
Dictionary
or something similar, e.g.:An in some method:
Enum 是集合所有可能值的显式列表。只要有一组值可供选择,就可以使用枚举。枚举的另一个流行用法是设置 flags
对于您的问题,我相信枚举可能不是最好的方法,因为文件名可能包含 C# 代码中不允许的字符。虽然您可以使用 Enum 描述hack 来做到这一点。只需通过添加
this< 将 GetDescription 方法设为扩展方法即可/code> 参数的关键字。
Enum is an explicit listing of all possible values of a set. You can use enums whenever you have a set of values to choose from. Another popular use of enums is to set flags
For your problem, I believe enums may not be the best way to go, as I filenames may contain characters not allowed in c# code. Although You can use the Enum descriptions hack to do this. Just make the GetDescription method an extension method by adding
this
keyword to the argument.我强烈建议您将希望能够选择的图像存储在 C# 项目的资源文件中。 (请参阅我的回答了解如何执行此操作。)
然后,您需要在代码中创建一个枚举(这只是一个定义集合中所有可能值的列表)。我建议在项目中创建一个新的空文件来保存枚举,并将其命名为与枚举相同的名称,以便您能够轻松地再次找到它。您的枚举文件将如下所示(您可以添加任意数量的值):
然后,无论您想要在何处指定要显示的图像,都可以引用枚举值之一。在要处理显示图像的代码中,您必须使用
switch
语句将您指定的枚举值转换为存储在项目资源中的图像文件:在上面的示例中,
FlowerButtonClick
方法仅指定您希望显示PartyImage
枚举中名为Flower
的图像。它包含的代码相当于调用MessageBox.Show
函数并指定要在消息框中显示的图像。您可以根据需要拥有任意数量的这些方法,并且它们可以位于代码中的任何位置,就像您可以从任何地方显示带有指定图标的消息框一样。UpdateDisplayedImage
方法的实际工作是从项目的资源文件中检索与您指定的枚举值相对应的图像并显示它。这相当于 MessageBox.Show 函数在创建并显示带有指定图标的新消息框时在内部完成的工作。有关枚举的更多信息,建议阅读:
枚举设计 (MSDN)
C# 中的枚举 (MSDN)< /p>
I highly recommend that you store the images you want to be able to choose from in your C# project's Resources file. (See my answer here for how to do this.)
Then, you need to create an enumeration (which is just a listing that defines all of the possible values in a set) in your code. I recommend creating a new, empty file in your project to hold your enumeration and naming it the same as the enum so that you will be able to find it again easily. Your enumeration file will look something like this (you can add as many values as you want):
Then, wherever you want to specify the image to be displayed, you can reference one of the enum values. In the code that is going to handle displaying the image, you'll have to use a
switch
statement that will translate the enum value that you specified to an image file that is stored in your project resources:In the example above, the
FlowerButtonClick
method simply specifies that you want the image calledFlower
in thePartyImage
enum to be displayed. The code it contains is equivalent to calling theMessageBox.Show
function and specifying the image you want to appear on the message box. You can have as many of these methods as you want, and they can be anywhere in your code, just like you can display a message box with a specified icon from anywhere.The
UpdateDisplayedImage
method does the actual work of retrieving the image from your project's Resources file that corresponds to the enum value that you specified and displaying it. This is equivalent to the work that is done internally by theMessageBox.Show
function when it creates and displays a new message box with the specified icon.Recommended reading for more information about enumerations:
Enumeration Design (MSDN)
Enums in C# (MSDN)