• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

CheckedListBox Problem

Joined
Nov 8, 2008
Messages
779 (0.13/day)
Location
Sydney, Australia
System Name Gearbox || Server
Processor i5 3570K @ 4.0Ghz || E8400 @ Stock 3Ghz
Motherboard Gigabyte Z68XP-UD3 || Gigabyte EP41-UD3L
Cooling Stock || Stock
Memory 8GB G.Skill RipjawX DDR3 @ 1600mhz || 4GB Kingston Value DDR2 800Mhz
Video Card(s) ASUS R9 270X Direct CU II TOP @ 1120/1500 || N/A
Storage Samsung 840 EVO 250GB || 1TB WD Green, 2TB WD Green, 3TB WD Red
Display(s) HP x23 LED 23" Full HD Panel
Case Corsair 200R || Open-Air
Audio Device(s) Audioengine D1 + Logitech Z623/Audio Technica ATH-M50 || N/A
Power Supply Antec EarthWatts Platinum 650 W || Antec Neo Eco 450 W
Software Windows 8.1 Update 3 Pro 64 || Ubuntu Server 14.04 64
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.

  1. Apple
  2. Banana
  3. 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 :)
 
Tried this and it works in C#. Should be very similar for VB.NET as well.

Code:
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.
 
... or if you want to avoid the try/catch
Code:
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).
 
OK, so I'm pretty sure that worked but now I'm getting another error. Here's my code for the button:

Code:
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:
Code:
CheckedListBox1.SetItemChecked(CheckedListBox1.SelectedItem(0), False)
\
is highlighted in yellow. Argh...
 
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
 
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.
Code:
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)
 
Thanks Kreij :) I will try converting it into a for loop in the morning. It's getting late :)
 
In this case you are probably good to go.
However, be aware of a situation like this ..
Code:
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.
Code:
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.
 
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!
Code:
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
 
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.
Code:
For Each CheckedItem in CheckedListBox1.CheckedItems
    Label1.Text = "Copying " & CheckedItem.ToString();
    ...
Next
 
Kreij when i grow up I wanna be just like you :) Thanks mate it worked like a charm!
 
Back
Top