1.1 快速上手
-
拖一个
ComboBox到窗体 -
填数据:
comboBox1.Items.AddRange(new object[]{"北京","上海","广州","深圳"});
// 或绑定数据源
comboBox1.DataSource = list; // 任何 IList/BindingList
comboBox1.DisplayMember = "Name"; // 对象属性
comboBox1.ValueMember = "Id"; // 背后值 -
读取选中项:
var txt = comboBox1.Text; // 显示文本
var val = comboBox1.SelectedValue; // 背后值(绑定才有效)
var idx = comboBox1.SelectedIndex; // -1 表示没选
1.2 核心属性速查表
|----------------------|-----------------|------------------------|
| 属性 | 典型值 | 作用 |
| DropDownStyle | DropDownList | 决定用户能否输入文本(见下一节) |
| AutoCompleteSource | CustomSource | 开启自动完成 |
| AutoCompleteMode | SuggestAppend | 自动完成模式 |
| MaxDropDownItems | 8 | 下拉最大可见行数 |
| IntegralHeight | false | 允许部分行高(自绘时有用) |
| Sorted | true | 对 Items 自动排序(绑定数据源时无效) |
1.3 DropDownStyle 三态对比
|----------------|--------|------|----------------|
| 枚举值 | 用户能否输入 | 能否下拉 | 典型场景 |
| DropDown | ✅ | ✅ | 搜索框(带建议) |
| DropDownList | ❌ | ✅ | 只允许选择(最常用) |
| Simple | ✅ | ❌ | 像 ListBox 一直展开 |
1.4 事件大全(常用)
|------------------------|-------|----------------|
| 事件 | 触发时机 | 用途 |
| SelectedIndexChanged | 每次换项 | 联动界面、保存设置 |
| TextChanged | 输入文字时 | 搜索过滤、实时提示 |
| DropDownClosed | 下拉收起 | 延迟加载、清理资源 |
| KeyDown | 按键 | 自定义快捷键(如回车即提交) |
示例:回车直接提交
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
buttonOK.PerformClick();
}
1.5 自动完成(零代码)
设计器里设置:
AutoCompleteSource = CustomSource
AutoCompleteCustomSource.AddRange(new string[]{"北京","北京朝阳","北京海淀"})
AutoCompleteMode = SuggestAppend
运行即可像搜索引擎一样下拉提示。
1.6 绑定对象 + 取值
public class City
{
public int Id { get; set; }
public string Name { get; set; }
}
// 窗体加载
var cities = new List<City>
{
new City{Id=1, Name="北京"},
new City{Id=2, Name="上海"}
};
comboBox1.DataSource = cities;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";
// 读取
City selected = (City)comboBox1.SelectedItem; // 强类型
int id = selected.Id;
1.7 自绘项(图标 + 颜色)
设置 DrawMode = OwnerDrawFixed,处理 DrawItem:
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0) return;
string text = comboBox1.Items[e.Index].ToString();
Color backColor = (text == "上海") ? Color.LightPink : Color.White;
e.Graphics.FillRectangle(new SolidBrush(backColor), e.Bounds);
e.Graphics.DrawString(text, e.Font, Brushes.Black, e.Bounds.Left + 2, e.Bounds.Top + 2);
e.DrawFocusRectangle();
}
效果:每项背景色不同,甚至加图标(用 e.Graphics.DrawImage)。
1.8 异步加载(大数据源)
await Task.Run(() =>
{
var hugeList = GetDataFromDB(); // 耗时
this.Invoke(() =>
{
comboBox1.DataSource = hugeList;
comboBox1.DisplayMember = "Name";
});
});
避免 UI 卡顿。
1.9 常见坑速查
|--------------------------|--------------------------------|------------------------------------------|
| 现象 | 原因 | 解决 |
| 下拉空白 | IntegralHeight=false + 行高<1 | 设 IntegralHeight=true 或调高 ItemHeight |
| SelectedIndex=-1 | 清空后未复位 | 手动赋 SelectedIndex = -1 |
| 绑定后 SelectedValue 为 null | 未设 ValueMember | 指定 ValueMember = "Id" |
| 输入文字不触发事件 | DropDownStyle=DropDownList | 改为 DropDown 即可输入 |
cs
//向集合追加单个成员
comboBox1.Items.Add("正弦信号");
comboBox1.Items.Add("脉冲信号");
//向集合追加数组成员
comboBox1.Items.AddRange(new string[] { "设置", "参数", "OK" });
//选择哪一个
comboBox1.SelectedIndex = 0;
//设定字体
comboBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10,FontStyle.Bold);
// 读取当前选择的项名称
string str3 = comboBox1.SelectedText;
// 获取当前选择的对象
string str2 = comboBox1.SelectedItem.ToString();
// Text 获取的为控件显示的内容
string str = comboBox1.Text;