當前位置: 首頁>>代碼示例>>C#>>正文


C# Enum.GetNames方法代碼示例

本文整理匯總了C#中System.Enum.GetNames方法的典型用法代碼示例。如果您正苦於以下問題:C# Enum.GetNames方法的具體用法?C# Enum.GetNames怎麽用?C# Enum.GetNames使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Enum的用法示例。


在下文中一共展示了Enum.GetNames方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;

public class GetNamesTest {
    enum Colors { Red, Green, Blue, Yellow };
    enum Styles { Plaid, Striped, Tartan, Corduroy };

    public static void Main() {

        Console.WriteLine("The members of the Colors enum are:");
        foreach(string s in Enum.GetNames(typeof(Colors)))
            Console.WriteLine(s);

        Console.WriteLine();

        Console.WriteLine("The members of the Styles enum are:");
        foreach(string s in Enum.GetNames(typeof(Styles)))
            Console.WriteLine(s);
    }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:20,代碼來源:Enum.GetNames

輸出:

The members of the Colors enum are:
Red
Green
Blue
Yellow

The members of the Styles enum are:
Plaid
Striped
Tartan
Corduroy

示例2: Main

//引入命名空間
using System;

enum SignMagnitude { Negative = -1, Zero = 0, Positive = 1 };
 
public class Example
{
   public static void Main()
   {
      foreach (var name in Enum.GetNames(typeof(SignMagnitude))) {
         Console.WriteLine("{0,3:D}     0x{0:X}     {1}",
                           Enum.Parse(typeof(SignMagnitude), name),
                           name);
}   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:15,代碼來源:Enum.GetNames

輸出:

0     0x00000000     Zero
1     0x00000001     Positive
-1     0xFFFFFFFF     Negative

示例3: Form1

//引入命名空間
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.ComboBox lstAlignmentH;
    private System.Windows.Forms.ComboBox lstTrimming;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.ComboBox lstAlignmentV;
        public Form1() {
            InitializeComponent();
      ResizeRedraw = true;
      lstAlignmentH.DataSource = Enum.GetNames(typeof(StringAlignment));
      lstAlignmentV.DataSource = Enum.GetNames(typeof(StringAlignment));
      lstTrimming.DataSource = Enum.GetNames(typeof(StringTrimming));
        }

    private void TextWrap_Paint(object sender, PaintEventArgs e)
    {
      string text = "Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Center each line of text.Center each line of textCenter each line of textCenter each line of textCenter each line of textCenter each line of text";
          
      StringFormat stringFormat = new StringFormat();
      stringFormat.Alignment = (StringAlignment)Enum.Parse(typeof(StringAlignment), lstAlignmentH.Text);
      stringFormat.LineAlignment = (StringAlignment)Enum.Parse(typeof(StringAlignment), lstAlignmentV.Text);
      
      stringFormat.Trimming = (StringTrimming)Enum.Parse(typeof(StringTrimming), lstTrimming.Text);

      Font font = new Font("Verdana", 12);
      Rectangle rect = new Rectangle(30, 110, Width - 60, Height - 160);
      e.Graphics.DrawString(text, font, Brushes.Black, rect, stringFormat);

      Pen pen = Pens.Black;
      e.Graphics.DrawRectangle(pen, rect);
    }

    private void lst_Changed(object sender, EventArgs e)
    {
      Invalidate();
    }
    private void InitializeComponent()
    {
      this.label1 = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.lstAlignmentH = new System.Windows.Forms.ComboBox();
      this.lstTrimming = new System.Windows.Forms.ComboBox();
      this.label3 = new System.Windows.Forms.Label();
      this.lstAlignmentV = new System.Windows.Forms.ComboBox();
      this.SuspendLayout();

      this.label1.AutoSize = true;
      this.label1.Location = new System.Drawing.Point(12, 19);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(113, 13);
      this.label1.TabIndex = 0;
      this.label1.Text = "Alignment (Horizontal):";

      this.label2.AutoSize = true;
      this.label2.Location = new System.Drawing.Point(12, 74);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(49, 13);
      this.label2.TabIndex = 1;
      this.label2.Text = "Trimming:";

      this.lstAlignmentH.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.lstAlignmentH.FormattingEnabled = true;
      this.lstAlignmentH.Location = new System.Drawing.Point(131, 16);
      this.lstAlignmentH.Name = "lstAlignmentH";
      this.lstAlignmentH.Size = new System.Drawing.Size(121, 21);
      this.lstAlignmentH.TabIndex = 2;
      this.lstAlignmentH.SelectedIndexChanged += new System.EventHandler(this.lst_Changed);

      this.lstTrimming.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.lstTrimming.FormattingEnabled = true;
      this.lstTrimming.Location = new System.Drawing.Point(131, 69);
      this.lstTrimming.Name = "lstTrimming";
      this.lstTrimming.Size = new System.Drawing.Size(121, 21);
      this.lstTrimming.TabIndex = 3;
      this.lstTrimming.SelectedIndexChanged += new System.EventHandler(this.lst_Changed);

      this.label3.AutoSize = true;
      this.label3.Location = new System.Drawing.Point(12, 47);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(100, 13);
      this.label3.TabIndex = 4;
      this.label3.Text = "Alignment (Vertical):";

      this.lstAlignmentV.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.lstAlignmentV.FormattingEnabled = true;
      this.lstAlignmentV.Location = new System.Drawing.Point(131, 42);
      this.lstAlignmentV.Name = "lstAlignmentV";
      this.lstAlignmentV.Size = new System.Drawing.Size(121, 21);
      this.lstAlignmentV.TabIndex = 5;
      this.lstAlignmentV.SelectedIndexChanged += new System.EventHandler(this.lst_Changed);

      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(319, 296);
      this.Controls.Add(this.lstAlignmentV);
      this.Controls.Add(this.label3);
      this.Controls.Add(this.lstTrimming);
      this.Controls.Add(this.lstAlignmentH);
      this.Controls.Add(this.label2);
      this.Controls.Add(this.label1);
      this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.Name = "TextWrap";
      this.Text = "Text Wrap";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.TextWrap_Paint);
      this.ResumeLayout(false);
      this.PerformLayout();

    }
      [STAThread]
      static void Main()
      {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
      }

}
開發者ID:C#程序員,項目名稱:System,代碼行數:127,代碼來源:Enum.GetNames


注:本文中的System.Enum.GetNames方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。