treeview_unit.pas
来自「一系列DELPHI的控件如何使用的例子,例子有的比较简单,但是能详细的说明了控件」· PAS 代码 · 共 88 行
PAS
88 行
unit treeview_Unit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, FileCtrl, ComCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
DriveComboBox1: TDriveComboBox;
FileListBox1: TFileListBox;
procedure FormCreate(Sender: TObject);
procedure TreeView1Expanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var firstnode,dirnode:ttreenode;
itemcount,index:integer;
itemstr:string;
begin
itemcount:=DriveComboBox1.Items.Count;
firstnode:=treeview1.Items.GetFirstNode;
for index:=0 to itemcount-1 do
begin
itemstr:=DriveComboBox1.Items[index];
itemstr:=copy(itemstr,1,pos(':',itemstr));
dirnode:=treeview1.Items.AddChild(firstnode,itemstr);
dirnode.HasChildren:=true;
dirnode.SelectedIndex:=1;
end;
end;
procedure TForm1.TreeView1Expanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
var
dirnode:ttreenode;
itemcount,index,level,icount:integer;
itemstr,strpath:string;
begin
if node.Count = 0 then
begin
icount:=0;
level:=node.Level ;
dirnode:=node;
strPath:=node.Text+'\' ;
while level<0 do
begin
strPath:=dirnode.Parent.Text+'\'+strpath;
dirnode:=dirnode.parent;
level :=level -1;
end;
FileListBox1.Clear ;
FileListBox1.Directory := strpath;
ItemCount:= FileListBox1.Items.Count;
for index:=0 to ItemCount -1 do
begin
itemstr:=filelistbox1.items[index];
itemstr:= copy(ItemStr,2,pos(']',ItemStr)-2) ;
if (itemstr<>'.') and (itemstr<>'..') then
begin
DirNode :=TreeView1.Items.AddChild(Node,itemstr );
DirNode.HasChildren :=true;
DirNode.ImageIndex := 0;
DirNode.SelectedIndex := 1;
icount:=icount+1;
end;
if icount = 0 then
Node.HasChildren := false;
end;
end;
end;
end.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?