本文轉貼自我 MSN ShareSpace 2005/6/25 的 使用列舉的方式取得列表
:
與上週一樣的範例,不同之處只是使用了列舉的方式,而不是笨笨的一個一個列出。總算是試用了 foreach 
-
-
-
-
-
- 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()
- {
-
- 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);
-
-
- 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);
-
-
- 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);
-
-
- 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;
-
-
-
-
- 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));
-
- HatchBrush brush = new HatchBrush(selectedHatchStyle,
- this.SelectColor.BackColor, this.SelectBKColor.BackColor);
-
- g.FillRectangle(brush, this.ClientRectangle);
-
- brush.Dispose();
- }
-
- void CBoxHatchStyleSelectedIndexChanged(object sender, System.EventArgs e)
- {
- this.Invalidate();
- }
- }
- }