精华内容
下载资源
问答
  • winform下拉图片列表
    千次阅读
    2016-08-11 23:03:16

    一,效果图:

    效果图

    二,代码:

    using System.Collections.Generic;
    using System.Drawing;
    using System.Net;
    using System.Windows.Forms;
    
    namespace ImageComBoxDemo
    {
        public partial class Form1 : Form
        {
            private ComboBox cbbImages = new ComboBox();
            private List<Image> images =new List<Image>();
            public Form1()
            {
                InitializeComponent();
    
                images.Add(GetImage("http://desk.fd.zol-img.com.cn/t_s960x600c5/g5/M00/05/06/ChMkJlaYoJiITw2PABGeHq5rgjYAAHUmABiB8IAEZ42227.jpg"));
                images.Add(GetImage("http://desk.fd.zol-img.com.cn/t_s960x600c5/g5/M00/05/06/ChMkJlaYoJiITw2PABGeHq5rgjYAAHUmABiB8IAEZ42227.jpg"));
                images.Add(GetImage("http://desk.fd.zol-img.com.cn/t_s960x600c5/g5/M00/05/06/ChMkJlaYoJiITw2PABGeHq5rgjYAAHUmABiB8IAEZ42227.jpg"));
                images.Add(GetImage("http://desk.fd.zol-img.com.cn/t_s960x600c5/g5/M00/05/06/ChMkJlaYoJiITw2PABGeHq5rgjYAAHUmABiB8IAEZ42227.jpg"));
                images.Add(GetImage("http://desk.fd.zol-img.com.cn/t_s960x600c5/g5/M00/05/06/ChMkJlaYoJiITw2PABGeHq5rgjYAAHUmABiB8IAEZ42227.jpg"));
    
                cbbImages.DrawMode = DrawMode.OwnerDrawFixed;
                cbbImages.DropDownStyle = ComboBoxStyle.DropDownList;
                cbbImages.DrawItem += CbbImages_DrawItem;
                cbbImages.Items.Add("1");
                cbbImages.Items.Add("2");
                cbbImages.Items.Add("3");
                cbbImages.Items.Add("4");
                cbbImages.Items.Add("5");
                cbbImages.SelectedIndex = 0;
                Controls.Add(cbbImages);
    
            }
            private void CbbImages_DrawItem(object sender, DrawItemEventArgs e)
            {
                e.DrawBackground();
                e.Graphics.DrawImage(images[e.Index], 0, e.Bounds.Y,16,16);
                e.Graphics.DrawString(cbbImages.Items[e.Index].ToString(),new Font("宋体",12),new SolidBrush(Color.Red),20,e.Bounds.Y+1);
            }
    
            private Image GetImage(string url)
            {
                try
                {
                return Image.FromStream(WebRequest.Create(url).GetResponse().GetResponseStream());
    
                }
                catch
                {
                    return new Bitmap(50, 50);
                }
            }
        }
    }
    
    更多相关内容
  • 1、默认下拉菜单控件可编辑选中的内容 2、设置不可编辑后,控件会有一个灰色不可编辑状态的背景颜色 3、设置不可编辑后,下拉控件动态添加的值,会显示空白的情况 此源码就是解决以上问题 1、简单几行代码,即可设置...
  • ![图片说明](https://img-ask.csdn.net/upload/202005/23/1590219391_338539.png) 让下拉列表从sql数据库中获取值
  • 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;
            }
        }
    }
    
    

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

    在这里插入图片描述

    展开全文
  • Winform中如何实现下拉树效果 简介:Winform中如何实现下拉树效果(类似于ComboBox下拉时显示的是树状结构),  如果是BS的WebForm就有很多解决方案了,Devexpress就更方便,利用DropdownList和TreeView去组合控件实现  ...

    Winform中如何实现下拉树效果

    简介:Winform中如何实现下拉树效果(类似于ComboBox下拉时显示的是树状结构),

            如果是BS的WebForm就有很多解决方案了,Devexpress就更方便,利用DropdownList和TreeView去组合控件实现

            但是CS中的Winform的传统窗体控件和Devexpress里面去实现此功能就比较麻烦了

            而此文,我们要讲解的就是如何利用Winform的传统用户控件去实现下拉树的效果

           主要利用TextBox,TreeView,Button等控件事件组合而成类似于ComboBox下拉出现树状结构

           下面就看看我们要实现的下拉树的效果图:

    说明:上图所示的就是我们要实现的效果当点击下拉控件时,出现一个树状结构,

             当我们选择树节点时(双击),把相应的节点值,放入上面的文本框中

             好了,下面我们就马上去看看,这样的功能是如何实现的呢?

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    首先我们需要建立一个用户控件,如图:

    说明:不需要再进行任何操作

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    接下来,我们需要放一张图片文件,这张图片文件是为了模拟下拉框右边的三角下拉按钮用的,如图:

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    接下来就是完全的代码模块了,就这样的界面操作,是如何让我们做到下拉树效果的呢?

    在用户控件窗体CS文件中,我们需要添加如下代码:

    一.自定义成员变量

    二.内部辅助方法

    三.对TextBox的事件处理

    四.对TreeView的事件处理

    五.对Button的事件处理

    六.对自身的事件处理

    七.外部属性封装

    八.构造函数

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    别看上面说的步骤很多,其实加起来也就几百行代码,也没有什么难点,接下来我们就依次展示我们八大步骤模块的代码:

    一.自定义成员变量 

            #region 自定义成员变量
    
            private TextBox m_TextBox;
            private TreeView m_TreeView;
            private Button m_DropdownButton;
            private int m_MaxDropDownItems = 8;
            //是否正在显示下拉列表
            private bool b_Dropdown = false;
            //使能
            private bool b_Enabled = true;
            //事件
            public event EventHandler DropDown;
            public event EventHandler DropDownClosed;
            public event EventHandler EnableChanged;
            public event TreeViewCancelEventHandler BeforeExpand;
            public event TreeViewCancelEventHandler BeforeCollapse;
            public event TreeViewEventHandler AfterExpand;
            public event TreeViewEventHandler AfterCollapse;
    
            #endregion
    

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    二.内部辅助方法

            #region 内部辅助方法
    
            /// <summary>
            /// 创建并初始化所有控件,包括添加事件处理函数
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            private void InitControls()
            {
                //TextBox
                this.m_TextBox = new TextBox();
                this.m_TextBox.KeyDown += new KeyEventHandler(m_TextBox_KeyDown);
                this.m_TextBox.Parent = this;
    
                //Button
                this.m_DropdownButton = new Button();
                //Assembly asm = Assembly.GetExecutingAssembly();
                //System.IO.Stream stream = asm.GetManifestResourceStream(asm.GetName().Name + ".BM_dropdown.bmp");
                Bitmap bm = new Bitmap(this.GetType(), "BM_dropdown.bmp");
                this.m_DropdownButton.Image = bm;
                this.m_DropdownButton.Width = 16;
                this.m_DropdownButton.Height = this.m_TextBox.Height - 2;
                this.m_DropdownButton.Location = new Point(this.m_TextBox.Right - 18, 1);
                this.m_DropdownButton.Click += new EventHandler(m_DropdownButton_Click);
                this.m_DropdownButton.FlatStyle = FlatStyle.Flat;
                this.m_DropdownButton.Parent = this;
                this.m_DropdownButton.BringToFront();
    
                //TreeView
                this.m_TreeView = new TreeView();
                this.m_TreeView.Visible = false;
                this.m_TreeView.DoubleClick += new EventHandler(m_TreeView_DoubleClick);
                this.m_TreeView.KeyDown += new KeyEventHandler(m_TreeView_KeyDown);
                this.m_TreeView.LostFocus += new EventHandler(m_TreeView_LostFocus);
                this.m_TreeView.BeforeExpand += new TreeViewCancelEventHandler(m_TreeView_BeforeExpand);
                this.m_TreeView.BeforeCollapse += new TreeViewCancelEventHandler(TreeComboBox_BeforeCollapse);
                this.m_TreeView.AfterExpand += new TreeViewEventHandler(m_TreeView_AfterExpand);
                this.m_TreeView.AfterCollapse += new TreeViewEventHandler(TreeComboBox_AfterCollapse);
                this.m_TreeView.Location = new Point(0, 0);
                this.m_TreeView.Parent = null;
    
                this.LostFocus += new EventHandler(TreeComboBox_LostFocus);
            }
    
            /// <summary>
            /// 布局所有控件,让TextBox尺寸适应容器尺寸
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            private void LayoutControls()
            {
                this.m_TextBox.Width = this.Width;
                this.Height = this.m_TextBox.Height;
                this.m_DropdownButton.Left = this.m_TextBox.Right - 18;
                this.m_DropdownButton.Height = this.m_TextBox.Height - 2;
                this.m_TreeView.Width = this.Width;
                this.m_TreeView.Height = (int) ((this.Font.Height + 3)*this.m_MaxDropDownItems);
            }
    
            /// <summary>
            /// 显示下拉列表
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            private void ShowDropDown()
            {
                if (this.Parent == null)
                    return;
    
                // 智能计算显示的位置,尝试在下方显示,如果没有足够空间,则在上方显示
                // 尝试在下方的位置(现在只是相对父窗口的相对位置)
                Point pos = new Point(this.Left, this.Bottom - 1);
    
                // 把位置映射到顶层窗口,获取父窗口的屏幕坐标
                Point parentPos = this.Parent.PointToScreen(this.Parent.Location);
                // 获取顶层窗口的屏幕坐标
                Point topParentPos = this.TopLevelControl.PointToScreen(this.Parent.Location);
                // 把相对父窗口的位置变换为相对顶级窗口的位置,因为popup的父是顶级窗口
                pos.Offset(parentPos.X - topParentPos.X, parentPos.Y - topParentPos.Y);
    
                // 检查是否有足够空间用于在label下方显示day picker
                if ((pos.Y + this.m_TreeView.Height) > this.TopLevelControl.ClientRectangle.Height)
                {
                    // 没有足够的空间(超出了顶级窗口客户区),尝试在上方显示将Y方向,向上平移
                    pos.Y -= (this.Height + this.m_TreeView.Height);
                    if (pos.Y < 0)
                    {
                        // 如果上方仍然没有空间显示,则显示在顶级窗口的底部
                        pos.Y = (this.TopLevelControl.ClientRectangle.Height - this.m_TreeView.Height);
                    }
                }
    
                // 尝试停靠,如果右边超过顶级窗口的客户区,则将控件向左移动,并紧靠在顶级窗口右侧
                if ((pos.X + this.m_TreeView.Width) > this.TopLevelControl.ClientRectangle.Width)
                    pos.X = (this.TopLevelControl.ClientRectangle.Width - this.m_TreeView.Width);
    
                this.m_TreeView.Location = pos; // this.Parent.PointToScreen(pt);
                this.m_TreeView.Visible = true;
                this.m_TreeView.Parent = this.TopLevelControl;
                this.m_TreeView.BringToFront();
                this.b_Dropdown = true;
                //raise event
                if (this.DropDown != null)
                    this.DropDown(this, EventArgs.Empty);
                this.m_TreeView.Focus();
            }
    
            /// <summary>
            /// 隐藏下拉列表
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            private void HideDropDown()
            {
                if (this.DropDownClosed != null)
                    this.DropDownClosed(this, EventArgs.Empty);
                this.m_TreeView.Parent = null;
                this.m_TreeView.Visible = false;
                this.b_Dropdown = false;
            }
    
            #endregion

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    三.对TextBox的事件处理

            #region 事件处理 - TextBox
    
            /// <summary>
            /// 在编辑框中按下按键
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void m_TextBox_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    if (this.b_Dropdown)
                        this.HideDropDown();
                    else
                        this.ShowDropDown();
                }
                else if (e.KeyCode == Keys.Down)
                {
                    this.ShowDropDown();
                    this.m_TreeView.Focus();
                }
            }
    
            #endregion

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    四.对TreeView的事件处理

            #region 事件处理 - TreeView
    
            /// <summary>
            /// 在下拉列表中选择了一个节点的事件处理!
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void m_TreeView_KeyDown(object sender, KeyEventArgs e)
            {
                //如果按下回车表示要选中当前节点
                if (e.KeyCode == Keys.Enter)
                {
                    this.m_TreeView_DoubleClick(sender, EventArgs.Empty);
                }
            }
    
            /// <summary>
            /// 失去焦点时,如果不是被下拉按钮夺取的焦点,则隐藏它!
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void m_TreeView_LostFocus(object sender, EventArgs e)
            {
                if (!this.m_DropdownButton.Focused)
                    this.HideDropDown();
            }
    
            /// <summary>
            /// 在下拉列表中双击
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void m_TreeView_DoubleClick(object sender, EventArgs e)
            {
                TreeNode node = this.m_TreeView.SelectedNode;
                if (node != null)
                    this.m_TextBox.Text = node.Text;
    
                if (this.b_Dropdown)
                {
                    this.HideDropDown();
                }
            }
    
            /// <summary>
            /// 折叠后事件
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void TreeComboBox_AfterCollapse(object sender, TreeViewEventArgs e)
            {
                if (this.AfterCollapse != null)
                    this.AfterCollapse(this, e);
            }
    
            /// <summary>
            /// 折叠前事件
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void TreeComboBox_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
            {
                if (this.BeforeCollapse != null)
                    this.BeforeCollapse(this, e);
            }
    
            /// <summary>
            /// 展开后事件
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void m_TreeView_AfterExpand(object sender, TreeViewEventArgs e)
            {
                if (this.AfterExpand != null)
                    this.AfterExpand(this, e);
            }
    
            /// <summary>
            /// 展开前事件
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void m_TreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
            {
                if (this.BeforeExpand != null)
                    this.BeforeExpand(this, e);
            }
    
    
            #endregion

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    五.对Button的事件处理

            #region 事件处理 - Button
    
            /// <summary>
            /// 下拉按钮点击事件
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void m_DropdownButton_Click(object sender, EventArgs e)
            {
                //throw new Exception("The method or operation is not implemented.");
                if (this.b_Dropdown)
                    this.HideDropDown();
                else
                    this.ShowDropDown();
            }
    
            #endregion

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    六.对自身的事件处理

            #region 事件处理 - 自身
    
            /// <summary>
            /// 失去焦点
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void TreeComboBox_LostFocus(object sender, EventArgs e)
            {
                if (this.b_Dropdown)
                    this.HideDropDown();
            }
    
            /// <summary>
            /// 重设尺寸
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            /// <param name="e"></param>
            protected override void OnResize(EventArgs e)
            {
                base.OnResize(e);
                this.LayoutControls();
            }
    
            /// <summary>
            /// 尺寸改变
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            /// <param name="e"></param>
            protected override void OnSizeChanged(EventArgs e)
            {
                base.OnSizeChanged(e);
                this.LayoutControls();
            }
    
            #endregion

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    七.外部属性封装

            #region 外部属性封装
    
            /// <summary>
            /// 获取节点集合
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            public TreeNodeCollection Nodes
            {
                get { return this.m_TreeView.Nodes; }
            }
    
            /// <summary>
            /// 设置或者获取节点的图片列表
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            public ImageList ImageList
            {
                get { return this.m_TreeView.ImageList; }
                set { this.m_TreeView.ImageList = value; }
            }
    
            /// <summary>
            /// 重写enabled属性
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            public new bool Enabled
            {
                get { return this.b_Enabled; }
                set
                {
                    if (this.b_Enabled != value)
                    {
                        this.b_Enabled = value;
                        this.m_DropdownButton.Enabled = value;
                        this.b_Enabled = value;
                        //当灰掉时,隐藏下拉列表
                        if (!this.b_Enabled && this.b_Dropdown)
                            this.HideDropDown();
                        if (this.EnableChanged != null)
                            this.EnableChanged(this, EventArgs.Empty);
                    }
                }
            }
    
            /// <summary>
            /// 字体
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            public override Font Font
            {
                get { return base.Font; }
                set
                {
                    base.Font = value;
                    this.m_TextBox.Font = value;
                    this.m_TreeView.Font = value;
                    //调整布局
                    this.LayoutControls();
                }
            }
    
            /// <summary>
            /// 重写Text
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            public override string Text
            {
                get { return this.m_TextBox.Text; }
                set
                {
                    if (this.Text != value)
                    {
                        this.m_TextBox.Text = value;
                    }
                }
            }
    
            /// <summary>
            /// 是否显示lines
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            public bool ShowLines
            {
                get { return this.m_TreeView.ShowLines; }
                set { this.m_TreeView.ShowLines = value; }
            }
    
            /// <summary>
            /// 是否显示+ -按钮
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            public bool ShowPlusMinus
            {
                get { return this.m_TreeView.ShowPlusMinus; }
                set { this.m_TreeView.ShowPlusMinus = value; }
            }
    
            /// <summary>
            /// 是否显示root lines
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            public bool ShowRootLines
            {
                get { return this.m_TreeView.ShowRootLines; }
                set { this.m_TreeView.ShowRootLines = value; }
            }
    
            /// <summary>
            /// 是否显示root lines
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            public bool ShowNodeToolTips
            {
                get { return this.m_TreeView.ShowNodeToolTips; }
                set { this.m_TreeView.ShowNodeToolTips = value; }
            }
    
            /// <summary>
            /// 获取或者设置选中的节点!
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            public TreeNode SelectedNode
            {
                get { return this.m_TreeView.SelectedNode; }
                set
                {
                    this.m_TreeView.SelectedNode = value;
                    if (value != null)
                        this.Text = value.Text;
                }
            }
    
            /// <summary>
            /// 设置下拉最多项
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 13:29:37</time>
            public int MaxDropDownItems
            {
                get { return this.m_MaxDropDownItems; }
                set
                {
                    if (this.m_MaxDropDownItems != value)
                    {
                        this.m_MaxDropDownItems = value;
                        this.m_TreeView.Height = this.m_TextBox.Height*value;
                    }
                }
            }
    
            #endregion

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    八.构造函数

            /// <summary>
            /// 构造函数
            /// </summary>
            public TreeComboBox()
            {
                this.InitControls();
                this.LayoutControls();
            }

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    OK,经过上面八个步骤,我们的下拉树自定义控件就完成了,接下来,就看下我们是如何使用它的

    首先我们需要生成一下项目,成功后我们将在我们的工具栏中,看到如下控件:

    说明:做到此处出现错误时,请参照文章尾部提出的解决方案...

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    接下来我们就需要把该控件拖到我们的窗体中

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    这些完成的之后,接下来我们就是我们真正的代码调用模块了,我们需要在调用下拉树控件窗体中添加如下代码:

    首先是绑定数据:

            /// <summary>
            /// 数据分类数据绑定
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 14:22:35</time>
            private void BindDataType()
            {
                for (int i = 0; i < 4; i++)
                {
                    tcbDataType.Nodes.Add("key" + i, "Depart" + i, 0, 0);
                    for (int j = 0; j < i + 1; j++)
                    {
                        tcbDataType.Nodes[i].Nodes.Add("key_child" + i + j, "User" + i + j, 2, 2);
                    }
                }
            }

    说明:此处的for循环代表树的级数,在此我们可以看出,此代码将会生成一个两级树,如果我们想出现多级,只需要加入多个循环,就可以了

             如果您嫌麻烦,可以封装一下通用方法

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    其次是添加事件:

    1.添加折叠事件

            /// <summary>
            /// 下拉树折叠后事件
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 14:18:29</time>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void tcbDataType_AfterCollapse(object sender, TreeViewEventArgs e)
            {
                if (e.Node.ImageIndex == 1)
                    e.Node.ImageIndex = e.Node.SelectedImageIndex = 0;
            }

    2.添加展开事件

            /// <summary>
            /// 下拉树展开后事件
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-11-1 14:18:29</time>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void tcbDataType_AfterExpand(object sender, TreeViewEventArgs e)
            {
       	   if (e.Node.ImageIndex == 0)
        	       e.Node.ImageIndex = e.Node.SelectedImageIndex = 1;
            }

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    最后,我们需要注册事件:

        可以在调用窗体的构造函数中加入如下代码: 

                this.tcbDataType.AfterExpand += new TreeViewEventHandler(tcbDataType_AfterExpand);
                this.tcbDataType.AfterCollapse += new TreeViewEventHandler(tcbDataType_AfterCollapse);


        或者查看下拉树控件的属性,在相应的事件中,选择我们编写的事件即可,如下图:

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    好了,到此,我们的下拉树就完成了,让我们看下效果,如图:

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    细心的人一定会发现,不对啊,这个数结构和文章的第一张图里面不一样啊,是的,因为少了节点图片,

    我们做的这么久还没有给节点加图片的操作呢,好了,接下来,就是我最后的操作了

    给树节点加图片操作,分三步

    1.拖一个ImageList控件到窗体中

    2.给ImageList添加图片列表,如图

    3.将我们的下拉树控件中的属性ImageList设置成我们刚拖进来的ImageList控件,就可以了

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    好的,做到这,我们的下拉树控件的一个完整效果就完成了,运行之后,我们就会看见文章刚出现的效果图的画面了

    不过,做到这,我们还要最后一个收尾操作,那就是获取下拉树中用户选择的节点键和值

    代码如下:

            /// <summary>
            /// 查询
            /// </summary>
            /// <author>PengZhen</author>
            /// <time>2013-10-30 11:08:03</time>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btSelect_Click(object sender, EventArgs e)
            {
                string value = tcbDataType.SelectedNode.Name;
    
                MessageBox.Show(value);
    
                string text = tcbDataType.Text.Trim();
    
                MessageBox.Show(text);
            }

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    OK,OK,终于,整个下拉树的完整操作就全部实现了,有木有发现很简单呢,嘿嘿

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    备注:此控件请放在窗体中使用,别放在用户控件中使用,至于为什么,您可以试试,就会出现意想不到的效果噢,

             而出现此问题,就留给你们自己去思考,解决吧,多去试试,变通思想,会出现更奇妙的事噢,

             好了,就写到这,让你们自己去见证奇迹的时刻把,嘿嘿...
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    写点题外话,当你们去完成以上操作时,可能会出现以下错误,去看看下面的链接,就知道怎么去解决了

    System.ArgumentException无法在类中找到资源 

     

    展开全文
  • Demo包含一些自定义控件(下拉列表,树形列表 , 图片列表等)均可实现透明效果。主窗体是一个海康相机的监控视频。使用时可替换成其他视频。
  • ComboBox 下拉列表框 Checkbox 复选框 RadioButton 单选框 GroupBox 分组控件 Label 标签 ListBox 列表框 PictureBox 图片框 DateTimePicker 日期时间控件 ProgressBar 进度条 NumericUpDown 数字输入框 ...
  • 今天在调试程序的发时候发现明明Winform界面上的菜单是有文字的,但是到运行的时候就不见掉了,在另一台电脑上是没有这个问题的。 最后发现是设置了多语言环境导致的,当我们对窗体设置过多语言之后,新添加控件...

    今天在调试程序的发时候发现明明Winform界面上的菜单是有文字的,但是到运行的时候就不见掉了,在另一台电脑上是没有这个问题的。

    最后发现是设置了多语言环境导致的,当我们对窗体设置过多语言之后,新添加控件或者菜单时,记得一定要把控件/菜单在用到的语言环境中都设置一下显示的文字和图片。否则在不同电脑上(语言环境不同),那些没有设置的文字的控件或者菜单会显示空白。

    所以如果我们同时设置了 中文简体 语言环境,记得一定要确保 Language=Default 的语言环境中对所有控件都设置过。

       

    这个是我自己疏忽导致的,但是找原因好了两三个小时,特别在这里mark一下。

    展开全文
  • a)点击ListView控件 ,设置属性项中LargeImageList想下拉列表与ImageList关联 b)为ListView添加项并与ImageList中每项关联 c)调整图片尺寸大小:首先选择ImageList控件,在属性中设置ImageSize的值即可 最终...
  • &lbrack;moka同学笔记&rsqb;...目录结构(文件不用解释,应该都可以看得懂,直接看代码)UITabBarController 基本定制UITabBarController 定制 ...准备好你的tabBar图片及其他图片(哈哈哈!!!!),我的图片都放在了...
  • 可以实现组合框下拉列表 前面显示图片后面显示名字
  • 设置方法:  一:Form对象  属性:  设计中的Name:窗体类的类名 AcceptButton:窗口的确定按钮 ...backgroundImage:背景图片 Font:设置字体 Formborderstyle:边框样式,常用Fixedsingle固定,size...
  • WinForm学习

    千次阅读 2022-03-16 09:32:47
    创建完成后出现如下界面 3)点击启动按钮,出现自己编写的窗体界面 五、Winform菜单和工具栏 1)ListBox 列表框 2)ComboBox 下拉框列表 3)CheckedListBox 复选框列表 4)MaskedTextBox 用于输入指定格式的内容,比如...
  • (2)引用图片时的路径问题 winform里面图片文件的路径,用windnows路径(反斜杠): 比如:“c\example\1.png”; 而"c:/example/123/jng" 则不支持。 (3)PictureBox和ImageList控件 PictureBox控件只能存放一张...
  • c#Winform自定义控件-目录

    千次阅读 多人点赞 2019-09-06 20:59:24
    官网: http://hzhcontrols.com/ 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 ...码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 如果觉得写的还行,...
  • 2.窗口设计与控件布局 3.窗口事件 (1)界面设计(2)逻辑代码 二.WinForm布局开发 3.逻辑事件代码Form.cs 四.图片框与项目资源 1.添加资源
  • ppt课件-winform程序设计_高级控件.ppt
  • 下拉刷新的ListView的中同时处理CheckBox,Button,和item事件
  • 最近做项目遇到需要循环打印一个带图片、表格、地图的列表,所以这里做一个记录,也希望能帮到有需要的人 //获取打印机列表 private void getPrintDocumentlist() { PrintDocument print = new PrintDocument();...
  • 注:以下方法不止适合下拉框,单元格可以显示文件,也可以显示单选框,下拉框,图片和超链接等,只要创建对应的类型就可以,例如单选框:DataGridViewCheckBoxColumn 下面以下拉组合框为例: 首先创建一个ComboBox...
  • 这次的项目需要使用Winform来实现,我们使用的是CSkin的皮肤。文章主要记录使用过程中遇到的小问题,也是楼主网上查找很久才找到的,这里总结下。插件地址:使用SKinDataGridView出现列乱排序问题下面这张图是我在...
  • 从今天开始咱们正式进入WinForm开发模式首先很官方的介绍下什么是winform:客户端应用程序:C/S这就是winform有什么特别特别重要的特点呢:可以操作用户电脑上的文件举个简单的例子:很多人都玩过LOL/CF/DOTA/CS/DNF...
  • WinForm窗口介绍

    2019-11-29 17:02:19
    WinForm窗口介绍 1.菜单工具 1.1 窗口内菜单-MenuStrip 1.2 窗口内给指定区域添加右键菜单-ContextMenuStrip 1.3 内嵌菜单区域 2.视图工具 ...ComboBox-下拉列表 DateTimePicker-日期选择框 Dom...
  • 在设计界面的时候,不管是在Web端,还是在WinForm端,或者是WPF或者移动界面等应用上,...界面的组织布局,我这里分为了三个部分,一个是主界面组织布局、列表界面组织布局、新增编辑界面组织布局。 点击获取WinForm
  • 删除下拉列表中的选项

    千次阅读 2017-04-12 10:43:20
    删除下拉列表中的单个选项可以用select对象的remove()、focus()方法和selectedIndex属性来实现。 1、remove()方法 该方法用于在下拉列表中删除指定的option对象。 myselect.remove(index) myselect:当前要删除...
  • winform 初学笔记

    2014-04-01 18:14:12
    Winform与js相互调用、文件保存后定位、树节点失去焦点后高亮显示、Timer、DataGridView序列号、DataGridView复选框列
  • DevExpress Winform常用控件是本人在前几个项目的一些笔记,在实际的项目中可能会经常涉及到,所以对常用控件的属性、方法、事件都列举出来。 在Visual Stuodio中添加DevExpress控件 1.使用控制台进入DevExpress...
  • 当时主要为开发公司内部ERP系统,重新设计实现了所有用到的Winform组建,包括Form窗体组建6个(支持换肤),基础控件25个。其中有很多参考借鉴其他开源组件,也有几个是集成的别人的组件,然后做了些调整。  现在...

空空如也

空空如也

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

winform下拉图片列表