CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
花落谁家,你作主! 盛大widget设计大赛英雄榜
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

各位高手,小弟有两个问题要请教......

楼主yezhitu(藍天の白雲)2004-09-03 12:55:38 在 .NET技术 / C# 提问

1.请问如何把一个枚举里的所有名称和值打印出来?  
   
  2.如何将一个键名的名称转成他的KEY码呢?就是例如把"A"转成65.  
   
   
  望高手们可以解答小弟的这两个问题,感激不尽..... 问题点数:50、回复次数:5Top

1 楼jiezhi(风满袖)回复于 2004-09-03 13:01:08 得分 15

enum   Days   {   Saturday,   Sunday,   Monday,   Tuesday,   Wednesday,   Thursday,   Friday   };  
  ...  
  Type   weekdays   =   typeof(Days);  
  foreach   (   string   s   in   Enum.GetNames(weekdays)   )  
            Console.WriteLine(   "{0,-11}=   {1}",   s,   Enum.Format(   weekdays,   Enum.Parse(weekdays,   s),   "d"));  
   
  foreach(int   i   in   Enum.GetValues(weekdays   ))  
              Console.WriteLine(i);  
  Top

2 楼The123(Shall We Dance? :))回复于 2004-09-03 13:01:31 得分 15

using   System;  
   
  public   class   EnumTest   {  
          enum   Days   {   Saturday,   Sunday,   Monday,   Tuesday,   Wednesday,   Thursday,   Friday   };  
          enum   BoilingPoints   {   Celcius   =   100,   Fahrenheit   =   212   };  
          [FlagsAttribute]  
          enum   Colors   {   Red   =   1,   Green   =   2,   Blue   =   4,   Yellow   =   8   };  
   
          public   static   void   Main()   {  
   
                  Type   weekdays   =   typeof(Days);  
                  Type   boiling   =   typeof(BoilingPoints);  
   
                  Console.WriteLine("The   days   of   the   week,   and   their   corresponding   values   in   the   Days   Enum   are:");  
   
                  foreach   (   string   s   in   Enum.GetNames(weekdays)   )  
                          Console.WriteLine(   "{0,-11}=   {1}",   s,   Enum.Format(   weekdays,   Enum.Parse(weekdays,   s),   "d"));  
   
                  Console.WriteLine();  
                  Console.WriteLine("Enums   can   also   be   created   which   have   values   that   represent   some   meaningful   amount.");  
                  Console.WriteLine("The   BoilingPoints   Enum   defines   the   following   items,   and   corresponding   values:");  
   
                  foreach   (   string   s   in   Enum.GetNames(boiling)   )  
                          Console.WriteLine(   "{0,-11}=   {1}",   s,   Enum.Format(boiling,   Enum.Parse(boiling,   s),   "d"));  
   
                  Colors   myColors   =   Colors.Red   |   Colors.Blue   |   Colors.Yellow;  
                  Console.WriteLine();  
                  Console.WriteLine("myColors   holds   a   combination   of   colors.   Namely:   {0}",   myColors);  
          }  
  }  
   
  ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemenumclasstopic.htmTop

3 楼qifanghcy(菜鸟QiFang)回复于 2004-09-03 13:03:04 得分 0

1.好像没办法打印...  
  2.参见如下代码:  
  private   void   Form1_KeyDown(object   sender,   System.Windows.Forms.KeyEventArgs   e)  
  {  
            MessageBox.Show(e.KeyValue.ToString());       //按a键,值为65  
  }Top

4 楼jiezhi(风满袖)回复于 2004-09-03 13:07:02 得分 10

(int)Keys.ATop

5 楼The123(Shall We Dance? :))回复于 2004-09-03 13:14:51 得分 10

2  
    (((int)Enum.Parse(typeof(System.Windows.Forms.Keys),"A",true)).ToString())Top

相关问题

  • 向各位请教两个问题,请各位前辈指教!
  • 各位大虾问两个问题,急!!!
  • 请教各位高手两个问题!
  • 请教各位两个问题
  • 各位朋友问两个小问题?
  • 各位帮个忙!
  • 各位帮个忙!!!!
  • 各位帮个忙
  • 各位帮个忙!
  • 各位朋友,我手上没书。。请各位看看这两个语句的意思。

关键词

  • weekdays
  • enum
  • typeof
  • days
  • writeline
  • console
  • type

得分解答快速导航

  • 帖主:yezhitu
  • jiezhi
  • The123
  • jiezhi
  • The123

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo