-
2021-01-26 23:27:42
在C#的winform中实现两个下拉菜单控件(combobox)联动
在C#的winform中实现两个下拉菜单的联动,目的是通过一个菜单的内容的改变来控制另外一个菜单内容的生成因此,因此我们要将联动的两个控件的内容存储在一个字典中,一个为key一个为value,我们将第一个控件的事件设置为SelectedIndexChanged,这样当第一个下拉菜单进行改变时,第二个下拉菜单的内容也会变化,我实现的内容是第一个下拉框显示的是省,联动下一个下拉框显示城市,具体实例的代码如下:
private void Form1_Load(object sender, EventArgs e) { //初始化字典,用来存储控件中的内容 dir.Add("黑龙江省", "哈尔滨市,佳木斯市,大庆市,黑河市,齐齐哈尔市"); dir.Add("辽宁省", "沈阳市,大连市,营口市,抚顺市,葫芦岛市"); //遍历字典的key将key的值赋值给第一个控件的items foreach (KeyValuePair<string,string> kv in dir) { cbx1.Items.Add(kv.Key); } //控件默认选择第一个内容 cbx1.SelectedIndex = 0; } //设置全局变量字典,用来存储内容 Dictionary<string ,string > dir = new Dictionary<string,string >(); private void cbx1_SelectedIndexChanged(object sender, EventArgs e) { //每当改变第一个控件内容的时侯,之前的内容进行清空 cbx2.Items.Clear(); //将value的值进行赋值 string str = cbx1.SelectedItem.ToString(); string[] str1 = dir[str].Split(','); cbx2.Items.AddRange(str1); //控件默认选择第一个内容 cbx2.SelectedIndex = 0; }
更多相关内容 -
WinForm自定义下拉列表框用户控件
2018-10-11 14:38:34非常值得收藏,下载的源码资源: 1. 代码源自网络,并添加部分示例代码; 2. 自定义Combobx下拉框,支持基础控件下拉,也支持自定义控件下拉,扩展十分方便; -
c#winform下拉多选自定义控件(带全选)
2018-01-04 16:01:48c#winform下拉多选自定义控件(带全选),可以自己定义选择项的下拉框 -
C# 实现WinForm带复选框的下拉列表DLL控件
2021-03-16 00:15:55C# 实现WinForm带复选框的下拉列表DLL控件,ComboBox WinForm应用,是一个DLL的源码,实现窗口中带复选框的下拉列表,源码编译后生成UCComboBox.dll,你在项目中添加引用就可使用了。 运行环境:Visual Studio2010 -
winform自动补全控件和下拉树控件.rar
2021-10-20 16:00:50使用 SunnyUI扩展,SunnyUI.Net 是基于.Net Framework 4.0+、.Net Core3.1、.Net 5 框架的 C# WinForm 开源控件库、工具类库、扩展类库、多页面开发框架。 -
CheckBoxList_WinForm多选下拉框_下拉多选_
2021-10-01 03:54:39自定义的WinForm窗体的下拉框中可以多选 -
C# Winform 窗体 下拉菜单控件 设置不可编辑 自定义下拉控件的背景和文字颜色 DrawDownList
2019-09-02 13:02:121、默认下拉菜单控件可编辑选中的内容 2、设置不可编辑后,控件会有一个灰色不可编辑状态的背景颜色 3、设置不可编辑后,下拉控件动态添加的值,会显示空白的情况 此源码就是解决以上问题 1、简单几行代码,即可设置... -
c# winform自定义下拉列表(源码)
2021-03-24 10:30:15控件有个DropDownControl属性,你可以指定给这个控件点下拉按钮时弹出datagridview,richTextBox等等系统控件甚至是自己定义的UserControl控件。。。下载后自己看吧,有源码有示例有真像 -
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# winform ListView 中实现可编辑文本或双击出现下拉列表框
2021-01-24 06:30:20ListView 中实现可编辑文本或双击出现下拉列表框 重写listview空间,在控件中添加下拉框和文本框 -
Winform-控件之Combox-下拉框
2021-01-25 13:47:49} 在代码中获取Combox下拉框控件选择的值 public void test() { //获取下拉框当前选择的值,并转换为string string result = _selebox_1.SelectedItem.ToString(); } 在代码中给Combox下拉框控件添加删除值 ...如何将下拉框设为阻止用户输入——只能选择无法输入
1.将DropDownStyle的属性设为DropDownList
2.使用代码实时修改//设置样式为用户无法输入 _selebox_1.DropDownStyle = ComboBoxStyle.DropDownList; //设置样式为用户可以输入 _selebox_1.DropDownStyle = ComboBoxStyle.DropDown; //设置样式为简单组合框-暂没有测试过 _selebox_1.DropDownStyle = ComboBoxStyle.Simple;
如何设置Combox下拉框控件的初始选择项
public Window() { InitializeComponent(); //0代表第一个值,在初始化窗口后选择第一个值 _selebox_1.SelectedIndex = 0; }
在代码中获取Combox下拉框控件选择的值
public void test() { //获取下拉框当前选择的值,并转换为string string result = _selebox_1.SelectedItem.ToString(); }
在代码中给Combox下拉框控件添加删除值
public void test() { //给下拉框添加一个值 _selebox_1.Items.Add("添加一个选项"); //把下拉框指定的值删除 _selebox_1.Items.Remove("添加一个选项"); //或者用索引删除指定的值,0代表第一项 _selebox_1.Items.RemoveAt(0); }
-
C# 基于ComboBox 下拉多选 自定义控件 源代码
2021-06-26 09:44:25继承自C# Winform中ComboBox的下拉多选控件,操作简单,效果非常好 1、多选。 2、可绑定List,DataTable,Dictionary类型的数据源。 3、在下拉列表中可显示自定义的多列数据。 4、可在下拉列表中通过输入关键字,... -
winform下拉树
2015-09-15 16:00:43下拉树 -
(四十八)c#Winform自定义控件-下拉按钮
2019-09-08 11:02:28入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 如果...前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
麻烦博客下方点个【推荐】,谢谢
NuGet
Install-Package HZH_Controls
目录
https://blog.csdn.net/kwwwvagaa/article/details/100586547
用处及效果
准备工作
这个控件将继承自(三)c#Winform自定义控件-有图标的按钮,如不了解,请移步查看
开始
添加一个用户控件UCDropDownBtn,继承自UCBtnImg
处理一些属性
1 Forms.FrmAnchor _frmAnchor; 2 private int _dropPanelHeight = -1; 3 public new event EventHandler BtnClick; 4 [Description("下拉框高度"), Category("自定义")] 5 public int DropPanelHeight 6 { 7 get { return _dropPanelHeight; } 8 set { _dropPanelHeight = value; } 9 } 10 private string[] btns ; 11 [Description("按钮"), Category("自定义")] 12 public string[] Btns 13 { 14 get { return btns; } 15 set { btns = value; } 16 } 17 [Obsolete("不再可用的属性")] 18 [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] 19 public override Image Image 20 { 21 get; 22 set; 23 } 24 [Obsolete("不再可用的属性")] 25 [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] 26 public override ContentAlignment ImageAlign 27 { 28 get; 29 set; 30 }
点击时候显示下拉框
1 void UCDropDownBtn_BtnClick(object sender, EventArgs e) 2 { 3 if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false) 4 { 5 6 if (Btns != null && Btns.Length > 0) 7 { 8 int intRow = 0; 9 int intCom = 1; 10 var p = this.PointToScreen(this.Location); 11 while (true) 12 { 13 int intScreenHeight = Screen.PrimaryScreen.Bounds.Height; 14 if ((p.Y + this.Height + Btns.Length / intCom * 50 < intScreenHeight || p.Y - Btns.Length / intCom * 50 > 0) 15 && (_dropPanelHeight <= 0 ? true : (Btns.Length / intCom * 50 <= _dropPanelHeight))) 16 { 17 intRow = Btns.Length / intCom + (Btns.Length % intCom != 0 ? 1 : 0); 18 break; 19 } 20 intCom++; 21 } 22 UCTimePanel ucTime = new UCTimePanel(); 23 ucTime.IsShowBorder = true; 24 int intWidth = this.Width / intCom; 25 26 Size size = new Size(intCom * intWidth, intRow * 50); 27 ucTime.Size = size; 28 ucTime.FirstEvent = true; 29 ucTime.SelectSourceEvent += ucTime_SelectSourceEvent; 30 ucTime.Row = intRow; 31 ucTime.Column = intCom; 32 33 List<KeyValuePair<string, string>> lst = new List<KeyValuePair<string, string>>(); 34 foreach (var item in Btns) 35 { 36 lst.Add(new KeyValuePair<string, string>(item, item)); 37 } 38 ucTime.Source = lst; 39 40 _frmAnchor = new Forms.FrmAnchor(this, ucTime); 41 _frmAnchor.Load += (a, b) => { (a as Form).Size = size; }; 42 43 _frmAnchor.Show(this.FindForm()); 44 45 } 46 } 47 else 48 { 49 _frmAnchor.Close(); 50 } 51 }
处理一下按钮事件
1 void ucTime_SelectSourceEvent(object sender, EventArgs e) 2 { 3 if (_frmAnchor != null && !_frmAnchor.IsDisposed && _frmAnchor.Visible) 4 { 5 _frmAnchor.Close(); 6 7 if (BtnClick != null) 8 { 9 BtnClick(sender.ToString(), e); 10 } 11 } 12 }
完整代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace HZH_Controls.Controls.Btn { [DefaultEvent("BtnClick")] public partial class UCDropDownBtn : UCBtnImg { Forms.FrmAnchor _frmAnchor; private int _dropPanelHeight = -1; public new event EventHandler BtnClick; [Description("下拉框高度"), Category("自定义")] public int DropPanelHeight { get { return _dropPanelHeight; } set { _dropPanelHeight = value; } } private string[] btns ; [Description("按钮"), Category("自定义")] public string[] Btns { get { return btns; } set { btns = value; } } [Obsolete("不再可用的属性")] [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public override Image Image { get; set; } [Obsolete("不再可用的属性")] [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public override ContentAlignment ImageAlign { get; set; } public UCDropDownBtn() { InitializeComponent(); IsShowTips = false; this.lbl.Image=Properties.Resources.ComboBox; this.lbl.ImageAlign = ContentAlignment.MiddleRight; base.BtnClick += UCDropDownBtn_BtnClick; } void UCDropDownBtn_BtnClick(object sender, EventArgs e) { if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false) { if (Btns != null && Btns.Length > 0) { int intRow = 0; int intCom = 1; var p = this.PointToScreen(this.Location); while (true) { int intScreenHeight = Screen.PrimaryScreen.Bounds.Height; if ((p.Y + this.Height + Btns.Length / intCom * 50 < intScreenHeight || p.Y - Btns.Length / intCom * 50 > 0) && (_dropPanelHeight <= 0 ? true : (Btns.Length / intCom * 50 <= _dropPanelHeight))) { intRow = Btns.Length / intCom + (Btns.Length % intCom != 0 ? 1 : 0); break; } intCom++; } UCTimePanel ucTime = new UCTimePanel(); ucTime.IsShowBorder = true; int intWidth = this.Width / intCom; Size size = new Size(intCom * intWidth, intRow * 50); ucTime.Size = size; ucTime.FirstEvent = true; ucTime.SelectSourceEvent += ucTime_SelectSourceEvent; ucTime.Row = intRow; ucTime.Column = intCom; List<KeyValuePair<string, string>> lst = new List<KeyValuePair<string, string>>(); foreach (var item in Btns) { lst.Add(new KeyValuePair<string, string>(item, item)); } ucTime.Source = lst; _frmAnchor = new Forms.FrmAnchor(this, ucTime); _frmAnchor.Load += (a, b) => { (a as Form).Size = size; }; _frmAnchor.Show(this.FindForm()); } } else { _frmAnchor.Close(); } } void ucTime_SelectSourceEvent(object sender, EventArgs e) { if (_frmAnchor != null && !_frmAnchor.IsDisposed && _frmAnchor.Visible) { _frmAnchor.Close(); if (BtnClick != null) { BtnClick(sender.ToString(), e); } } } } }
namespace HZH_Controls.Controls.Btn { partial class UCDropDownBtn { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UCDropDownBtn)); this.SuspendLayout(); // // lbl // this.lbl.Font = new System.Drawing.Font("微软雅黑", 14F); this.lbl.ForeColor = System.Drawing.Color.White; this.lbl.ImageAlign = System.Drawing.ContentAlignment.MiddleRight; this.lbl.ImageList = null; this.lbl.Text = "自定义按钮"; this.lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // UCDropDownBtn // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; this.BtnFont = new System.Drawing.Font("微软雅黑", 14F); this.BtnForeColor = System.Drawing.Color.White; this.ForeColor = System.Drawing.Color.White; this.Image = ((System.Drawing.Image)(resources.GetObject("$this.Image"))); this.ImageAlign = System.Drawing.ContentAlignment.MiddleRight; this.Margin = new System.Windows.Forms.Padding(2); this.Name = "UCDropDownBtn"; this.ResumeLayout(false); } #endregion } }
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
-
winform-下拉框多选
2016-08-15 11:47:35支持下拉框多选、全选 -
WinForm学习03 下拉菜单
2021-05-23 21:48:35控件:combobox 下拉内容设置位置:属性中的 Item 属性 使用的事件位置 逻辑代码 // 当选择下拉菜单中的选项时执行 private void combobox_SelectedIndexChanged(object sender, EventArgs e) { // ... -
DevExpress Winform 常用控件 LookUpEdit(下拉列表视图)(三)
2020-04-26 23:23:49//设置下拉列表大小 this.lookUpEdit1.Properties.PopupFormMinSize = new Size(100, 286); /*ps: 补充说明 this.lookUpEdit1.ItemIndex 等于 - 1 时,表示不存在数据源。 如果 this.lookUpEdit1.Properties.... -
c#Winform自定义控件-目录
2021-08-17 17:19:49入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 如果... -
Winform datagridview中显示下拉框示例
2022-04-29 11:36:52方式一:如下图所示,该方式也是较为简单的一种。 你只需要添加一列类型为DataGridView...其实原理也非常简单,只需要在选择DataGridView的单元格时,判断是不是要显示下拉列表的列,如果是的话,将下拉列... -
WinForm中常用控件
2020-08-21 17:47:02公共控件 一、Button:按钮 按钮是一种非常常见的控件,任何需要在界面点击然后进行下一步操作的行为都可以使用按钮来完成。 使用:QQ的登录按钮 常用属性: Enable:设置按钮是否可以点击,常用于步骤... -
Winform 属性编辑器 propertyGrid示例 下拉列表框
2016-02-01 22:39:35工作中用到的 Winform 属性编辑器 propertyGrid示例 下拉列表框 -
winform dev 网格树形下拉 控件封装
2021-02-26 12:49:32添加一个组件 开始自定义控件 文件结构 Entity Code using System; namespace Test { public class DataDto { public Guid Id { get; set; } public Guid ParentId { get; set; } public -
c# winform combobox下拉内容超长
2022-04-21 15:09:53private void initCjlx() { int maxSize = 0; System.Drawing.Graphics g = CreateGraphics(); for (int i = 0; i < wz_comboBox_collectType.Items.Count; i++) ... wz_comboBox_collectType.Sele... -
.net WinForm用户控件开发--(3)可多选的下拉列表框
2012-07-25 17:54:56这一节给大家演示一个具有多选功能的下拉列表框,其实就是一个文本框和checkboxlist组合实现的用户控件,换个角度来实现自定义控件。 先来看下效果图吧,我会分几个步骤来具体讲解这个文本框的实现。 1.界面...