精华内容
下载资源
问答
  • 2018-01-17 13:21:07

    复选列表框控件(checkedListBox)是列表框控件(listBox)的派生,相对于列表框控件其为每个选项增加了复选框功能。


    常用属性:

    Items属性:复选列表框中所有项目的集合。

    SelectedIndex属性:选中的集合中项目的索引(Items集合中的项目索引从0开始)。

    SelectedItem属性:选中的项,跟listBox控件一样,该属性是object类型。、

    SelectedValue属性:选中的项是否勾选。其值是一个枚举类型,有几个参选值:CheckState.Checked/CheckState.UnChecked/Indeterminate。

    SelectedItems属性:选中的项的集合。

    MultiColumn属性:Bool值控制是否支持多列。


    常用事件:

    ItemCheck事件:集合中项的勾选状态更改时发生。参数e有几个属性:

    e.NewValue:与SelectedValue类似,表示更改后的值。

    e.Index:与SelectedIndex类似,表示选定的项的索引。





    更多相关内容
  • C# 实现WinForm带复选下拉列表DLL控件,ComboBox WinForm应用,是一个DLL的源码,实现窗口中带复选下拉列表,源码编译后生成UCComboBox.dll,你在项目中添加引用就可使用了。 运行环境:Visual Studio2010
  • c#winform下拉多选自定义控件(带全选),可以自己定义选择项的下拉框
  • 非常值得收藏,下载的源码资源: 1. 代码源自网络,并添加部分示例代码; 2. 自定义Combobx下拉框,支持基础控件下拉,也支持自定义控件下拉,扩展十分方便;
  • 自定义的WinForm窗体的下拉框中可以多选
  • ListView 中实现可编辑文本或双击出现下拉列表框 重写listview空间,在控件中添加下拉框和文本框
  • 1、默认下拉菜单控件可编辑选中的内容 2、设置不可编辑后,控件会有一个灰色不可编辑状态的背景颜色 3、设置不可编辑后,下拉控件动态添加的值,会显示空白的情况 此源码就是解决以上问题 1、简单几行代码,即可设置...
  • 使用 SunnyUI扩展,SunnyUI.Net 是基于.Net Framework 4.0+、.Net Core3.1、.Net 5 框架的 C# WinForm 开源控件库、工具类库、扩展类库、多页面开发框架。
  • 工作中用到的 Winform 属性编辑器 propertyGrid示例 下拉列表框
  • 继承自C# Winform中ComboBox的下拉多选控件,操作简单,效果非常好 1、多选。 2、可绑定List,DataTable,Dictionary类型的数据源。 3、在下拉列表中可显示自定义的多列数据。 4、可在下拉列表中通过输入关键字,...
  • c# winform自定义下拉列表(源码)

    热门讨论 2015-01-10 11:27:27
    控件有个DropDownControl属性,你可以指定给这个控件下拉按钮时弹出datagridview,richTextBox等等系统控件甚至是自己定义的UserControl控件。。。下载后自己看吧,有源码有示例有真像
  • winform下拉

    2015-09-15 16:00:43
    下拉
  • winform 下拉框(自定义下拉框控件

    千次阅读 2020-06-08 19:51:56
    项目中遇到需要下拉框列表内容为线类型或点类型图形的需求,想到可以使用手绘线条和图形的方式来实现下拉列表内容自定义,记录下来供大家参考学习之用。 在项目中添加一个组件 添加完之后会显示如下设计界面 这里...

    项目中遇到需要下拉框列表内容为线类型或点类型图形的需求,想到可以使用手绘线条和图形的方式来实现下拉列表内容自定义,记录下来供大家参考学习之用。

    在项目中添加一个组件

    在这里插入图片描述

    在这里插入图片描述
    添加完之后会显示如下设计界面
    在这里插入图片描述
    这里直接点击切换到代码视图即可

    注意:新建的组件默认继承Component,我们这里是要自定义一个ComboBox下拉框,所以要继承自ComboBox,此类位于using System.Windows.Forms命名空间下
    在这里插入图片描述

    线的类型列表

    在这里插入图片描述

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace CustomComponents
    {
        /// <summary>
        /// 填充线型
        /// </summary>
        public partial class FillLineTypeCbo : ComboBox
        {
            public FillLineTypeCbo()
            {
          		//初始化组件
                InitializeComponent();
            }
    
            public FillLineTypeCbo(IContainer container)
            {
                container.Add(this);
    
                InitializeComponent();
                InitItems();
            }
    
            private void InitItems()
            {
                this.DrawMode = DrawMode.OwnerDrawFixed;//手动绘制所有元素
                this.DropDownStyle = ComboBoxStyle.DropDownList;//下拉框样式设置为不能编辑
                this.Items.Clear();//清空原有项 
            }
    
            public enum LineType
            {
                DoubleHorizontalLine = 0,//双横线
                VerticalLine,//垂直线
                LeftSlash,//左斜线
                RightSlash,//右斜线
                VerticalGridlines,//垂直网格线
                CrossGridlines,//交叉网格线
                SolidBrush,//实心刷
                HollowBrush,//空心刷
            }
            //protected
            protected override void OnDrawItem(DrawItemEventArgs e)
            {
                if (e.Index >= 0)//判断是否需要重绘
                {
                    int typeId = int.Parse(this.Items[e.Index].ToString());//获取选项id
    
                    Font font = new Font("宋体", 9);//定义字体
                    Rectangle rect = e.Bounds;
                    //rect.Inflate(-2, -2);//缩放一定大小
                    rect.Inflate(5, -2);//缩放一定大小
                    Pen pen = null;
                    SolidBrush solidBrush = new SolidBrush(Color.Black);
    
                    float offSet = rect.Height / 2;
                    float x = rect.Width / 10;
                    float y = rect.Top + offSet;//如果设置成e.ItemHeigh*e.Index +offSet,则在选中节点后,由于Item位置固定,因此Item不能显示出来。
                    switch (typeId)
                    {
                        case (int)LineType.DoubleHorizontalLine:
                            pen = new Pen(Color.Black, 1);
                            e.Graphics.DrawLine(pen, new PointF(x, y), new PointF(8 * x, y));//绘制水平线
                            e.Graphics.DrawLine(pen, new PointF(x, y + 5), new PointF(8 * x, y + 5));
                            //e.Graphics.DrawLine(pen, new PointF(x, y+2), new PointF(x, y+2));
                            break;
                        case (int)LineType.VerticalLine:
                            pen = new Pen(Color.Black, 1);
                            //绘制垂直线
                            int xNum = 0;
                            for (int i = 0; i < 10; i++)
                            {
                                e.Graphics.DrawLine(pen, new PointF(x + xNum, y - 4), new PointF(1 * x + xNum, y + 6));
                                xNum += 7;
    
                            }
                            break;
                        case (int)LineType.LeftSlash:
                            pen = new Pen(Color.Black, 1);
                            //绘制左斜线
                            int xNumLeftSlash = 0;
                            for (int i = 0; i < 10; i++)
                            {
                                e.Graphics.DrawLine(pen, new PointF(x + xNumLeftSlash, y - 4), new PointF(x + 5 + xNumLeftSlash, y + 7));
                                xNumLeftSlash += 7;
                            }
                            break;
                        case (int)LineType.RightSlash:
                            pen = new Pen(Color.Black, 1);
                            //绘制右斜线
                            int xNumRightSlash = 0;
                            for (int i = 0; i < 10; i++)
                            {
                                e.Graphics.DrawLine(pen, new PointF(x + xNumRightSlash + 7, y - 4), new PointF(x + xNumRightSlash, y + 7));
                                xNumRightSlash += 7;
                            }
                            break;
                        case (int)LineType.VerticalGridlines:
                            pen = new Pen(Color.Black, 1);
                            //绘制垂直网格线
                            xNum = 0;
                            for (int i = 0; i < 10; i++)//绘制竖线
                            {
                                e.Graphics.DrawLine(pen, new PointF(x + xNum, y - 4), new PointF(1 * x + xNum, y + 6));
                                xNum += 7;
                            }
                            //绘制双横线
                            e.Graphics.DrawLine(pen, new PointF(x - 3, y - 2), new PointF(8 * x, y - 2));
                            e.Graphics.DrawLine(pen, new PointF(x - 3, y + 5), new PointF(8 * x, y + 5));
                            break;
                        case (int)LineType.CrossGridlines:
                            pen = new Pen(Color.Black, 1);
                            //绘制交叉网格线
                            //绘制左斜线
                            xNumLeftSlash = 0;
                            for (int i = 0; i < 10; i++)
                            {
                                e.Graphics.DrawLine(pen, new PointF(x + xNumLeftSlash, y - 4), new PointF(x + 10 + xNumLeftSlash, y + 7));
                                xNumLeftSlash += 7;
                            }
                            //绘制右斜线
                            xNumRightSlash = 0;
                            for (int i = 0; i < 10; i++)
                            {
                                e.Graphics.DrawLine(pen, new PointF(x + xNumRightSlash + 10, y - 4), new PointF(x + xNumRightSlash, y + 7));
                                xNumRightSlash += 7;
                            }
                            break;
                        case (int)LineType.SolidBrush:
                            pen = new Pen(Color.Black, 10);
                            //绘制实心刷
                            e.Graphics.DrawLine(pen, new PointF(x, y), new PointF(8 * x, y));
                            break;
                        case (int)LineType.HollowBrush:
                            pen = new Pen(Color.Black, 1);
                            //绘制空心刷
                            e.Graphics.DrawLine(pen, new PointF(x, y - 6), new PointF(x, y + 6));//左
                            e.Graphics.DrawLine(pen, new PointF(x, y - 6), new PointF(8 * x, y - 6));//上
                            e.Graphics.DrawLine(pen, new PointF(8 * x, y - 6), new PointF(8 * x, y + 6));//右
                            e.Graphics.DrawLine(pen, new PointF(x, y + 6), new PointF(8 * x, y + 6));//下
                            break;
                        default:
                            pen = new Pen(Color.Black, 1);
                            break;
                    }
                }
            }
        }
    }
    
    

    边框线类型列表

    在这里插入图片描述

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace CustomComponents
    {
        /// <summary>
        /// 线型
        /// </summary>
        public partial class LineShapeCbo : ComboBox
        {
            public LineShapeCbo()
            {
                InitializeComponent();
            }
    
            public LineShapeCbo(IContainer container)
            {
                container.Add(this);
    
                InitializeComponent();
                InitItems();
            }
    
            private void InitItems()
            {
                this.DrawMode = DrawMode.OwnerDrawFixed;//手动绘制所有元素
                this.DropDownStyle = ComboBoxStyle.DropDownList;//下拉框样式设置为不能编辑
                this.Items.Clear();//清空原有项 
            }
    
            public enum LineType
            {
                SolidLine = 0,//实线
                LongDisconnection,//长间断线
                ShortEndLine,//短间端线
                LongAndShort,//长短相间
                ShortToLong,//长短短相间
                EmptyBrushes//空画笔
            }
    
            protected override void OnDrawItem(DrawItemEventArgs e)
            {
                if (e.Index >= 0)//判断是否需要重绘
                {
                    int typeId = int.Parse(this.Items[e.Index].ToString());//获取选项id
    
                    Font font = new Font("宋体", 9);//定义字体
                    Rectangle rect = e.Bounds;
                    //rect.Inflate(-2, -2);//缩放一定大小
                    rect.Inflate(5, -2);//缩放一定大小
                    Pen pen = null;
                    SolidBrush solidBrush = new SolidBrush(Color.Black);
    
                    float offSet = rect.Height / 2;
                    float x = rect.Width / 10;
                    float y = rect.Top + offSet;//如果设置成e.ItemHeigh*e.Index +offSet,则在选中节点后,由于Item位置固定,因此Item不能显示出来。
                    switch (typeId)
                    {
                        case (int)LineType.SolidLine:
                            pen = new Pen(Color.Black, 1);
                            e.Graphics.DrawLine(pen, new PointF(x, y), new PointF(8 * x, y));//绘制实线
                            //e.Graphics.DrawLine(pen, new PointF(x, y+2), new PointF(x, y+2));
                            break;
                        case (int)LineType.LongDisconnection:
                            pen = new Pen(Color.Black, 1);
                            //绘制长间断线
                            int xNum = 0;
                            for (int i = 0; i < 3; i++)
                            {
                                e.Graphics.DrawLine(pen, new PointF(x + xNum, y), new PointF(15+x + xNum, y));
                                xNum += 20;
                            }
                            e.Graphics.DrawLine(pen, new PointF(x + xNum, y), new PointF(10 + x + xNum, y));
                            break;
                        case (int)LineType.ShortEndLine:
                            pen = new Pen(Color.Black, 1);
                            //绘制短间端线
                            xNum = 0;
                            for (int i = 0; i < 8; i++)
                            {
                                e.Graphics.DrawLine(pen, new PointF(x + xNum, y), new PointF(6 + x + xNum, y));
                                xNum += 9;
                            }
                            break;
                        case (int)LineType.LongAndShort:
                            pen = new Pen(Color.Black, 1);
                            //绘制长短相间
                                e.Graphics.DrawLine(pen, new PointF(x, y), new PointF(x+10, y));
                                e.Graphics.DrawLine(pen, new PointF(x+15, y), new PointF(x+20, y));
                                e.Graphics.DrawLine(pen, new PointF(x+25, y), new PointF(x+35, y));
                                e.Graphics.DrawLine(pen, new PointF(x+40, y), new PointF(x+45, y));
                                e.Graphics.DrawLine(pen, new PointF(x+50, y), new PointF(x+60, y));
                                e.Graphics.DrawLine(pen, new PointF(x+65, y), new PointF(x+70, y));
                            break;
                        case (int)LineType.ShortToLong:
                            pen = new Pen(Color.Black, 1);
                            //绘制长短短相间
                            e.Graphics.DrawLine(pen, new PointF(x, y), new PointF(x + 10, y));
                            e.Graphics.DrawLine(pen, new PointF(x+15, y), new PointF(x + 18, y));
                            e.Graphics.DrawLine(pen, new PointF(x+23, y), new PointF(x + 26, y));
                            e.Graphics.DrawLine(pen, new PointF(x+31, y), new PointF(x + 41, y));
                            e.Graphics.DrawLine(pen, new PointF(x + 46, y), new PointF(x + 49, y));
                            e.Graphics.DrawLine(pen, new PointF(x + 54, y), new PointF(x + 57, y));
                            e.Graphics.DrawLine(pen, new PointF(x + 62, y), new PointF(x + 70, y));
                            break;
                        case (int)LineType.EmptyBrushes:
                            pen = new Pen(Color.Black, 1);
                            //绘制空画笔
                            break;
                        //case (int)LineType.LineWithPoints:
                        default:
                            pen = new Pen(Color.Black, 1);
                            break;
                    }
                    //if (e.Index < 9)
                    //{
                    //    e.Graphics.DrawLine(pen, new PointF(x, y), new PointF(8 * x, y));//绘制线
                    //    //Rectangle rectColor = new Rectangle(rect.Location, new Size(9 * (int)x, rect.Height));
                    //    //e.Graphics.DrawRectangle(Pens.Black, rectColor);//绘制边框
                    //}
                }
            }
        }
    }
    
    

    点类型列表

    在这里插入图片描述

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace CustomComponents
    {
        /// <summary>
        /// 点类型
        /// </summary>
        public partial class PointTypeCbo : ComboBox
        {
            public PointTypeCbo()
            {
                InitializeComponent();
            }
    
            public PointTypeCbo(IContainer container)
            {
                container.Add(this);
    
                InitializeComponent();
                InitItems();
            }
            private void InitItems()
            {
                this.DrawMode = DrawMode.OwnerDrawFixed;//手动绘制所有元素
                this.DropDownStyle = ComboBoxStyle.DropDownList;//下拉框样式设置为不能编辑
                this.Items.Clear();//清空原有项 
            }
    
            protected override void OnDrawItem(DrawItemEventArgs e)
            {
                if (e.Index >= 0)//判断是否需要重绘
                {
                    int typeId = int.Parse(this.Items[e.Index].ToString());//获取选项id
    
                    Font font = new Font("宋体", 9);//定义字体
                    Rectangle rect = e.Bounds;
                    //rect.Inflate(-2, -2);//缩放一定大小
                    rect.Inflate(5, -2);//缩放一定大小
                    Pen pen = null;
                    SolidBrush solidBrush = new SolidBrush(Color.Black);
    
                    float offSet = rect.Height / 2;
                    float x = rect.Width / 10;
                    float y = rect.Top + offSet;//如果设置成e.ItemHeigh*e.Index +offSet,则在选中节点后,由于Item位置固定,因此Item不能显示出来。
                    switch (typeId)
                    {
                        case 0:
                            pen = new Pen(Color.Red, 1);//设置边框
                            //SolidBrush sb0 = new SolidBrush(Color.Black);//设置填充颜色
                            //绘制
                            //e.Graphics.DrawLine(pen, new PointF(x, y), new PointF(8 * x, y));
                            //Graphics g = CreateGraphics();
                            //Brush myBrush = new SolidBrush(Color.Green);
                            //string str = "just for fun";
                            //Font myFont = new Font("宋体", 9, FontStyle.Bold);
                            //e.Graphics.DrawString(str, myFont, myBrush, 60, 20);          
                            //e.Graphics.FillEllipse(sb0, 10 ,10 , 10, 10);
                            e.Graphics.DrawEllipse(pen, 9, 5, 10, 10);
                            break;
                        case 1:
                            pen = new Pen(Color.Red, 1);
                            //绘制正方形
                            e.Graphics.DrawLine(pen, new PointF(x, y - 5), new PointF(x, y + 5));//左
                            e.Graphics.DrawLine(pen, new PointF(x, y - 5), new PointF(x + 10, y - 5));//上
                            e.Graphics.DrawLine(pen, new PointF(x + 10, y - 5), new PointF(x + 10, y + 5));//右
                            e.Graphics.DrawLine(pen, new PointF(x, y + 5), new PointF(x + 10, y + 5));//下
    
                            //Rectangle rectColor = new Rectangle(rect.Location, new Size(10, 10));
                            //e.Graphics.DrawRectangle(Pens.Black, rectColor);//绘制边框
    
                            break;
                        case 2:
                            pen = new Pen(Color.Red, 1);
                            //绘制十字形
                            e.Graphics.DrawLine(pen, new PointF(x, y), new PointF(x + 10, y));//横
                            e.Graphics.DrawLine(pen, new PointF(x + 5, y - 5), new PointF(x + 5, y + 5));//竖
                            break;
                        case 3:
                            pen = new Pen(Color.Red, 1);
                            //绘制三角形
                            e.Graphics.DrawLine(pen, new PointF(x, y + 5), new PointF(x + 5, y - 5));//左
                            e.Graphics.DrawLine(pen, new PointF(x + 5, y - 5), new PointF(x + 10, y + 5));//右
                            e.Graphics.DrawLine(pen, new PointF(x, y + 5), new PointF(x + 10, y + 5));//下
                            break;
                        case 4:
                            pen = new Pen(Color.Red, 1);
                            //绘制右(三角)红旗
                            e.Graphics.DrawLine(pen, new PointF(x + 5, y + 5), new PointF(x + 5, y - 5));//竖杆
                            e.Graphics.DrawLine(pen, new PointF(x + 5, y - 5), new PointF(x + 10, y));//红旗右边
                            e.Graphics.DrawLine(pen, new PointF(x + 5, y), new PointF(x + 10, y));//红旗下边
                            break;
                        case 5:
                            pen = new Pen(Color.Red, 1);
                            //绘制左(三角)红旗 
                            e.Graphics.DrawLine(pen, new PointF(x + 5, y + 5), new PointF(x + 5, y - 5));//竖杆
                            e.Graphics.DrawLine(pen, new PointF(x, y), new PointF(x + 5, y - 5));//红旗左边
                            e.Graphics.DrawLine(pen, new PointF(x, y), new PointF(x + 5, y));//红旗下边
                            break;
                        case 6:
                            pen = new Pen(Color.Red, 1);
                            //绘制右方旗
                            e.Graphics.DrawLine(pen, new PointF(x + 5, y + 5), new PointF(x + 5, y - 5));//竖杆
                            e.Graphics.DrawLine(pen, new PointF(x + 5, y - 5), new PointF(x + 10, y - 5));//右上边
                            e.Graphics.DrawLine(pen, new PointF(x + 10, y - 5), new PointF(x + 10, y));//右右边
                            e.Graphics.DrawLine(pen, new PointF(x + 5, y), new PointF(x + 10, y));//右下边
                            break;
                        case 7:
                            pen = new Pen(Color.Red, 1);
                            //绘制大圆包小圆
                            //e.Graphics.DrawEllipse(pen, 20, 115, 10, 10);//空心大圆
                            //e.Graphics.DrawEllipse(pen, 13, 118, 4, 4);//空心小圆 
                            e.Graphics.DrawEllipse(pen, x, y - 5, 10, 10);//空心大圆
                            e.Graphics.DrawEllipse(pen, x + 3, y - 5 + 3, 4, 4);//空心小圆
                            //Rectangle rectColor = new Rectangle(rect.Location, new Size(9 * (int)x, rect.Height));
                            //e.Graphics.DrawRectangle(Pens.Black, rectColor);//绘制边框
                            break;
                        case 8:
                            pen = new Pen(Color.Red, 1);
                            //绘制大圆加十字
                            e.Graphics.DrawEllipse(pen, x, y - 5, 10, 10);//空心大圆
                            //绘制十字形
                            e.Graphics.DrawLine(pen, new PointF(x, y), new PointF(x + 10, y));//横
                            e.Graphics.DrawLine(pen, new PointF(x + 5, y - 5), new PointF(x + 5, y + 5));//竖
                            break;
                        case 9:
                            pen = new Pen(Color.Red, 1);
                            //绘制实心圆
                            SolidBrush sb9 = new SolidBrush(Color.Green);//设置填充颜色
                            e.Graphics.DrawEllipse(pen, x, y - 5, 10, 10);//实心圆边框
                            e.Graphics.FillEllipse(sb9, x, y - 5, 10, 10);//实心圆填充
                            break;
                        case 10:
                            pen = new Pen(Color.Red, 1);
                            //绘制实心正方形
                            //绘制正方形
                            e.Graphics.DrawRectangle(pen, x, y - 5, 10, 10);
                            SolidBrush sb10 = new SolidBrush(Color.Green);//设置填充颜色
                            e.Graphics.FillRectangle(sb10, x, y - 5, 10, 10);
                            break;
                        case 11:
                            pen = new Pen(Color.Red, 1);
                            //绘制
                            PointF p1 = new PointF(x + 5, y - 5);
                            PointF p2 = new PointF(x, y + 5);
                            PointF p3 = new PointF(x + 10, y + 5);
                            PointF[] points = new PointF[3];
                            Brush myBrush = new SolidBrush(Color.Green);
                            points[0] = p1;
                            points[1] = p2;
                            points[2] = p3;
                            e.Graphics.FillPolygon(myBrush, points, FillMode.Alternate);
                            e.Graphics.DrawPolygon(pen, points);
                            break;
                        //case (int)LineType.LineWithPoints:
                        default:
                            pen = new Pen(Color.Black, 1);
                            break;
                    }
                    if (e.Index < 12)
                    {
                        //e.Graphics.DrawLine(pen, new PointF(x, y), new PointF(8 * x, y));//绘制线
                        //设置后面的字符串
                        Rectangle Rect = new Rectangle(rect.Location, new Size(5 * (int)x, rect.Height));
    
                        //文本内容显示区域
                        Rectangle textRect =
                                new Rectangle(Rect.Right + 2, Rect.Y, e.Bounds.Width - Rect.Width, e.Bounds.Height - 2);
    
                        //文本格式垂直居中
                        StringFormat strFormat = new StringFormat();
                        strFormat.LineAlignment = StringAlignment.Center;
                        e.Graphics.DrawString((e.Index + 1).ToString(), new Font("微软雅黑", 12), Brushes.Black, textRect, strFormat);
                    }
                }
            }
        }
    }
    
    

    带图片的列表

    在这里插入图片描述

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace ShowState
    {
        /// <summary>
        /// 下拉框  下拉列表 图片显示
        /// </summary>
        public partial class ComboBoxImg : ComboBox
        {
            public ComboBoxImg()
            {
                InitializeComponent();
                DrawMode = DrawMode.OwnerDrawFixed;
                DropDownStyle = ComboBoxStyle.DropDownList;
                ItemHeight = 50;
                Width = 40;
            }
    
            public ComboBoxImg(IContainer container)
            {
                container.Add(this);
                InitializeComponent();
    
                DrawMode = DrawMode.OwnerDrawFixed;
                DropDownStyle = ComboBoxStyle.DropDownList;
                ItemHeight = 200;
                Width = 40;
            }
    
            protected override void OnDrawItem(DrawItemEventArgs e)
            {
                if (Items.Count == 0 || e.Index == -1)
                    return;
                if ((e.State & DrawItemState.Selected) != 0)
                {
                    //渐变画刷
                    LinearGradientBrush brush = new LinearGradientBrush(e.Bounds, Color.FromArgb(255, 251, 237),
                                                     Color.FromArgb(255, 236, 181), LinearGradientMode.Vertical);
                    //填充区域
                    Rectangle borderRect = new Rectangle(3, e.Bounds.Y, e.Bounds.Width - 5, e.Bounds.Height - 2);
    
                    e.Graphics.FillRectangle(brush, borderRect);
    
                    //画边框
                    Pen pen = new Pen(Color.FromArgb(229, 195, 101));
                    e.Graphics.DrawRectangle(pen, borderRect);
                }
                else
                {
                    SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255));
                    e.Graphics.FillRectangle(brush, e.Bounds);
                }
    
                //获得项图片,绘制图片
                ItemEx item = (ItemEx)Items[e.Index];
                Image img = item.Image;
    
                //图片绘制的区域
                Rectangle imgRect = new Rectangle(6, e.Bounds.Y + 3, 45, 45);
                e.Graphics.DrawImage(img, imgRect);
    
                //文本内容显示区域
                Rectangle textRect =
                        new Rectangle(imgRect.Right + 2, imgRect.Y, e.Bounds.Width - imgRect.Width, e.Bounds.Height - 2);
    
                //获得项文本内容,绘制文本
                String itemText = Items[e.Index].ToString();
    
                //文本格式垂直居中
                StringFormat strFormat = new StringFormat();
                strFormat.LineAlignment = StringAlignment.Center;
                e.Graphics.DrawString(itemText, new Font("微软雅黑", 12), Brushes.Black, textRect, strFormat);
                base.OnDrawItem(e);
            }
    
        }
    
        /// <summary>
        /// 下拉框  下拉列表 图片显示
        /// </summary>
        public class ItemEx
        {
            /// <summary>
            /// 图片显示
            /// </summary>
            /// <param name="text">文本</param>
            /// <param name="img">图片(图片路径)</param>
            public ItemEx(string text, Image img)
            {
                Text = text;
                Image = img;
            }
    
            public string Text { get; set; }
            public Image Image { get; set; }
    
            public override string ToString()
            {
                return Text;
            }
        }
    }
    
    

    组件编写完之后,进入工具箱,就能看到当前项目中我们自定义的组件了,使用方法跟普通控件方法完全相同,直接拖到窗体即可

    在这里插入图片描述

    展开全文
  • C#树形下拉列表控件

    2020-03-04 01:00:32
    C#树形下拉列表控件 非常好用的 完全源代码
  • C#自定义控件下拉列表框

    万次阅读 2019-02-12 13:48:56
    设置 DropDownStyle 为DropDownList 将DrawMode 改为OwnerDrawFixed ... 样式就变为下面这样,但是只能在列表中选择,而不能手动输入 public class ComBox : ComboBox { public ComBox() { ...
    1. 设置 DropDownStyle 为 DropDownList


       
    2. 将 DrawMode 改为 OwnerDrawFixed


       
    3. 添加 DrawItem 事件
    4.  样式就变为下面这样,但是只能在列表中选择,而不能手动输入

        public class ComBox : ComboBox
        {
            public ComBox()
            {
                this.DropDownStyle = ComboBoxStyle.DropDownList;
                this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
                this.DrawItem += new DrawItemEventHandler(ComBox_DrawItem);
            }
    
            void ComBox_DrawItem(object sender, DrawItemEventArgs e)
            {
                ComboBox cb = sender as ComboBox;
                if (e.Index < 0)
                {
                    return;
                }
                e.DrawBackground();
                e.DrawFocusRectangle();
                e.Graphics.DrawString(cb.GetItemText(cb.Items[e.Index]).ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);
            }
    
        }
    
    List<object> listCmbInfo = new List<object>();
    listCmbInfo.Add(new { ID = 100, Name = "100" });
    listCmbInfo.Add(new { ID = 120, Name = "120" });
    listCmbInfo.Add(new { ID = 150, Name = "150" });
    this.cmb1.DisplayMember = "Name";
    this.cmb1.ValueMember = "ID";
    this.cmb1.DataSource = listCmbInfo;
    
    //这样显示的是 Name 属性,取值时可以取 ID 属性 (this.cmb1.SelectedValue)
    

     

     

    展开全文
  • 这一节给大家演示一个具有多选功能的下拉列表框,其实就是一个文本框和checkboxlist组合实现的用户控件,换个角度来实现自定义控件。 先来看下效果图吧,我会分几个步骤来具体讲解这个文本框的实现。    1.界面...
          这一节给大家演示一个具有多选功能的下拉列表框,其实就是一个文本框和checkboxlist组合实现的用户控件,换个角度来实现自定义控件。

    先来看下效果图吧,我会分几个步骤来具体讲解这个文本框的实现。

                                            

        1.界面实现

         我们先来看下界面效果怎样实现,然后再讲解具体的功能性实现。

          先创建一个用户控件,当然是继承UserControl类,这个界面需要用到以下控件来实现

         下拉按钮:是一个button控件

         文本框:显示选中的内容,

         多选列表框: checkboxlist

         全选/取消:是两个LABEL控件

          右下角的黑三角:是一个LABEL控件,可实现拉长下拉列表框.

          窗体:点击下拉按钮,实际是弹出一个窗体,下拉列表框中的控件都在这个窗体中显示

        首先看下下拉按钮的实现

       

    /// <summary>
        /// 重写BUTTON
        /// </summary>
        public class ButtonS : Button
        {
            public ButtonS()
            {
                //防止重绘控件出现闪烁
                this.SetStyle(ControlStyles.UserPaint, true);
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            }
            ButtonState state;
            //当按钮被按下
            protected override void OnMouseDown(MouseEventArgs mevent)
            {
                state = ButtonState.Pushed;
                base.OnMouseDown(mevent);
            }
    
            //当按钮被释放
            protected override void OnMouseUp(MouseEventArgs mevent)
            {
                state = ButtonState.Normal;
                base.OnMouseUp(mevent);
            }
    
            protected override void OnPaint(PaintEventArgs pevent)
            {
                base.OnPaint(pevent);
                System.Windows.Forms.ControlPaint.DrawComboButton(pevent.Graphics, 0, 0, this.Width, this.Height, state);
            }
        }


       接下来再看下具有拉动功能的LABEL

      

    /// <summary>
        /// 重写LABEL
        /// </summary>
        public class LabelS : Label
        {
            public LabelS()
            {
                //控件绘制的时候减少闪烁
                this.SetStyle(ControlStyles.UserPaint, true);
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                System.Windows.Forms.ControlPaint.DrawSizeGrip(e.Graphics, Color.Black, 1, 0, this.Size.Width, this.Size.Height);
            }
        }


     接下来,我们动态用代码生成这些控件,并设置控件的布局,

     现在构造函数里生成这些控件,然后在用户控件的ComCheckBoxList_Layout事件里面布局这些控件,代码如下

      

     /// <summary>
        /// 带下拉框的用户控件
        /// </summary>
        public partial class ComCheckBoxList : UserControl
        {
            private TextBox tbSelectedValue;
            private ButtonS btnSelect;//下拉箭头
            private LabelS lbGrip;//此LABEL用于设置可以拖动下拉窗体变化
    
            private CheckedListBox checkListBox;
            private Label lbSelectAll;//全选
            private Label lbSelectNo;//取消
    
            private Form frmCheckList;
    
            private Panel pnlBack;
            private Panel pnlCheck;
    
            private System.Drawing.Point DragOffset; //用于记录窗体大小变化的位置
            public ComCheckBoxList()
            {
                InitializeComponent();
                this.Name = "comBoxCheckBoxList";
                this.Layout+=new LayoutEventHandler(ComCheckBoxList_Layout);
    
                //生成控件
                tbSelectedValue = new TextBox();
                tbSelectedValue.ReadOnly = true;
                tbSelectedValue.BorderStyle = BorderStyle.None;
    
                //下拉箭头
                this.btnSelect = new ButtonS();
                btnSelect.FlatStyle = FlatStyle.Flat;
                btnSelect.Click+=new EventHandler(btnSelect_Click);
    
                //全选
                this.lbSelectAll = new Label();
                lbSelectAll.BackColor = Color.Transparent;
                lbSelectAll.Text = "全选";
                lbSelectAll.Size = new Size(40, 20);
                lbSelectAll.ForeColor = Color.Blue;
                lbSelectAll.Cursor = Cursors.Hand ;
                lbSelectAll.TextAlign = ContentAlignment.MiddleCenter;
                lbSelectAll.Click+=new EventHandler(lbSelectAll_Click);
    
                //取消
                lbSelectNo = new Label();
                lbSelectNo.BackColor = Color.Transparent;
                lbSelectNo.Text = "取消";
                lbSelectNo.Size = new Size(40, 20);
                lbSelectNo.ForeColor = Color.Blue;
                lbSelectNo.Cursor = Cursors.Hand;
                lbSelectNo.TextAlign = ContentAlignment.MiddleCenter;
                lbSelectNo.Click+=new EventHandler(lbSelectNo_Click);
    
                //生成checkboxlist
                this.checkListBox = new CheckedListBox();
                checkListBox.BorderStyle = BorderStyle.None;
                checkListBox.Location = new Point(0,0);
                checkListBox.CheckOnClick = true;
                checkListBox.ScrollAlwaysVisible = true;
                checkListBox.LostFocus +=new EventHandler(checkListBox_LostFocus);
                checkListBox.ItemCheck+=new ItemCheckEventHandler(checkListBox_ItemCheck);
    
                //窗体
                frmCheckList = new Form();
                frmCheckList.FormBorderStyle = FormBorderStyle.None;
                frmCheckList.StartPosition = FormStartPosition.Manual;
                frmCheckList.BackColor = SystemColors.Control;
                frmCheckList.ShowInTaskbar = false;
    
                //可拖动窗体大小变化的LABEL
                lbGrip = new LabelS();
                lbGrip.Size = new Size(9,18);
                lbGrip.BackColor = Color.Transparent;
                lbGrip.Cursor = Cursors.SizeNWSE;
                lbGrip.MouseDown+=new MouseEventHandler(lbGrip_MouseDown);
                lbGrip.MouseMove+=new MouseEventHandler(lbGrip_MouseMove);
    
                //panel
                pnlBack = new Panel();
                pnlBack.BorderStyle = BorderStyle.Fixed3D;
                pnlBack.BackColor = Color.White;
                pnlBack.AutoScroll = false;
    
                //
                pnlCheck = new Panel();
                pnlCheck.BorderStyle = BorderStyle.FixedSingle;
                pnlCheck.BackColor = Color.White; ;
    
                SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                SetStyle(ControlStyles.ResizeRedraw, true);
    
                pnlBack.Controls.Add(tbSelectedValue);
                pnlBack.Controls.Add(btnSelect);
    
                this.Controls.Add(pnlBack);
    
                pnlCheck.Controls.Add(checkListBox);
                pnlCheck.Controls.Add(lbSelectAll);
                pnlCheck.Controls.Add(lbSelectNo);
                pnlCheck.Controls.Add(lbGrip);
                this.frmCheckList.Controls.Add(pnlCheck);
    
              
            }
    
            private void ReloationGrip()
            {
    
                lbGrip.Top = this.frmCheckList.Height - lbGrip.Height - 1;
                lbGrip.Left = this.frmCheckList.Width - lbGrip.Width - 1;
    
                lbSelectAll.Left =5;
                lbSelectAll.Top = frmCheckList.Height - lbSelectAll.Height;
    
                lbSelectNo.Left = 50;
                lbSelectNo.Top = frmCheckList.Height - lbSelectNo.Height;
          
    
            }
    
            #region 事件
            
          
            //布局
            private void ComCheckBoxList_Layout(object sender,LayoutEventArgs e)
            {
                this.Height = tbSelectedValue.Height + 6;
                this.pnlBack.Size = new Size(this.Width, this.Height - 2);
    
                //设置按钮的位置
                this.btnSelect.Size = new Size(16, this.Height - 6);
                btnSelect.Location = new Point(this.Width - this.btnSelect.Width - 4, 0);
    
                this.tbSelectedValue.Location = new Point(2, 2);
                this.tbSelectedValue.Width = this.Width - btnSelect.Width - 4;
    
                checkListBox.Height =150;
    
                //设置窗体
                this.frmCheckList.Size = new Size(this.Width,this.checkListBox.Height);
                this.pnlCheck.Size =frmCheckList.Size;
               
    
                this.checkListBox.Width = this.frmCheckList.Width;
                this.checkListBox.Height = this.frmCheckList.Height-lbSelectNo.Height;
    
                ReloationGrip();
    
    
            }
            /// <summary>
            /// 单价下拉框
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            public void btnSelect_Click(object sender,EventArgs e)
            {
                if (this.frmCheckList.Visible == false)
                {
                    Rectangle rec = this.RectangleToScreen(this.ClientRectangle);
                    this.frmCheckList.Location = new Point(rec.X, rec.Y + this.pnlBack.Height);
                    this.frmCheckList.Show();
                    this.frmCheckList.BringToFront();
    
                    ReloationGrip();
                }
                else
                    this.frmCheckList.Hide();
            }
    
            //全选事件
            private void lbSelectAll_Click(object sender, EventArgs e)
            { 
            
            }
            //取消
            private void lbSelectNo_Click(object sender,EventArgs e)
            { 
            
            }
    
            private void checkListBox_LostFocus(object sender, EventArgs e)
            {
                //如果鼠标位置在下拉框按钮的以为地方,则隐藏下拉框
                if (!this.btnSelect.RectangleToScreen(this.btnSelect.ClientRectangle).Contains(Cursor.Position))
                {
                    frmCheckList.Hide();
                }
            }
    
            private void checkListBox_ItemCheck(object sender, ItemCheckEventArgs e)
            {
    
            }
    
            /// <summary>
            /// 鼠标按下
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void lbGrip_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button==MouseButtons.Left)
                {
                    int offsetX = System.Math.Abs(Cursor.Position.X - frmCheckList.RectangleToScreen(this.frmCheckList.ClientRectangle).Right);
                    int offsetY = System.Math.Abs(Cursor.Position.Y - frmCheckList.RectangleToScreen(this.frmCheckList.ClientRectangle).Bottom);
                    this.DragOffset = new Point(offsetX, offsetY);
                }
            }
    
            /// <summary>
            /// 鼠标移动
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void lbGrip_MouseMove(object sender,MouseEventArgs e)
            {
                if (e.Button==MouseButtons.Left)
                {
                    //获取拉伸长度
                    int curWidth = Cursor.Position.X - frmCheckList.Location.X;
                    int curHeight = Cursor.Position.Y - frmCheckList.Location.Y;
                      if (curWidth<this.Width)
    	              {
    		                curWidth=this.Width;
    	               }
    
                      if (curHeight<checkListBox.Height)
    	              {
    		                curHeight=checkListBox.Height;
    	              }
              
                    this.frmCheckList.Size=new Size(this.Width,curHeight);
                    this.pnlCheck.Size=frmCheckList.Size;
                    this.checkListBox.Height=(this.frmCheckList.Height-lbGrip.Height)<50?50:this.frmCheckList.Height-lbGrip.Height;
    
                   ReloationGrip();
                    SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                    SetStyle(ControlStyles.ResizeRedraw, true);
                    SetStyle(ControlStyles.UserPaint, true);
                    SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                    
    
    
                }
            }
    
            #endregion
        }


      这样用户控件的布局以及完成,我们编译项目,就可以拖动用户控件到窗体上,看到效果了

     

    2. 绑定和添加数据

            2.1 从数据库中读取数据绑定显示

                   既然可以从数据库中读取数据,数据源就可以是DATATABLE,我们在这里为用户控件设置数据源,以及绑定后,要显示的字段,以及对于的值字段。

                  为控件添加以下属性

                

        /// <summary>
            /// 设置数据源
            /// </summary>
            public object DataSource
            {
                set
                {
                    this.checkListBox.DataSource = value;
                }
                get
                {
                    return checkListBox.DataSource;
                }
            }
            /// <summary>
            /// 设置值
            /// </summary>
            public string ValueMember
            {
                set
                {
                    checkListBox.ValueMember = value;
                }
            }
            /// <summary>
            /// 设置显示名称
            /// </summary>
            public string DisplayMember
            {
                set
                {
                    checkListBox.DisplayMember = value;
                }
            }


              2.2 添加项

               我们也可以不绑定数据源,为控件添加项,所有这里我们要为控件添加一个方法,可以实现手动添加项。

               

      /// <summary>
            /// 添加项
            /// </summary>
            public int AddItems(object value)
            {
                checkListBox.Items.Add(value);
                return checkListBox.Items.Count;
            }


        这样我们就为用户控件实现了两种显示数据的方法。下面我们看看在窗体中怎样使用这两种方法

         在窗体上放置两个按钮,然后绑定数据

       

     private void button1_Click(object sender, EventArgs e)
            {
                //绑定数据源
                DataTable dt = new DataTable();
                DataColumn dc1 = new DataColumn("weibo", typeof(System.String));
                dt.Columns.Add(dc1);
    
                DataColumn dc2 = new DataColumn("mail", typeof(System.String));
                dt.Columns.Add(dc2);
    
                DataColumn dc3 = new DataColumn("blog", typeof(System.String));
                dt.Columns.Add(dc3);
    
    
                for (int i = 0; i < 50; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = "下里巴人simple";
                    dr[1] = "524300045@qq.com";
                    dr[2] = "msdn" + i.ToString();
                    dt.Rows.Add(dr);
                }
                comCheckBoxList1.DataSource = dt;
                comCheckBoxList1.DisplayMember = "weibo";
                comCheckBoxList1.ValueMember = "mail";
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                //添加项
                comCheckBoxList2.AddItems("邮箱:524300045@qq.com");
                comCheckBoxList2.AddItems("微博:下里巴人simple");
            }

      至此,第二步骤已经完成,可以实现大部分功能了.

       3.事件处理

         这里我们定义下全选事件,取消事件,和某项选中状态更改时发生的事件

       

      //全选事件
            private void lbSelectAll_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < checkListBox.Items.Count; i++)
                {
                    checkListBox.SetItemChecked(i, true);
                }
                tbSelectedValue.Text ="已选择"+checkListBox.Items.Count.ToString()+"项";
            }
            //取消
            private void lbSelectNo_Click(object sender,EventArgs e)
            {
                for (int i = 0; i < checkListBox.Items.Count; i++)
                {
                    checkListBox.SetItemChecked(i, false);
                }
                tbSelectedValue.Text = "没有选择!";
            }


         这里我们自定义一个选项状态更改时发生的事件,供别人使用的时候自行处理选择状态更改逻辑,需要我们自定义事件

      

      //单击列表项状态更改事件
            public delegate void CheckBoxListItemClick(object sender, ItemCheckEventArgs e);
            public event CheckBoxListItemClick ItemClick;

       

       private void checkListBox_ItemCheck(object sender, ItemCheckEventArgs e)
            {
                ItemClick(sender, e);
    
               //获取选中的数量
                int nCount = this.checkListBox.CheckedItems.Count;
                if (this.checkListBox.CheckedItems.Contains(this.checkListBox.Items[e.Index]))
                {
    
                    if (e.NewValue != CheckState.Checked)
                    {
                        nCount--;
                    }
                }
                else
                {
                    if (e.NewValue == CheckState.Checked)
                    {
                        nCount++;
                    }
                }
                tbSelectedValue.Text = "已选择" + nCount.ToString() + "项";
           
            }

      

      /// <summary>
            /// 选项集合
            /// </summary>
            public CheckedListBox.ObjectCollection Items
            {
                get
                {
                    return checkListBox.Items;
                }
            }
    
            /// <summary>
            /// 获取选中项的文本
            /// </summary>
            /// <param name="item"></param>
            /// <returns></returns>
            public string GetItemText(object item)
            {
                return checkListBox.GetItemText(item);
            }


     

    然后当用户把控件拖放到窗体上,我们就可以使用该事件了。

      

      /// <summary>
            /// 获取选中项的文本
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void comCheckBoxList1_ItemClick(object sender, ItemCheckEventArgs e)
            {
              string text=comCheckBoxList1.GetItemText(comCheckBoxList1.Items[e.Index]);
              MessageBox.Show(text);
            }


        到此,这个控件的基本功能已经实现完成。再看下效果图

               

         demo下载:http://download.csdn.net/detail/zx13525079024/4454696

    展开全文
  • 摘要:C#源码,控件类库,ComboBox,Select美化 美化ComboBox,为窗体中的下拉列表框各个下拉项着色,这样在数据量较多的时候,更容易区分和取值。  private static Brush[] listBoxBrushes;//该数组用来存储绘制...
  • 添加一个组件 开始自定义控件 文件结构 Entity Code using System; namespace Test { public class DataDto { public Guid Id { get; set; } public Guid ParentId { get; set; } public
  • LookupEdit 下拉单选,可搜索,下拉展示为一个table; ComboxEdit 下拉单选,可当做text使用,输入数据源中没有的项,只有显示值; CheckcomboxEdit 下拉多选,可搜索,有显示值、隐藏值。 转载于:...
  • winform-下拉框多选

    2016-08-15 11:47:35
    支持下拉框多选、全选
  • 如上图所示布局省份combobox的SelectedIndexChanged事件: public Form1() { InitializeComponent(); cmbprovince.SelectedIndex = 0;//默认选择省份里的第一项 ... private void cmbprovince_...
  • C#使用DropDownList绑定添加新数据的方法汇总DropDownList控件又称下拉列表框控件控件 列表 中的多行数 据 以隐含 的形式表 示 出 来,当用户需要选择所需列表项时,通过点击 “下三角 ”图形 展示 ,用户每次 ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,341
精华内容 936
关键字:

winform下拉列表框控件