简体   繁体   中英

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

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

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

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.

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. Since those two are always different ( object and string ), it is always false.

Have you tried setting your data source to a list of strings?

Items is an ItemCollection and not list of strings. In your case its a collection of ComboboxItem and you need to check its Content property.

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:

mycombo.ValueMember = "ID";
mycombo.DisplayMember = "prop1";
mycombo.DataSource = MyValue.Get();

afterwards I had the selectedvalue as a property:

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};
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM