private void comboBox1_DropDown(object sender, EventArgs e) { if (string.IsNullOrEmpty(comboBox1.Text.Trim())) return; for (int i = 0; i < comboBox1.Items.Count; i++) { if (comboBox1.Items[i].ToString() == comboBox1.Text) { MessageBox.Show("输入条目已经存在!"); break; } else { comboBox1.Items.Add(comboBox1.Text); break; } } } 大概写了点应该能满足你的需要
添加之前可以判断一下是否存在 combobox.Items.Contais();
如果要插入的值比较多的话 可以实现封装起来 add要的是objectDisplayMember这个属性是设置选项显示的项 可以将封装起来的类的要显示在选项上 字符串类型的属性 取值得时候 取的item也是object 转成你封装类就可以了封装类 public class a { public a(string n,int v) { this.name=n; this.value=v;
} string name; int Value; }调用时候 combobox.items.add(new a("选项一",1)); combobox.items.add(new a("选项二",2)); combobox.items.add(new a("选项三",3)); combobox.DisplayMember="name";取值时候 a A=combobox.selectedItem as a; messagebox.show(A.value.tostring());
private void comboBox1_DropDown(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(comboBox1.Text.Trim())) return;
for (int i = 0; i < comboBox1.Items.Count; i++)
{
if (comboBox1.Items[i].ToString() == comboBox1.Text)
{
MessageBox.Show("输入条目已经存在!");
break;
}
else
{
comboBox1.Items.Add(comboBox1.Text);
break;
}
}
}
大概写了点应该能满足你的需要
combobox.Items.Contais();
取值得时候 取的item也是object 转成你封装类就可以了封装类
public class a
{
public a(string n,int v)
{
this.name=n;
this.value=v;
}
string name;
int Value;
}调用时候
combobox.items.add(new a("选项一",1));
combobox.items.add(new a("选项二",2));
combobox.items.add(new a("选项三",3));
combobox.DisplayMember="name";取值时候
a A=combobox.selectedItem as a;
messagebox.show(A.value.tostring());
a A=combobox.selectedItem as a;敢问大侠 这句是什么意思?谢啦