winform中为ComboBox控件添加“请选择”或“全部”选项

本文介绍如何在Winform的ComboBox控件中添加一个默认选项,如“请选择”或“全部”。通过修改数据源的方式实现,包括List泛型集合和DataTable两种常见数据源的处理方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Winform中的下拉列表控件ComboBox,在使用DataSource属性赋数据源后,想添加“请选择”或“全部”这样一个选项,比WEB中要麻烦一些,经过测试,我们只能在数据源上做文章,即需要在数据源中添加这样一项。
一般开发中,最常用的数据源有两种,一是List泛型集合,二是DataTable。
示例代码如下:
一:数据源是List集合:
List list = GetList() as List; //GetList是一个返回IList的方法
list.Insert(0,new BookType(“0”,”请选择”));//如果实体对象有多个属性,可以在实例化该对象后,只为用到的两个属性赋值
comboBox1.DataSource = list;
comboBox1.DisplayMember = “Typename”;
comboBox1.ValueMember = “typeId”;
二:数据源是DataTable:
DataTable dt=GetDataTable();
DataRow dr = dt.NewRow();
dr[“typeID”] = “0”;
dr[“typeName”] = “请选择”;
dt.Rows.InsertAt(dr, 0);
cbxType.DataSource = dt;
cbxType.DisplayMember = “typeName”;
cbxType.ValueMember = “typeID”;

Winform combobox默认情况下是不支持触屏上下滑动的,但可以通过自定义控件来实现这个功能。 一种常见的方法是使用自定义的控件去替代原生的Winform combobox控件。这个自定义控件可以继承自ComboBox,并重写其OnDropDown方法,以便在下拉菜单展开时添加一个滚动条。 具体步骤如下: 1. 创建一个新的Winform控件,取名为CustomComboBox。 2. 继承自ComboBox,并重写OnDropDown方法,以便在下拉菜单展开时添加一个滚动条。 ``` public class CustomComboBox : ComboBox { protected override void OnDropDown(EventArgs e) { base.OnDropDown(e); var handle = SendMessage(this.Handle, CB_GETCOMBOBOXINFO, IntPtr.Zero, IntPtr.Zero); var info = (COMBOBOXINFO)Marshal.PtrToStructure(handle, typeof(COMBOBOXINFO)); var listBoxHandle = info.hwndList; SetWindowLong(listBoxHandle, GWL_STYLE, GetWindowLong(listBoxHandle, GWL_STYLE) | LBS_NOTIFY | WS_VSCROLL); } private const int CB_GETCOMBOBOXINFO = 0x164; private const int GWL_STYLE = -16; private const int LBS_NOTIFY = 0x00000001; private const int WS_VSCROLL = 0x00200000; [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", SetLastError = true)] private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [StructLayout(LayoutKind.Sequential)] private struct COMBOBOXINFO { public int cbSize; public RECT rcItem; public RECT rcButton; public int stateButton; public IntPtr hwndCombo; public IntPtr hwndEdit; public IntPtr hwndList; } [StructLayout(LayoutKind.Sequential)] private struct RECT { public int left, top, right, bottom; } } ``` 3. 在Form中使用CustomComboBox,然后在TouchDown事件中记录当前触屏的坐标。 ``` public partial class Form1 : Form { private Point _startPoint; public Form1() { InitializeComponent(); } private void customComboBox1_TouchDown(object sender, TouchEventArgs e) { _startPoint = e.Location; } } ``` 4. 在TouchMove事件中计算当前触屏滑动的距离,然后根据距离调整下拉菜单的位置。 ``` private void customComboBox1_TouchMove(object sender, TouchEventArgs e) { var distance = e.Location.Y - _startPoint.Y; var dropDownHeight = customComboBox1.DropDownHeight; if (distance > 0) { customComboBox1.DropDownHeight = Math.Min(dropDownHeight + distance, customComboBox1.Items.Count * customComboBox1.ItemHeight); } else { customComboBox1.DropDownHeight = Math.Max(dropDownHeight + distance, customComboBox1.ItemHeight); } } ``` 5. 最后,在Form的Load事件中为CustomComboBox的TouchDown和TouchMove事件添加处理程序。 ``` private void Form1_Load(object sender, EventArgs e) { customComboBox1.TouchDown += customComboBox1_TouchDown; customComboBox1.TouchMove += customComboBox1_TouchMove; } ``` 这样,就可以在Winform中实现触屏上下滑动combobox的下拉部分了。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值