954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to populate checkedListBox dynamically?

I'm using VC++ 10.0 Express to create an app that will find all visible drives (C:, USB drives, Memory Sticks, Lettered network drives, etc.) and populate a checked list box with those available drive letters. The app currently is loading the prefab list of A: through Z:, with the eventual option of disabling the unavailable drive letters in the checkedListBox. This current option , however, is an imperfect jerry-rig.
How do I load the checkedListBox dynamically using a separate function to find the available drives? Thanks in advance.

this->checkedListBox1->AccessibleName = L"checkedBoxDrive";
			this->checkedListBox1->FormattingEnabled = true;
			this->checkedListBox1->Items->AddRange
				(gcnew cli::array< System::Object^  >(26) {L"A:", L"B:", L"C:", L"D:", L"E:", L"F:", 
				L"G:", L"H:", L"I:", L"J:", L"K:", L"L:", L"M:", L"N:", L"O:", L"P:", L"Q:", L"R:", 
				L"S:", L"T:", L"U:", L"V:", L"W:", L"X:", L"Y:", L"Z:"});
pstrohma
Newbie Poster
7 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I finally got this worked out (I'm newbe to Windows Forms).

1. Create Form2 with checkedListBox, or you may already have it on another form. Add this code in Form2.cpp. The form designer doesn't like having loops in the *.h file so I had to put the code in *.cpp file.

#include "StdAfx.h"
#include "Form2.h"

using namespace System::IO;
namespace MyApplication { // Change this to the name of the application
    void Form2::GetMyDrives()
    {
            array<DriveInfo^>^ allDrives = DriveInfo::GetDrives();
            for each(DriveInfo^ d in allDrives)
            {
                this->checkedListBox1->Items->Add(d->Name);
            }
    }
};


2. Call the above function from the InitializeComponent() method in Form2.h, after other initialization for checkListBox1 component.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

The getDriveInfo function passed unit test. The checkListBox initialization appears to be spot on. Now implementing. Words of thanks are insufficient. More to come.

pstrohma
Newbie Poster
7 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: