asp.net C#自訂控制項一下拉色塊方法

來源:互聯網
上載者:User

asp教程.net C#自訂控制項一下拉色塊方法

通過繼承ComboBox可以設計出類似C#控制項屬性欄中的顏色下拉選擇框。

添加組件命名為myColorComboBox.cs

 

第一步:繼承ComboBox,public partial class myColorComboBox : ComboBox

 

第二步:構造下拉顏色選擇框

private void InitItems()

        {

            this.DrawMode = DrawMode.OwnerDrawFixed;//手動繪製所有元素

            this.DropDownStyle = ComboBoxStyle.DropDownList;//下拉框樣式設定為不能編輯

            this.Items.Clear();//清空原有項

            Array allColors = Enum.GetValues(typeof(KnownColor));//擷取系統色彩名存入列表

            foreach (KnownColor var in allColors)

            {

                this.Items.Add(var.ToString()); //載入該選項框的子項

            }

            this.SelectedIndex = 0;

        }

在兩個建構函式中加入InitItems()

 

第三步:重寫OnDrawItem方法

protected override void OnDrawItem(DrawItemEventArgs e)

        {

            if (e.Index >= 0)//判斷是否需要重繪

            {

                string colorName = this.Items[e.Index].ToString();//擷取顏色名

                SolidBrush brush = new SolidBrush(Color.FromName(colorName));//定義畫刷

                Font font = new Font("宋體", 9);//定義字型

                Rectangle rect = e.Bounds;

                rect.Inflate(-2, -2);

 

                Rectangle rectColor = new Rectangle(rect.Location, new Size(20, rect.Height));

                e.Graphics.FillRectangle(brush, rectColor);//填充顏色

                e.Graphics.DrawRectangle(Pens.Black, rectColor);//繪製邊框

                e.Graphics.DrawString(colorName, font, Brushes.Black, (rect.X + 22), rect.Y);//繪製文字

            }

        }

 

第四步:增加控制項屬性

 

/// <summary>

        /// 選擇的顏色名稱

        /// </summary>

        public string SelectColorName

        {

            get { return this.Text; }

        }

 

        /// <summary>

        /// 選擇的顏色

        /// </summary>

        public Color SelectColor

        {

            get { return Color.FromName(this.Text); }

        }

 

用法:

直接從控制項欄中找到自訂控制項myColorComboBox,拖過去自動命名為myColorCombBox1,可以通過myColorCombBox1.SelectColor擷取顏色,類型為Color,通過myColorCombBox1.SelectColorName擷取使用者選定的顏色名。

 

下面貼出全部代碼:

//控制項名:myColorComboBox

//作者:劉典武

//時間:2011-06-01

 

using System;

using System.ComponentModel;

using System.Collections.Generic;

using System.Diagnostics;

using System.Text;

using System.Windows.Forms;

using System.Drawing;

 

namespace myControl

{

    public partial class myColorComboBox : ComboBox

    {

        public myColorComboBox()

        {

            InitializeComponent();

            InitItems();

        }

 

        public myColorComboBox(IContainer container)

        {

            container.Add(this);

 

            InitializeComponent();

            InitItems();

        }

 

        private void InitItems()

        {

            this.DrawMode = DrawMode.OwnerDrawFixed;//手動繪製所有元素

            this.DropDownStyle = ComboBoxStyle.DropDownList;//下拉框樣式設定為不能編輯

            this.Items.Clear();//清空原有項

            Array allColors = Enum.GetValues(typeof(KnownColor));//擷取系統色彩名存入列表

            foreach (KnownColor var in allColors)

            {

                this.Items.Add(var.ToString()); //載入該選項框的子項

            }

            this.SelectedIndex = 0;

        }

 

        protected override void OnDrawItem(DrawItemEventArgs e)

        {

            if (e.Index >= 0)//判斷是否需要重繪

            {

                string colorName = this.Items[e.Index].ToString();//擷取顏色名

                SolidBrush brush = new SolidBrush(Color.FromName(colorName));//定義畫刷

                Font font = new Font("宋體", 9);//定義字型

                Rectangle rect = e.Bounds;

                rect.Inflate(-2, -2);

 

                Rectangle rectColor = new Rectangle(rect.Location, new Size(20, rect.Height));

                e.Graphics.FillRectangle(brush, rectColor);//填充顏色

                e.Graphics.DrawRectangle(Pens.Black, rectColor);//繪製邊框

                e.Graphics.DrawString(colorName, font, Brushes.Black, (rect.X + 22), rect.Y);//繪製文字

            }

        }

 

        /// <summary>

        /// 選擇的顏色名稱

        /// </summary>

        public string SelectColorName

        {

            get { return this.Text; }

        }

 

        /// <summary>

        /// 選擇的顏色

        /// </summary>

        public Color SelectColor

        {

            get { return Color.FromName(this.Text); }

        }

    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.