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:"});

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.

Edited 6 Years Ago by Ancient Dragon: n/a

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

This article has been dead for over six months. Start a new discussion instead.