使用列舉的方式取得列表

與上週一樣的範例,不同之處只是使用了列舉的方式,而不是笨笨的一個一個列出。總算是試用了 foreach

/*
 * Created by 記事本 [:P]
 * User: Lee, Dong-Liang <dllee@edirect168.com>
 * Date: 2005/06/25
 */
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace ItemOfComboBox
{
 public class MainForm : System.Windows.Forms.Form
 {
  private System.Windows.Forms.ComboBox cBoxHatchStyle;
  private System.Windows.Forms.Button SelectColor;
  private System.Windows.Forms.Button SelectBKColor;

  public MainForm()
  {
   // cBoxHatchStyle
   this.cBoxHatchStyle = new System.Windows.Forms.ComboBox();
   this.cBoxHatchStyle.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
   this.cBoxHatchStyle.Location = new System.Drawing.Point(20, 10);
   this.cBoxHatchStyle.Name = "cBoxHatchStyle";
   this.cBoxHatchStyle.Size = new System.Drawing.Size(200, 20);
   this.cBoxHatchStyle.TabIndex = 16;
   this.cBoxHatchStyle.SelectedIndexChanged += new System.EventHandler(this.CBoxHatchStyleSelectedIndexChanged);

   // SelectColor
   this.SelectColor = new System.Windows.Forms.Button();
   this.SelectColor.BackColor = System.Drawing.Color.Blue;
   this.SelectColor.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
   this.SelectColor.Location = new System.Drawing.Point(250, 10);
   this.SelectColor.Name = "SelectColor";
   this.SelectColor.TabIndex = 8;
   this.SelectColor.Text = "Color1";
   this.SelectColor.Click += new System.EventHandler(this.SelectColorClick);

   // SelectBKColor
   this.SelectBKColor = new System.Windows.Forms.Button();
   this.SelectBKColor.BackColor = System.Drawing.Color.Yellow;
   this.SelectBKColor.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
   this.SelectBKColor.Location = new System.Drawing.Point(350, 10);
   this.SelectBKColor.Name = "SelectBKColor";
   this.SelectBKColor.TabIndex = 15;
   this.SelectBKColor.Text = "Color2";
   this.SelectBKColor.Click += new System.EventHandler(this.SelectBKColorClick);

   // MainForm
   this.ClientSize = new System.Drawing.Size(600, 400);
   this.Controls.Add(this.cBoxHatchStyle);
   this.Controls.Add(this.SelectColor);
   this.Controls.Add(this.SelectBKColor);
   this.Name = "MainForm";
   this.Text = "ItemOfComboBox : HatchSytle Example by http://dllee.ktop.com.tw";
   this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form_Paint);
   this.ActiveControl = this.cBoxHatchStyle;

   // C# 的 comboBox 可以直接加入物件,它似乎會自己取 .ToString() 的方法
   // 得到字串作列表,而加入的物件之後可以直接取出使用。
   // <2005-06-25> 使用列舉的方式取得列表。
   Array hsList = Enum.GetValues( typeof( HatchStyle ) );
    foreach ( HatchStyle hs in hsList )
     this.cBoxHatchStyle.Items.Add(hs);
   this.cBoxHatchStyle.SelectedIndex=1;
  }
  
  [STAThread]
  public static void Main(string[] args)
  {
   Application.Run(new MainForm());
  }

  void SelectColorClick(object sender, System.EventArgs e)
  {
   ColorDialog clrDlg = new ColorDialog();
   clrDlg.Color = this.SelectColor.BackColor;
   if (clrDlg.ShowDialog() == DialogResult.OK)
   {
    this.SelectColor.BackColor = clrDlg.Color;
     this.Invalidate();
   }
   clrDlg.Dispose();
  }
  
  void SelectBKColorClick(object sender, System.EventArgs e)
  {
   ColorDialog clrDlg = new ColorDialog();
   clrDlg.Color = this.SelectBKColor.BackColor;
   if (clrDlg.ShowDialog() == DialogResult.OK)
   {
    this.SelectBKColor.BackColor = clrDlg.Color;
     this.Invalidate();
   }
   clrDlg.Dispose();
  }

  void Form_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
   Graphics g = e.Graphics;
   // 物件轉換成其他類別
   HatchStyle selectedHatchStyle = (HatchStyle)Convert.ChangeType(
     this.cBoxHatchStyle.SelectedItem,typeof(HatchStyle));
   // Create a hatch brush with selected hatch style and colors
   HatchBrush brush = new HatchBrush(selectedHatchStyle,
     this.SelectColor.BackColor, this.SelectBKColor.BackColor);
   // Fill rectangle
   g.FillRectangle(brush, this.ClientRectangle);
   // Dispose of objects
   brush.Dispose();
  }

  void CBoxHatchStyleSelectedIndexChanged(object sender, System.EventArgs e)
  {
   this.Invalidate();
  }
 }
}

About dllee

http://gplus.to/dllee http://about.me/dllee
本篇發表於 C#。將永久鏈結加入書籤。

3 Responses to 使用列舉的方式取得列表

  1. Unknown 說道:

    看法不同,學習角度就不同。範例越來越棒喔^_^

  2. dllee 說道:

    謝謝 :)不過,您的範例又小又好用,比較經典,我的看起來太大了,都有點擔心 MySpace 不給我貼…看了您在東森的 bloghttp://bloguide.ettoday.com/flyup2005/好像在那兒的回應比較多…東森的 blog 有什麼限制嗎? 程式碼好貼嗎?

  3. Unknown 說道:

    1.範例又小..因為我使用簡易程式碼範本…要寫作品在用正規的範本^_^ 2.BLOG都一樣…只是IE瀏覽比較不會像MSN網頁箱簿開啟還要載入ActiveX. 東森有專人負責管理,所以Blog比較活潑.^_^

回覆給Unknown 取消回覆