Click here to Skip to main content
15,306,245 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to check new item adding to the combox is in the same combobox items.

can u plz help me..
i tried following code...

C#
for (int i = 0; i <= cmbdep.Items.Count; i++)
                  {
                      if (cmbdep.Text == cmbdep.Items.ToString())
                      {
                          c = 1;
                          break;
                      }
                      else
                      {
                          c = 0;
                      }
                  }

but not working...
Posted

I am not sure why you think this will work. Why not use foreach to iterate over the items ? Items.ToString() is obviously not using your index. Try Items[i].Text, that would mean you were not checking the same thing over and over, but instead checking each individual item.

Also, why use 'c' instead of a bool ?
   
Comments
Ragi Gopi 2-Jul-11 4:03am
   
thankss...
It all makes no sense, plus a couple of bugs:

You make one iteration more that the number of items; use < instead of <= in the loop ending condition.

Also, cmbdep.Items.ToString() will not return what you expect. You probably wanted an item by index, so use cmbdep.Items[i].ToString().

The calculation if c makes no sense. Do you simply want check up if the text is one of the items? (You don't have to answer. :-))

—SA
   
Comments
Ragi Gopi 2-Jul-11 4:03am
   
thanku...
   
You're welcome.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900