Forum guidelines
AuthorQuestion
Loop through combo box items
Posted By Sri on 08 Jul 2008

Q1) What is the easy way to loop through the collection of items in the combo box?

Q2) What is the easy way to select a combo box item by value?

Some attempts related to Q2:

private object FindCbItemByValue(ComboBox cb, object val)

{

object oldval = cb.SelectedValue;

cb.SelectedValue = val;

object item = cb.SelectedItem;

cb.SelectedValue = oldval;

return item;

}

 

I do not want this since this fires the SelectedIndexChanged event of the combo box. So, I was thinking I had better loop through the collection. I had greatly appreciate if anyone knowledgeable can address this and guide me in the right direction.

 


Cheers

Sri

AuthorReply
Re: Loop through combo box items
Posted By Satish Kathi on 08 Jul 2008  

You can use "foreach" to loop through the ComboBox items

Here is a small example, hope this will help

private Emp FindCbItemByValueKsk(ComboBox cb, string val)

{

Emp returnObject = null;

foreach (Emp item in comboBox1.Items)

{

if (item.Id == val)

returnObject = item;

}

return returnObject;

}


Satish Kathi
Sri
  • 0
  • 0
Re: Loop through combo box items
Posted By Sri on 08 Jul 2008  

Thanks for the help. I had tried before to run through the combo box items with a for each but I have another small issue there. My combo box is data bound.

What is Emp in that code? I am guessing, a custom class with one field Id? But I am databinding the combo box and want to know how to select from the values in the combo box at anytime. I dont insert Emp or my custom object items into the combo box. I use comboBox.DataSource and set my display member as well as the value member. In that case would I be better of looping the dataasource rather than the combo box? Let me know.


Cheers

Sri

Re: Loop through combo box items
Posted By Satish Kathi on 08 Jul 2008  

Yes Emp is a custom class.

Here is the code,  hope this clarifies

Emp Class

class Emp

    {

        private string _id;

        private string _fname;

        private string _lname;

 

        public Emp()

        { }

 

        public Emp(string id, string fname, string lname)

        {

            _id = id;

            _fname = fname;

            _lname = lname;

        }

 

        public string Id

        {

            get{ return _id; }

            set{_id = value;}

        }

 

        public string FName

        {

            get{return _fname;}

            set{_fname = value;}

        }

 

        public string LName

        {

            get{return _lname;}

            set{_lname = value;}

        }

    }

 

Binding ComboBox

        private void Form1_Load(object sender, EventArgs e)

        {

            List<Emp> emps = new List<Emp>();

            emps.Add(new Emp("1", "F1", "L1"));

            emps.Add(new Emp("2", "F2", "L2"));

            emps.Add(new Emp("3", "F3", "L3"));

            emps.Add(new Emp("4", "F4", "L4"));

            emps.Add(new Emp("5", "F5", "L5"));

 

            BindingSource bs = new BindingSource(emps, "");

           

            comboBox1.DisplayMember = "FName";

            comboBox1.ValueMember = "Id";

            comboBox1.DataSource = bs;

        }

 

private void button2_Click(object sender, EventArgs e)

        {

            Emp eObj = FindCbItemByValueKsk(comboBox1, "3");

        }

 

Method to get the object from the Combo Box

        private Emp FindCbItemByValueKsk(ComboBox cb, string val)

        {

            Emp returnObject = null;

            foreach (Emp item in comboBox1.Items)

            {

                if (item.Id == val)

                    returnObject = item;

            }

            return returnObject;

        }

 

 


Satish Kathi
Re: Loop through combo box items
Posted By Ryan Alford on 11 Jul 2008  
why re-invent the wheel for finding a specific item in a combobox?  The control already has the functionality built in....

int index = comboBox1.FindStringExact("TEST"); // This will find the first item with "TEST" as it's value.
comboBox1.SelectedIndex = index; // this will select the first item that has "TEST" as it's value.

Re: Loop through combo box items
Posted By Satish Kathi on 11 Jul 2008  

What you said is right but it will raise SelectedIndexChanged event.

Sri’s requiremt is not raise the SelectedIndexChanged event.


Satish Kathi
Re: Loop through combo box items
Posted By Ryan Alford on 11 Jul 2008  
the SelectedIndexChange will always be fired if you want to selected an item that is not the first item in the list.

Re: Loop through combo box items
Posted By Satish Kathi on 11 Jul 2008  
In my code above SelectedIndexChanged will not be fired as I am not changing any property in the ComboBox
Satish Kathi
Re: Loop through combo box items
Posted By Ryan Alford on 11 Jul 2008  
I am sorry.  You were answering question 1 and I was answering question 2.  There is no way to select a certain item in a combobox without firing the SelectedIndexChange Event.  However, as you have posted, you can loop through the items in the combobox with ease.

Sri
  • 0
  • 0
Re: Loop through combo box items
Posted By Sri on 11 Jul 2008  

Exactly right Satish! I did that and it worked. Ryan thanks for your involvement. It made things clear and interesting.


Cheers

Sri

jnc
  • 0
  • 0
Re: Loop through combo box items
Posted By jnc on 07 Feb 2011  
Good tip , worked for SilverLight 2½ years later thanks
jnc

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.