• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

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 :)
 

xbonez

New Member
Joined
Nov 29, 2010
Messages
1,182 (0.22/day)
Location
Philly, PA (US)
System Name Winter
Processor AMD Phenom II x4 965 BE @ 4.0Ghz
Motherboard MSI 790FX-GD70
Cooling Corsair H50 Liquid Cooling
Memory 2 x 2Gb Gskill Ripjaws 1600Mhz (7-7-7-24@1.6V)
Video Card(s) Asus GTX 470 @ Stock (Zalman VF3000 cooler)
Storage 2 x Samsung Spinpoint F3 500GB (RAID 0)
Display(s) Hanns G 28" @ 1920x1200
Case Antec 1200
Audio Device(s) Onboard -- TosLink --> Z5500
Power Supply Corsair 850TX 850W PSU
Software Win 7 64-bit Ultimate
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.
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.08/day)
Location
Cheeseland (Wisconsin, USA)
... 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).
 
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
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...
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,263 (4.35/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.08/day)
Location
Cheeseland (Wisconsin, USA)
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)
 
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
Thanks Kreij :) I will try converting it into a for loop in the morning. It's getting late :)
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.08/day)
Location
Cheeseland (Wisconsin, USA)
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.
 
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
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
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.08/day)
Location
Cheeseland (Wisconsin, USA)
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
 
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
Kreij when i grow up I wanna be just like you :) Thanks mate it worked like a charm!
 
Top