摘要:轉貼 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 可解出。
- /*
- * Created by 記事本
- * 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();
- }
- }
- }
/*
* Created by 記事本
* 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