Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1886
  • Last Modified:

Visual C#, find and check a item in CheckedListBox

Hi Friends,

I am developing a Windows based application using .net 2008 with C# and facing a problem while using "FindString" and "FindStringExact" function of CheckedListBox.

The issue is, while using a "For Each" loop to iterate through a string array and to find given string value in CheckedListBox in order to get a index value and then using the same index to check the item in checked list box.

But these functions only work for first item and then always returns -1 even when those items are available in CheckedList box.

Following is the code, well even if this method is not going to work please guide me how to set items in Checkedlistbox checked which matches with given string value.

thanks in advance




string[] aSigs = sigs.Split(new string[] { ","}, StringSplitOptions.RemoveEmptyEntries);
            
            foreach(string str in aSigs)
            {
 
                int i = clbSigs.FindStringExact(str,-1);
                if(i > -1)
                clbSigs.SetItemChecked(i, true);   
            }

Open in new window

0
ArunVashist
Asked:
ArunVashist
1 Solution
 
anarki_jimbelCommented:
Fare enough, thats what the method does:


http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.findstringexact.aspx
"Finds the first item in the ListBox that exactly matches the specified string.  (Inherited from ListBox.)"

you may use another method:
      FindStringExact(String, Int32) where the second argument - start index.

Finds the first item in the ListBox that exactly matches the specified string. The search starts at a specific starting index.

In other words, you need some loop inside your main loop, something like that

            foreach(string str in aSigs)
            {
 
                int i = clbSigs.FindStringExact(str,-1);
                if(i > -1)
                clbSigs.SetItemChecked(i, true);  

              while(i>-1){
                   i = clbSigs.FindStringExact(str,i+1);
                  if(i > -1)
                     clbSigs.SetItemChecked(i, true);  
              }
            }


I didn't test the code.
0
 
ArunVashistAuthor Commented:
Hi Anarki,

Thanks for your response, but i manage it on my own, actually the problem was not "FindString" or "FindStringExact" function but my given string contains trailing space thats why it always retunr -1.


thanks,
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now