轉貼 Item Of ComboBox

摘要:轉貼 Item Of ComboBox

本文轉貼自我 MSN ShareSpace 2005/6/18 的Item Of ComboBox

今天終於去下載了 .NET SDK,試試 csc /out:sample.exe sample.cs 還真是簡單
以下是最近在學 GDI+ 時,修改書本的範例,所得到的心得,原本以為,.NET ComboBox 為什麼 List Item 只能放 object 沒有 string,原來她把這兩個東西給合併了。


附件:ItemOfComboBox.cs

PS1. MSN Spaces 不太適合貼程式碼... 金害...


PS2. 我貼的圖內含程式原始碼及 csc 輸出的可執行檔喔,下載改名為 .zip 可解出。


  1. /* 
  2.  * Created by 記事本 tongue-out 
  3.  * User: Lee, Dong-Liang  dllee@edirect168.com 
  4.  * Date: 2005/06/18 
  5.  */  
  6. using System;  
  7. using System.Drawing;  
  8. using System.Drawing.Drawing2D;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace ItemOfComboBox  
  12. {  
  13.     public class MainForm : System.Windows.Forms.Form  
  14.     {  
  15.         private System.Windows.Forms.ComboBox cBoxHatchStyle;  
  16.         private System.Windows.Forms.Button SelectColor;  
  17.         private System.Windows.Forms.Button SelectBKColor;  
  18.   
  19.         public MainForm()  
  20.         {  
  21.             // cBoxHatchStyle  
  22.             this.cBoxHatchStyle = new System.Windows.Forms.ComboBox();  
  23.             this.cBoxHatchStyle.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;  
  24.             this.cBoxHatchStyle.Location = new System.Drawing.Point(20, 10);  
  25.             this.cBoxHatchStyle.Name = "cBoxHatchStyle";  
  26.             this.cBoxHatchStyle.Size = new System.Drawing.Size(200, 20);  
  27.             this.cBoxHatchStyle.TabIndex = 16;  
  28.             this.cBoxHatchStyle.SelectedIndexChanged += new System.EventHandler(this.CBoxHatchStyleSelectedIndexChanged);  
  29.   
  30.             // SelectColor  
  31.             this.SelectColor = new System.Windows.Forms.Button();  
  32.             this.SelectColor.BackColor = System.Drawing.Color.Blue;  
  33.             this.SelectColor.FlatStyle = System.Windows.Forms.FlatStyle.Flat;  
  34.             this.SelectColor.Location = new System.Drawing.Point(250, 10);  
  35.             this.SelectColor.Name = "SelectColor";  
  36.             this.SelectColor.TabIndex = 8;  
  37.             this.SelectColor.Text = "Color1";  
  38.             this.SelectColor.Click += new System.EventHandler(this.SelectColorClick);  
  39.   
  40.             // SelectBKColor  
  41.             this.SelectBKColor = new System.Windows.Forms.Button();  
  42.             this.SelectBKColor.BackColor = System.Drawing.Color.Yellow;  
  43.             this.SelectBKColor.FlatStyle = System.Windows.Forms.FlatStyle.Flat;  
  44.             this.SelectBKColor.Location = new System.Drawing.Point(350, 10);  
  45.             this.SelectBKColor.Name = "SelectBKColor";  
  46.             this.SelectBKColor.TabIndex = 15;  
  47.             this.SelectBKColor.Text = "Color2";  
  48.             this.SelectBKColor.Click += new System.EventHandler(this.SelectBKColorClick);  
  49.   
  50.             // MainForm  
  51.             this.ClientSize = new System.Drawing.Size(600, 400);  
  52.             this.Controls.Add(this.cBoxHatchStyle);  
  53.             this.Controls.Add(this.SelectColor);  
  54.             this.Controls.Add(this.SelectBKColor);  
  55.             this.Name = "MainForm";  
  56.             this.Text = "ItemOfComboBox : HatchSytle Example by http://dllee.ktop.com.tw";  
  57.             this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form_Paint);  
  58.             this.ActiveControl = this.cBoxHatchStyle;  
  59.   
  60.             // C# 的 comboBox 可以直接加入物件,它似乎會自己取 .ToString() 的方法  
  61.             // 得到字串作列表,而加入的物件之後可以直接取出使用。  
  62.             this.cBoxHatchStyle.Items.Add(HatchStyle.BackwardDiagonal);  
  63.             this.cBoxHatchStyle.Items.Add(HatchStyle.Cross); // 與 LargeGrid 相同?!  
  64.             this.cBoxHatchStyle.Items.Add(HatchStyle.DarkDownwardDiagonal);  
  65.             this.cBoxHatchStyle.Items.Add(HatchStyle.DarkHorizontal);  
  66.             this.cBoxHatchStyle.Items.Add(HatchStyle.DarkUpwardDiagonal);  
  67.             this.cBoxHatchStyle.Items.Add(HatchStyle.DarkVertical);  
  68.             this.cBoxHatchStyle.Items.Add(HatchStyle.DashedDownwardDiagonal);  
  69.             this.cBoxHatchStyle.Items.Add(HatchStyle.DashedHorizontal);  
  70.             this.cBoxHatchStyle.Items.Add(HatchStyle.DashedUpwardDiagonal);  
  71.             this.cBoxHatchStyle.Items.Add(HatchStyle.DashedVertical);  
  72.             this.cBoxHatchStyle.Items.Add(HatchStyle.DiagonalBrick);  
  73.             this.cBoxHatchStyle.Items.Add(HatchStyle.DiagonalCross);  
  74.             this.cBoxHatchStyle.Items.Add(HatchStyle.Divot);  
  75.             this.cBoxHatchStyle.Items.Add(HatchStyle.DottedDiamond);  
  76.             this.cBoxHatchStyle.Items.Add(HatchStyle.DottedGrid);  
  77.             this.cBoxHatchStyle.Items.Add(HatchStyle.ForwardDiagonal);  
  78.             this.cBoxHatchStyle.Items.Add(HatchStyle.Horizontal);  
  79.             this.cBoxHatchStyle.Items.Add(HatchStyle.HorizontalBrick);  
  80.             this.cBoxHatchStyle.Items.Add(HatchStyle.LargeCheckerBoard);  
  81.             this.cBoxHatchStyle.Items.Add(HatchStyle.LargeConfetti);  
  82.             this.cBoxHatchStyle.Items.Add(HatchStyle.LargeGrid);  
  83.             this.cBoxHatchStyle.Items.Add(HatchStyle.LightDownwardDiagonal);  
  84.             this.cBoxHatchStyle.Items.Add(HatchStyle.LightHorizontal);  
  85.             this.cBoxHatchStyle.Items.Add(HatchStyle.LightUpwardDiagonal);  
  86.             this.cBoxHatchStyle.Items.Add(HatchStyle.LightVertical);  
  87.             this.cBoxHatchStyle.Items.Add(HatchStyle.Max);  
  88.             this.cBoxHatchStyle.Items.Add(HatchStyle.Min);  
  89.             this.cBoxHatchStyle.Items.Add(HatchStyle.NarrowHorizontal);  
  90.             this.cBoxHatchStyle.Items.Add(HatchStyle.NarrowVertical);  
  91.             this.cBoxHatchStyle.Items.Add(HatchStyle.OutlinedDiamond);  
  92.             this.cBoxHatchStyle.Items.Add(HatchStyle.Percent05);  
  93.             this.cBoxHatchStyle.Items.Add(HatchStyle.Percent10);  
  94.             this.cBoxHatchStyle.Items.Add(HatchStyle.Percent20);  
  95.             this.cBoxHatchStyle.Items.Add(HatchStyle.Percent25);  
  96.             this.cBoxHatchStyle.Items.Add(HatchStyle.Percent30);  
  97.             this.cBoxHatchStyle.Items.Add(HatchStyle.Percent40);  
  98.             this.cBoxHatchStyle.Items.Add(HatchStyle.Percent50);  
  99.             this.cBoxHatchStyle.Items.Add(HatchStyle.Percent60);  
  100.             this.cBoxHatchStyle.Items.Add(HatchStyle.Percent70);  
  101.             this.cBoxHatchStyle.Items.Add(HatchStyle.Percent75);  
  102.             this.cBoxHatchStyle.Items.Add(HatchStyle.Percent80);  
  103.             this.cBoxHatchStyle.Items.Add(HatchStyle.Percent90);  
  104.             this.cBoxHatchStyle.Items.Add(HatchStyle.Plaid);  
  105.             this.cBoxHatchStyle.Items.Add(HatchStyle.Shingle);  
  106.             this.cBoxHatchStyle.Items.Add(HatchStyle.SmallCheckerBoard);  
  107.             this.cBoxHatchStyle.Items.Add(HatchStyle.SmallConfetti);  
  108.             this.cBoxHatchStyle.Items.Add(HatchStyle.SmallGrid);  
  109.             this.cBoxHatchStyle.Items.Add(HatchStyle.SolidDiamond);  
  110.             this.cBoxHatchStyle.Items.Add(HatchStyle.Sphere);  
  111.             this.cBoxHatchStyle.Items.Add(HatchStyle.Trellis);  
  112.             this.cBoxHatchStyle.Items.Add(HatchStyle.Vertical);  
  113.             this.cBoxHatchStyle.Items.Add(HatchStyle.Wave);  
  114.             this.cBoxHatchStyle.Items.Add(HatchStyle.Weave);  
  115.             this.cBoxHatchStyle.Items.Add(HatchStyle.WideDownwardDiagonal);  
  116.             this.cBoxHatchStyle.Items.Add(HatchStyle.WideUpwardDiagonal);  
  117.             this.cBoxHatchStyle.Items.Add(HatchStyle.ZigZag);  
  118.             this.cBoxHatchStyle.SelectedIndex=1;  
  119.         }  
  120.           
  121.         [STAThread]  
  122.         public static void Main(string[] args)  
  123.         {  
  124.             Application.Run(new MainForm());  
  125.         }  
  126.   
  127.         void SelectColorClick(object sender, System.EventArgs e)  
  128.         {  
  129.             ColorDialog clrDlg = new ColorDialog();  
  130.             clrDlg.Color = this.SelectColor.BackColor;  
  131.             if (clrDlg.ShowDialog() == DialogResult.OK)  
  132.             {  
  133.                 this.SelectColor.BackColor = clrDlg.Color;  
  134.                 this.Invalidate();  
  135.             }  
  136.             clrDlg.Dispose();  
  137.         }  
  138.           
  139.         void SelectBKColorClick(object sender, System.EventArgs e)  
  140.         {  
  141.             ColorDialog clrDlg = new ColorDialog();  
  142.             clrDlg.Color = this.SelectBKColor.BackColor;  
  143.             if (clrDlg.ShowDialog() == DialogResult.OK)  
  144.             {  
  145.                 this.SelectBKColor.BackColor = clrDlg.Color;  
  146.                 this.Invalidate();  
  147.             }  
  148.             clrDlg.Dispose();  
  149.         }  
  150.   
  151.         void Form_Paint(object sender, System.Windows.Forms.PaintEventArgs e)  
  152.         {  
  153.             Graphics g = e.Graphics;  
  154.             // 物件轉換成其他類別  
  155.             HatchStyle selectedHatchStyle = (HatchStyle)Convert.ChangeType(  
  156.                     this.cBoxHatchStyle.SelectedItem,typeof(HatchStyle));  
  157.             // Create a hatch brush with selected hatch style and colors  
  158.             HatchBrush brush = new HatchBrush(selectedHatchStyle,  
  159.                     this.SelectColor.BackColor, this.SelectBKColor.BackColor);  
  160.             // Fill rectangle  
  161.             g.FillRectangle(brush, this.ClientRectangle);  
  162.             // Dispose of objects  
  163.             brush.Dispose();  
  164.         }  
  165.   
  166.         void CBoxHatchStyleSelectedIndexChanged(object sender, System.EventArgs e)  
  167.         {  
  168.             this.Invalidate();  
  169.         }  
  170.     }  
  171. }  

