By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,835 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,835 IT Pros & Developers. It's quick & easy.

CheckedListBox Wierdness

P: n/a
Dim dirs() as string = Directory.GetDirectories(MyPath)
CheckedListBox.DataSource = dirs

For I as Integer = 0 To CheckedListBox.Items.Count - 1
CheckedListBox.SelectedIndex = I

The first line in the For loop (CheckedListBox.SelectedIndex = I) sometimes
throws an exception about the index being out of range. "Index must be
non-negative and less than the number of items in the collection".

Sometimes it doesn't throw an exception, it works fine. I've checked the
values when it does bomb and the CheckedListBox.Items.Count = 562 and I = 0.
Yet it still throws the exception.

I've gotten around it by doing this:

For I as Integer = 0 To CheckedListBox.Items.Count - 1
CheckedListBox.SelectedIndex = I
End Try

Now the program works perfectly fine without any errors. It also gives the
correct results. Any ideas??? Anyone??

Nov 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.