C#(WinForm) ComboBox和ListBox添加项及设置默认选择项
作者:mdxy-dxy 时间:2022-05-13 10:09:49
Web控件DropDownList和WinForm控件ComboBox机制不一样。
ComboBox没有对应的ListItem需要自己写一个:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WinListItem
{
/// <summary>
/// 选择项类,用于ComboBox或者ListBox添加项
/// </summary>
public class ListItem
{
private string id = string.Empty;
private string name = string.Empty;
public ListItem(string sid, string sname)
{
id = sid;
name = sname;
}
public override string ToString()
{
return this.name;
}
public string ID
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}
}
然后可以类似DropDownList添加项:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinListItem
{
public partial class MainFrm : Form
{
public MainFrm()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
ListItem listItem = comboBox1.SelectedItem as ListItem;
MessageBox.Show(listItem.ID + "," + listItem.Name);
}
private void MainFrm_Load(object sender, EventArgs e)
{
//添加项,Web控件DropDownList有对应的ListItem
ListItem listItem0 = new ListItem("0", "选项零");
ListItem listItem1 = new ListItem("1", "选项一");
ListItem listItem2 = new ListItem("2", "选项二");
comboBox1.Items.Add(listItem0);
comboBox1.Items.Add(listItem1);
comboBox1.Items.Add(listItem2);
//设置默认选择项,DropDownList会默认选择第一项。
comboBox1.SelectedIndex = 0;//设置第一项为默认选择项。
comboBox1.SelectedItem = listItem1;//设置指定的项为默认选择项
}
}
}
运行如图:
参考:c#(winform)中ComboBox和ListBox添加项完全解决
刚开始用.net 的winform开发,发现好些控件都很难用,可能是不熟悉的原因吧,这不,一个给ComboBox添加项的问题就搞的我很头疼,我要同时给一个项添加名字和值,怎么都没法加,查了查资料,又自己汇总测试了下,终于全部搞定了,现把完整的方案写下。
用comboBox的数据绑定的方法很简单,建一个数据源,绑定到ComboBox上,然后指定DisplayMember和 ValueMember就可以了。但是感觉好不灵活哦,如果我要在ComboBox上再添加一项,那怎么办?Web里面有ListItem, winform里面怎么没有了?感觉真是不爽,网上找了个方法,自己添加一个ListItem类,然后add到items里面,感觉还不错,有点象web 里面的用法了,可是问题又来了,添加的第一项怎么变成类名了?不是我给它赋的名字,其他项又都没有问题。于是又查到说,“因为combobox的 Item.ADD(一个任意类型的变量),而显示的时候调用的是这个变量的ToString()方法,如果这个类没有重载ToString(),那么显示的结果就是命名空间 + 类名”,于是加上重载的ToString()方法,好了,至此,我终于可以很方便的来给ComboBox和ListBox添加项了。
ListItem item = new ListItem("我是值", "我是名字");
this.lbChoiceRoom.Items.Add(item);
this.lbChoiceRoom.DisplayMember = "Name";
this.lbChoiceRoom.ValueMember = "ID";
标签:ComboBox,ListBox
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java工具类DateUtils实例详解
2022-08-22 00:40:35
java两个integer数据判断相等用==还是equals
2021-06-14 00:46:52
详解java nio中的select和channel
2021-08-09 19:17:39
C#获得程序的根目录以及判断文件是否存在的实例讲解
2022-12-07 18:00:27
SpringBoot自定义加载yml实现方式,附源码解读
2022-01-22 22:39:40
![](https://img.aspxhome.com/file/2023/5/86025_0s.jpg)
深入分析java并发编程中volatile的实现原理
2023-11-23 05:02:25
详解用maven将dubbo工程打成jar包运行
2023-10-20 04:34:42
![](https://img.aspxhome.com/file/2023/1/86861_0s.png)
Java单例模式下的MongoDB数据库操作工具类
2023-11-20 12:55:01
老生常谈Java动态编译(必看篇)
2022-04-02 14:19:38
![](https://img.aspxhome.com/file/2023/8/91748_0s.png)
Android开发应用第一步 安装及配置模拟器Genymotion
2023-05-25 11:18:09
![](https://img.aspxhome.com/file/2023/4/91004_0s.png)
c#处理3种json数据的实例
2023-06-23 19:12:47
Android Studio常用快捷键功能说明
2023-02-20 04:44:17
如何在Unity中检测死循环和卡死
2023-12-18 00:55:17
详解SpringBoot JPA常用注解的使用方法
2023-12-09 17:10:31
![](https://img.aspxhome.com/file/2023/5/72535_0s.png)
java常量字符串过长报错的解决办法以及原因分析
2023-10-18 10:15:25
![](https://img.aspxhome.com/file/2023/2/80922_0s.png)
200行java代码实现2048小游戏
2023-11-29 12:03:57
![](https://img.aspxhome.com/file/2023/3/60693_0s.gif)
Spring4如何自定义@Value功能详解
2021-12-16 06:31:12
深入理解Java IO的flush
2022-01-02 20:05:53
![](https://img.aspxhome.com/file/2023/9/78229_0s.jpg)
C# web.config之<customErrors>节点说明案例详解
2023-07-06 20:20:15
详解Java注解知识点
2021-06-24 18:39:12
![](https://img.aspxhome.com/file/2023/5/60905_0s.png)