/*
 * Created by 記事本 tongue-out
 * User: Lee, Dong-Liang  dllee@edirect168.com
 * Date: 2005/06/18
 */
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() 的方法
			// 得到字串作列表,而加入的物件之後可以直接取出使用。
			this.cBoxHatchStyle.Items.Add(HatchStyle.BackwardDiagonal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Cross); // 與 LargeGrid 相同?!
			this.cBoxHatchStyle.Items.Add(HatchStyle.DarkDownwardDiagonal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.DarkHorizontal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.DarkUpwardDiagonal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.DarkVertical);
			this.cBoxHatchStyle.Items.Add(HatchStyle.DashedDownwardDiagonal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.DashedHorizontal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.DashedUpwardDiagonal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.DashedVertical);
			this.cBoxHatchStyle.Items.Add(HatchStyle.DiagonalBrick);
			this.cBoxHatchStyle.Items.Add(HatchStyle.DiagonalCross);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Divot);
			this.cBoxHatchStyle.Items.Add(HatchStyle.DottedDiamond);
			this.cBoxHatchStyle.Items.Add(HatchStyle.DottedGrid);
			this.cBoxHatchStyle.Items.Add(HatchStyle.ForwardDiagonal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Horizontal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.HorizontalBrick);
			this.cBoxHatchStyle.Items.Add(HatchStyle.LargeCheckerBoard);
			this.cBoxHatchStyle.Items.Add(HatchStyle.LargeConfetti);
			this.cBoxHatchStyle.Items.Add(HatchStyle.LargeGrid);
			this.cBoxHatchStyle.Items.Add(HatchStyle.LightDownwardDiagonal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.LightHorizontal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.LightUpwardDiagonal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.LightVertical);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Max);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Min);
			this.cBoxHatchStyle.Items.Add(HatchStyle.NarrowHorizontal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.NarrowVertical);
			this.cBoxHatchStyle.Items.Add(HatchStyle.OutlinedDiamond);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Percent05);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Percent10);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Percent20);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Percent25);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Percent30);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Percent40);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Percent50);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Percent60);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Percent70);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Percent75);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Percent80);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Percent90);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Plaid);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Shingle);
			this.cBoxHatchStyle.Items.Add(HatchStyle.SmallCheckerBoard);
			this.cBoxHatchStyle.Items.Add(HatchStyle.SmallConfetti);
			this.cBoxHatchStyle.Items.Add(HatchStyle.SmallGrid);
			this.cBoxHatchStyle.Items.Add(HatchStyle.SolidDiamond);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Sphere);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Trellis);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Vertical);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Wave);
			this.cBoxHatchStyle.Items.Add(HatchStyle.Weave);
			this.cBoxHatchStyle.Items.Add(HatchStyle.WideDownwardDiagonal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.WideUpwardDiagonal);
			this.cBoxHatchStyle.Items.Add(HatchStyle.ZigZag);
			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();
		}
	}
}

附件:ItemOfComboBox.cs