how i can get the index of the checkedlistbox item according to the value+C#+VS 2008+Window application

i want to get the index of the checkedlistbox item according to the value, so i have used
string value="hello";
// but all the items the answer is -1, and i sure that there is a item of value equal hello in the list
int getIndex=CheckedListBox.Items.IndexOf(value);
i read a lot of the internet and many people face this problem.

regards,
MKItaniAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guveraCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Easwaran ParamasivamCommented:
http://visualcsharptutorials.com/2011/03/checkedlistbox-control/

In the checkedList_ItemCheck event you can get the index by event arguments.

One more option there, loop through all the items in the checklistbox and if the value is your String then return the index.


if(myCheckedListBox.Count != 0) 
{  
  string s = "YourSring";    
  for(int i = 0; i <= myCheckedListBox.Count - 1 ; i++)  
  {      
   if (myCheckedListBox[i] == s
    retrun i;
  }
}

Open in new window

0
Kelvin McDanielSr. Developer/ConsultantCommented:
Since this inherits from the ListBox control you should use FindString(+2 overloads) or FindStringExact(+2 overloads).

You can get more information here: http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.aspx .

I just tested this and got the expected result... check the screenshot.
 Screenshot of CheckedListBox and positions of specific items in list.
0
Kelvin McDanielSr. Developer/ConsultantCommented:
Just saw that you'll need to be using the v3.5 framework to use the FindString() method... which I assume you are since you're IDE is VS2008.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.