Как в treeview динамически создавать список? Хочу написать программу типа проводника, но незнаю как правильно сформировать динамическое создание списка.
System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
this->listView1->FullRowSelect = true;
array<DriveInfo^>^ driver = DriveInfo::GetDrives();
for each( DriveInfo^ drinfo in driver )
{
String^ str = drinfo->Name;
array<wchar_t>^ wch = gcnew array<wchar_t>(999);
for ( int i = 0; i < str->Length; i++)
wch[i] = str[i];
str = gcnew String( wch, 0, str->Length - 1); //получаю букву диска
DirectoryInfo^ dire = gcnew DirectoryInfo(drinfo->Name);
System::Windows::Forms::TreeNode^ node = gcnew System::Windows::Forms::TreeNode;
array<DirectoryInfo^>^ dir = dire->GetDirectories();
node->Text = str;
for each( DirectoryInfo^ di in dir )
{
node->Nodes->Add( di->Name );
}
this->treeView1->Nodes->Add(node);
}
this->label2->Text = L"";
}
System::Void treeView1_AfterExpand(System::Object^ sender, System::Windows::Forms::TreeViewEventArgs^ e)
{
e->Node->Nodes->Clear();
DirectoryInfo^ dirinf;
dirinf = gcnew DirectoryInfo(e->Node->FullPath + L"\\");
array<DirectoryInfo^>^ ardir = dirinf->GetDirectories();
for ( int i = 0; i < ardir->Length; i++)
{
DirectoryInfo^ dirinff = gcnew DirectoryInfo(ardir[i]->FullName + L"\\");
if ( dirinff->Attributes != System::IO::FileAttributes::Directory)
{
e->Node->Nodes->Add( ardir[i]->Name);
continue;
}
array<DirectoryInfo^>^ aardir = dirinff->GetDirectories();
TreeNode^ nod = gcnew TreeNode();
if ( aardir->Length == 0 )
{
e->Node->Nodes->Add( ardir[i]->Name);
continue;
}
else
{
nod->Text = ardir[i]->Name;
nod->Nodes->Add(aardir[0]->Name);
e->Node->Nodes->Add(nod);
}
}
}