C# 如何限制Combobox添加重复值?
我有一个类,用于向Combobox添加值(一个用于显示,另一个用于隐藏) 使用这个类,我向Combobox添加值C# 如何限制Combobox添加重复值?,c#,C#,我有一个类,用于向Combobox添加值(一个用于显示,另一个用于隐藏) 使用这个类,我向Combobox添加值 cmbServerNo.Items.Add(new ComboBoxItem(strIPAddress, iConnectionID.ToString())); 但是我想用下面的方法限制重复的值 foreach (KeyValuePair<int, Object> ikey in m_lstConnectionID) {
cmbServerNo.Items.Add(new ComboBoxItem(strIPAddress, iConnectionID.ToString()));
但是我想用下面的方法限制重复的值
foreach (KeyValuePair<int, Object> ikey in m_lstConnectionID)
{
if (!cmbServerNo.Items.Contains(strIPAddress))
{
cmbServerNo.Items.Add(new ComboBoxItem(strIPAddress, iConnectionID.ToString()));
}
}
foreach(MlstConnectionId中的KeyValuePair ikey)
{
如果(!cmbServerNo.Items.Contains(strIPAddress))
{
cmbServerNo.Items.Add(新的ComboBoxItem(strIPAddress,iConnectionID.ToString());
}
}
但它可能同时添加了strIpAddress和ConnectionID,所以当我检查它是否包含时,它会失败。
如何解决这个问题?
谢谢您可以使用LINQ的
任何扩展方法:
if (!cmbServerNo.Items.Any(x => x.ToString() == strIPAddress))
{
cmbServerNo.Items.Add(new ComboBoxItem(strIPAddress,
iConnectionID.ToString()));
}
您可以使用LINQ的任何扩展方法:
if (!cmbServerNo.Items.Any(x => x.ToString() == strIPAddress))
{
cmbServerNo.Items.Add(new ComboBoxItem(strIPAddress,
iConnectionID.ToString()));
}
您可以使用HashSet()
HashSet items=newhashset();
foreach(MlstConnectionId中的KeyValuePair ikey)
{
如果(!items.Contains(strIPAddress))
{
项目。添加(地址);
cmbServerNo.Items.Add(新的ComboBoxItem(strIPAddress,iConnectionID.ToString());
}
}
您可以使用哈希集()
HashSet items=newhashset();
foreach(MlstConnectionId中的KeyValuePair ikey)
{
如果(!items.Contains(strIPAddress))
{
项目。添加(地址);
cmbServerNo.Items.Add(新的ComboBoxItem(strIPAddress,iConnectionID.ToString());
}
}
如果要使用列表框或组合框中的Items.Contains
函数,对象需要实现IEquatable
接口
大概是这样的:
public class ComboBoxItem : IEquatable<ComboBoxItem> {
// class stuff
public bool Equals(ComboBoxItem other) {
return (this.ToString() == other.ToString());
}
public override bool Equals(object obj) {
if (obj == null)
return base.Equals(obj);
if (obj is ComboBoxItem)
return this.Equals((ComboBoxItem)obj);
else
return false;
}
public override int GetHashCode() {
return this.ToString().GetHashCode();
}
}
// this should be added:
ComboBoxItem addItem = new ComboBoxItem("test", "test item");
if (!cb.Items.Contains(addItem)) {
cb.Items.Add(addItem);
}
// this should not be added:
ComboBoxItem testItem = new ComboBoxItem("test", "duplicate item");
if (!cb.Items.Contains(testItem)) {
cb.Items.Add(testItem);
}
如果要使用列表框或组合框中的Items.Contains
函数,对象需要实现IEquatable
接口
大概是这样的:
public class ComboBoxItem : IEquatable<ComboBoxItem> {
// class stuff
public bool Equals(ComboBoxItem other) {
return (this.ToString() == other.ToString());
}
public override bool Equals(object obj) {
if (obj == null)
return base.Equals(obj);
if (obj is ComboBoxItem)
return this.Equals((ComboBoxItem)obj);
else
return false;
}
public override int GetHashCode() {
return this.ToString().GetHashCode();
}
}
// this should be added:
ComboBoxItem addItem = new ComboBoxItem("test", "test item");
if (!cb.Items.Contains(addItem)) {
cb.Items.Add(addItem);
}
// this should not be added:
ComboBoxItem testItem = new ComboBoxItem("test", "duplicate item");
if (!cb.Items.Contains(testItem)) {
cb.Items.Add(testItem);
}
您首先维护重复条目是有原因的吗?您首先维护重复条目是有原因的吗?@user662285:您可以在这种情况下使用非泛型。@user662285:您可以在这种情况下使用非泛型。