# CheckedListBox Problem



## <<Onafets>> (Aug 12, 2011)

Hey guys,
this should be a simple solution but i can't find someone with the same problem on the net.

I want to get the first checked item in my checked listbox in to a string.


Apple
Banana
Orange

So {Checked list box item 0} to string which would mean if i sent the string to a labels text it would say "Apple"

Hope you understand 

This is in VB.net btw


----------



## xbonez (Aug 13, 2011)

Tried this and it works in C#. Should be very similar for VB.NET as well.


```
try
            {
                textBox1.AppendText(clbTest.CheckedItems[0].ToString());
            }
            catch (IndexOutOfRangeException)
            {
                textBox1.AppendText("No item selected");
            }
```

Basically, the CheckedListbox has a property CheckedItems which returns a collection of all checked items. The first checked item would be in index 0. ToString() method works just fine to convert it to a string.

Let me know if you need more help understanding.


----------



## Kreij (Aug 13, 2011)

... or if you want to avoid the try/catch

```
if (checkedListBox.CheckedItems.Count > 0)
{
    label.Text = checkedListBox.CheckedItems[0].ToString();
}
```

You can also use the CheckedIndices method if you want each index of the checked items.
This is usually used if you have the ListBox bound to some type of collection (like a List<T> or array).


----------



## <<Onafets>> (Aug 15, 2011)

OK, so I'm pretty sure that worked but now I'm getting another error. Here's my code for the button:


```
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Do Until CheckedListBox1.CheckedIndices.Count = 0
            first = CheckedListBox1.CheckedItems(0).ToString()
            My.Computer.FileSystem.CopyDirectory(saves & first, "C:\Backups\")
            CheckedListBox1.SetItemChecked(CheckedListBox1.SelectedItem(0), False)
        Loop

    End Sub
```

I'm getting: No default member found for type 'DirectoryInfo'. And the last line:

```
CheckedListBox1.SetItemChecked(CheckedListBox1.SelectedItem(0), False)
```
\
is highlighted in yellow. Argh...


----------



## FordGT90Concept (Aug 15, 2011)

That error seems completely unrelated to what you are doing.  Why are you trying to change the the check?  I would use CheckedListBox1.SelectedItem(0).Checked = False


----------



## Kreij (Aug 16, 2011)

Just a tip ...
It's usually not a good idea to modify a value inside a loop, that changes what the loop is using to determine its state (in this case checked items count).
It may work fine in this case, but it is a good habit not to do that as in some cases it will break things badly and be hard to debug.
A for-loop is usually a better way of going about it.

```
for (int i = 0; i < CheckedListBox1.CheckedItems.count; i++)
{
    first = CheckedListBox1.CheckedItem(i).ToString();
    ....
    CheckedListBox.SelectedItem(i).Checked = false;
}
```

As fo your DirectoryInfo error, I'm not sure.
Does "saves & first" create a valid directory name?
Do you need to backslash the backslashes in the string? "C:\\Backups\\", so it interprets the directory delimiters correctly?
(I'm not that familiar with VB. In C# it would be @"C:\Backups\", if you only want to use sing;e backslashes in a directoy path)


----------



## <<Onafets>> (Aug 16, 2011)

Thanks Kreij  I will try converting it into a for loop in the morning. It's getting late


----------



## Kreij (Aug 16, 2011)

In this case you are probably good to go.
However, be aware of a situation like this ..

```
List<string> myList = new List<string>{"one", "two", "three"};

foreach (string s in myList)
{
   if (s != "two") myList.Remove(s);
}
```

Not good. lol

It's even safer to use something like.

```
int Count = CheckedBoxList.SelectedItems.Count;
for (int i = 0; i < Count; i++)
{
    ...
}
```

That way you know that the integer variable "Count" will not change on manipulation of the SelectedItems collection.


----------



## <<Onafets>> (Aug 22, 2011)

Ok so I converted i to a for loop but it gets stuck after the first item. No errors or anything and the program can be used (buttons work) but it just won't progress...argh!

```
For Each CheckedItem In CheckedListBox1.CheckedItems
            first = CheckedListBox1.CheckedItems(0).ToString
            Label1.Text = "Copying " & first
            If My.Computer.FileSystem.DirectoryExists("C:\Backups\" & first & "\") = False Then
                My.Computer.FileSystem.CreateDirectory("C:\Backups\" & first & "\")
            End If
            My.Computer.FileSystem.CopyDirectory(saves & first & "\", "C:\Backups\" & first & "\", True)
            ProgressBar1.Value = +10
        Next
```


----------



## Kreij (Aug 22, 2011)

Using the ForEach loop you are taking one CheckedItem at a time as you enumerate through the list of them. You no longer need the "first" variable.

```
For Each CheckedItem in CheckedListBox1.CheckedItems
    Label1.Text = "Copying " & CheckedItem.ToString();
    ...
Next
```


----------



## <<Onafets>> (Aug 23, 2011)

Kreij when i grow up I wanna be just like you  Thanks mate it worked like a charm!


----------

