asp.net C#自定义控件一下拉颜色框方法

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); }

        }

    }

}

时间: 2024-09-09 15:10:25

asp.net C#自定义控件一下拉颜色框方法的相关文章

asp自定义下拉颜色框

问题描述 类似于这样的c#可以实现网页版的不会只能实现下面的求帮忙实现上面的用asp.net实现 解决方案 解决方案二:网页上能实现第二个实现不了第一个求解决方案三:是颜色名称前面有色块吗?看一下解决方案四:使用DorpDownExtender解决方案五:下拉框中显示图片和背景颜色//设置下拉列表框privatevoidcbox_DisplayPictures_DrawItem(objectsender,DrawItemEventArgse){if(G_ImageList!=null)//判断I

asp.net DataGridTree 下拉树 实现方法

asp教程.net datagridtree 下拉树 实现方法 下拉树实现原理 输出json到客户端 客户端实现动态加载 中间不会和服务端交互 数据量支持上 经测试 几千 还是很快的 本下拉树控件是用c#+js树实现 --------------------------------------------------------------------------------   2.c# 计算器 计算字符串数学表达式源码   计算数学表达式原理 采用c#实现 很实用 //a.建立两个栈:第一个

asp.net 如何让下拉菜单控件里的数据和文本框控件里的数据连接在一起

问题描述 asp.net 如何让下拉菜单控件里的数据和文本框控件里的数据连接在一起 string profilename=Workcella+bay+family+model; workcella与bay是下拉菜单控件的 family与model是文本框控件的 VS2010贴出的代码是 错误 1 运算符"+"无法应用于"System.Web.UI.WebControls.DropDownList"和"System.Web.UI.WebControls.Dr

ASP.NET实现级联下拉框效果实例讲解_实用技巧

用ASP.NET控件实现部门和员工的联动,参考过程如下效果图:  Default.aspx代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/199

asp.net from winform中textbox下拉提示框

asp教程.net from winform中textbox下拉提示框 private void form1_load(object sender, eventargs e) {     autocompletestringcollection strings = new autocompletestringcollection();     strings.add("a1");//不区分大小写     strings.add("a2");     strings.

asp.net使用DataGridTree实现下拉树的方法_实用技巧

本文实例讲述了asp.net使用DataGridTree实现下拉树的方法.分享给大家供大家参考.具体实现方法如下: 下拉树实现原理:输出json到客户端,客户端实现动态加载,中间不会和服务端交互.数据量支持上经测试几千还是很快的.本下拉树控件是用c#+js树实现. 2.c# 计算器 计算字符串数学表达式源码 计算数学表达式原理 采用c#实现 很实用 //a.建立两个栈:第一个位操作数栈,第二个操作符符栈!(将栈定义为string类型) //b.对数字来说是无条件压入数字栈中. //c.而对符号来

ASP.NET:多级下拉菜单的级连显示问题

asp.net|菜单|问题|下拉|显示 多级下拉菜单的级连显示问题. 在这里,我为了实现公司-部门级连显示问题,我编写了leader_add_competence_dialogquery.jsp ,web.xml,SelectCropDepartServlet.java 类. 其中注意的是leader_add_competence_dialogquery中的.jspChange_Select() javascript函数中的/selectCropDepart是在web.xml中定义的servle

js使用DOM设置单选按钮、复选框及下拉菜单的方法

 这篇文章主要介绍了js使用DOM设置单选按钮.复选框及下拉菜单的方法,较为详细的分析了单选按钮.复选框及下拉菜单的具体用法及实现技巧,非常具有实用价值,需要的朋友可以参考下     本文实例讲述了js使用DOM设置单选按钮.复选框及下拉菜单的方法.分享给大家供大家参考.具体实现方法如下: 1.设置单选按钮 单选按钮在表单中即<input type="radio" />它是一组供用户选择的对象,但每次只能选一个.每一个都有checked属性,当一项选择为ture时,其它的都

ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性

原文:ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性                                         深入讲解控件的属性持久化(一) 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 ASP.NET自定义控件组件开发 第一章 第三篇 ASP.NET自定义控件组件开发 第二章 继承WebControl的自定义控件 ASP.NET自定义控件组件开发 第三章 为控件添加事件 前