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

CheckedListBox Wierdness

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

For I as Integer = 0 To CheckedListBox.Items.Count - 1
CheckedListBox.SelectedIndex = I
DoMySub(CheckedListBox.SelectedItem)
Next

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
Try
CheckedListBox.SelectedIndex = I
Catch
End Try
DoMySub(CheckedListBox.SelectedItem)
Next

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

Thanks.
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.