[英]How to check user input in combobox in c#
I have a ComboBox that populate from class named class1 then i created object named obj from the class, i use SqlDataReader 我有一个从名为class1的类填充的ComboBox,然后从该类创建了名为obj的对象,我使用SqlDataReader
combobox1.DataSource = obj.Myfunction();
combobox1.ValueMember = "ID";
combobox1.DisplayMember = "Name";
and the user can also write in the ComboBox i want to check if the user input are in the ComboBox items 并且用户还可以在ComboBox中写我要检查用户输入是否在ComboBox项中
if( comboBox1.Items.Contains(comboBox1.Text) )
//do something
else
{
MessageBox.Show("The comboBox1 contains new value");
}
but the result false 但结果错误
i don't want to use another method like SqlDataAdapter 我不想使用其他方法,例如SqlDataAdapter
thanks advanced 谢谢先进
UPDATE 更新
As temporary solution I used this code 作为临时解决方案,我使用了这段代码
int m = combobox1.SelectedIndex;
if (((class1)combobox1.Items[m]).Name.ToString() == combobox1.Text)
{
}
else
{
MessageBox.Show("The comboBox1 contains new value");
}
thank you again 再次感谢你
My Solution is little bit lengthy, but this will solve your problem 我的解决方案有点冗长,但这可以解决您的问题
int count = 0;
for (int i = 0; i < comboBox1.Items.Count; i++)
{
string value = comboBox1.GetItemText(comboBox1.Items[i]);
if (value.Contains(comboBox1.Text))
{
count++;
break;
}
}
if (count > 0)
{
//do something
}
else
{
MessageBox.Show("The comboBox1 contains new value");
}
This code is very simple and easy to understand, But you can use LINQ to shorten your code. 这段代码非常简单易懂,但是您可以使用LINQ来缩短代码。
I'm guessing here, but I suppose the data returned from 我猜这里,但我想从
obj.Myfunction();
(what you're setting as data source) is not a List<string>
and thus, when checking if the inner collection of the combobox contains a string
(returned from comboBox1.Text
), it doesn't contain a string but an object. (您要设置为数据源的)不是
List<string>
,因此,当检查combobox的内部集合是否包含string
(从comboBox1.Text
返回)时,它不包含字符串,而是一个对象。 Since those two are always different ( object
and string
), it is always false. 因为这两个总是不同的(
object
和string
),所以它总是错误的。
Have you tried setting your data source to a list of strings? 您是否尝试过将数据源设置为字符串列表?
Items is an ItemCollection and not list of strings. Items是ItemCollection,而不是字符串列表。 In your case its a collection of ComboboxItem and you need to check its Content property.
在您的情况下,它是ComboboxItem的集合,您需要检查其Content属性。
comboboxId.Items.Cast<ComboBoxItem>().Any(com=> com.Content.Equals("Your string"));
I am assuming you are using List of your class type to bind to combo. 我假设您正在使用您的类类型的列表绑定到组合。
You should validate against the source of the combo box like - 您应针对组合框的来源进行验证,例如-
List<ClassType> cboSource = comboBox1.DataSource as List<ClassType>;
var itemAlreadyThere = cboSource.Any(a => a.Name == comboBox1.Text);
if (itemAlreadyThere)
{
MessageBox.Show("The comboBox1 contains old value");
}
//do something
else
{
MessageBox.Show("The comboBox1 contains new value");
}
As a suggestion, instead of casting the combo data source, use a private variable, populate it, and use it for both binding to combo and for validation
作为建议,不要强制转换组合数据源,而应使用私有变量,然后将其填充并用于绑定到组合和进行验证
I had this type of behaviour a couple of months ago. 几个月前,我有这种行为。
I filled a combobox with class data 我用类数据填充了一个组合框
class MyValue
{
public string ID { get; set; }
public string prop1 { get; set; }
public string prop2 { get; set; }
public int Value { get; set; }
public static List<MyValue> Get()
{
//create a list of MyValues and return it
return new List<MyValue>();
}
}
in the GUI: 在GUI中:
mycombo.ValueMember = "ID";
mycombo.DisplayMember = "prop1";
mycombo.DataSource = MyValue.Get();
afterwards I had the selectedvalue as a property: 之后,我将selectedvalue作为属性:
public MyValue SelectedValue
{
get
{
if(mycombo.SelectedValue is MyValue)
return (MyValue)mycombo.SelectedValue);
else
{
MessageBox.Show(string.Format("{0} as new value", mycombo.SelectedText));
return new MyValue{prop1 = mycombo.SelectedText};
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.