listview和treeview的简单用法
listview添加数据.
void __fastcall TForm1::Button2Click(TObject *Sender) //加入数据
{
static int i;
TListItem *listitem;
listitem=ListView1->Items->Add();
listitem->Caption=IntToStr(i);
listitem->SubItems->Add("标号");
listitem->SubItems->Add("姓名");
listitem->SubItems->Add("地址");
i++;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) //查找值为5的Item,
{
TListItem* Item;
Item = ListView1->FindCaption(-1, "5", false, false, false);
int num;
num=ListView1->Items->IndexOf(Item); //这个item的标号。
//可以添加其他的动作
ShowMessage(IntToStr(num));
}
treeview的用法//列举文件目录.
//------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
AnsiString Path=Edit1->Text; //Edit1->Text是文件目录
if(Path.SubString(Path.Length(),1)!="\\")
Path+="\\";
TTreeNode * Node1=TreeView3->Items->Add(NULL,Path);
//Node1->ImageIndex=2; //可以添加节点的图标.
TSearchRec sr;
if (FindFirst(Path+"*.*", faAnyFile, sr) == 0)
{
do
{
if((sr.Attr & faDirectory) && sr.Name!="." && sr.Name!="..")
{
TreeView3->Items->AddChild(Node1,sr.Name);
//Node1->ImageIndex=1; //可以添加节点的图标.
}
} while (FindNext(sr) == 0);
FindClose(sr);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TreeView3Change(TObject *Sender, TTreeNode *Node)
{
TSearchRec sr;
TTreeNode * NodeTmp=Node;
//Node->ImageIndex=2; //可以添加节点的图标.
AnsiString DirName,DirTmp;
DirName=Node->Text;
for(int I=Node->Level ;I>0 ;I--)
{
NodeTmp=NodeTmp->Parent;
DirTmp=NodeTmp->Text;
if(DirTmp.SubString(DirTmp.Length(),1)!="\\")
DirTmp+="\\";
DirName.Insert(DirTmp,0);
}
if(DirName.SubString(DirName.Length(),1)!="\\")
DirName+="\\";
for(int J=0;J<Node->Count;J++)
{
if(Node->Item[J]->Count==0);
{
if (FindFirst(DirName+Node->Item[J]->Text+"\\*.*", faAnyFile, sr) == 0)
{
do
{
if((sr.Attr & faDirectory) && sr.Name!="." && sr.Name!="..")
{
TreeView3->Items->AddChild(Node->Item[J] ,sr.Name);
//Node1->ImageIndex=0; //可以添加节点的图标.
}
} while (FindNext(sr) == 0);
FindClose(sr);
}
}
}
}
//---------------------------------------------------------------------------