dllee's profiledllee 的分享空間PhotosBlogListsMore Tools Help

Blog


    6/25/2005

    使用列舉的方式取得列表

    與上週一樣的範例,不同之處只是使用了列舉的方式,而不是笨笨的一個一個列出。總算是試用了 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();
      }
     }
    }

    Comments (3)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Picture of Anonymous
    Flyup2005 wrote:
    1.範例又小..因為我使用簡易程式碼範本...要寫作品在用正規的範本^_^
    2.BLOG都一樣...只是IE瀏覽比較不會像MSN網頁箱簿開啟還要載入ActiveX.
    東森有專人負責管理,所以Blog比較活潑.^_^
    July 23
    Picture of Anonymous
    dllee wrote:
    謝謝 :)
    不過,您的範例又小又好用,比較經典,我的看起來太大了,都有點擔心 MySpace 不給我貼...
    看了您在東森的 blog
    http://bloguide.ettoday.com/flyup2005/
    好像在那兒的回應比較多...
    東森的 blog 有什麼限制嗎? 程式碼好貼嗎?
    July 1
    Picture of Anonymous
    Flyup2005 wrote:
    看法不同,學習角度就不同。
    範例越來越棒喔^_^
    June 25

    Trackbacks

    The trackback URL for this entry is:
    http://sharedllee.spaces.live.com/blog/cns!AFE16592B135E8B3!139.trak
    Weblogs that reference this entry
    • None