社区
Delphi
帖子详情
DELPHI中利用TreeView控件建立目录树
太空11
2004-01-05 03:35:49
DELPHI中利用TreeView控件建立目录树
...全文
359
2
打赏
收藏
DELPHI中利用TreeView控件建立目录树
DELPHI中利用TreeView控件建立目录树
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
2 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
popmailzjw
2004-07-28
打赏
举报
回复
收藏一下
shirlley
2004-01-05
打赏
举报
回复
TreeView是一个显示树型结构的控件,通过它能够方便地管理和显示具有层次结构的
信息,是Windows应用程序的基本控件之一。DELPHI虽然具有比较强大的文件管理功能,
提供了多个用于文件管理的标准控件,如DriveComboBox、DirectoryListBox、FileListBox
等,通过设置它们的属性,使其建立起联系,甚至不用编写一行程序,我们就可以实现在
不同的目录之间进行切换,然而这样的目录切换只适用于进行文件的查找定位,而不能方
便地进行目录的浏览,例如我们要从c:\windows目录转到c:\program files目录,就必须返
回到根目录才能进行切换,而不能象Windows资源管理器那样任意地在不同的目录之间进
行浏览与切换。
要实现在不同目录之间任意切换和浏览,还是需要使用TreeView控件,以下程序就利
用DELPHI的TreeView控件来建立目录树。
在该程序中采用的各部件以及界面设计如下图所示:
各部件的主要属性设置如下:
部件 属性属性值
form name
caption form1
‘目录浏览’
drivecommbobox name
visible drivecommbobox1
false
filelistbox name
visible
filetype filelistbox1
false
fddirectory
imagelist name imagelist1
treeview name
images dirtreeview
imagelist1
该程序利用DriveCommboBox控件来获得系统具有的驱动器,并以此作为目录树的最
上层,利用FileListBox控件,通过设置其Filetype属性为fdDirectory,可以获得所需的子
目录,在TreeView控件的OnExpanding事件中将得到的子目录加到该控件的某一节点下。
整个程序的源代码如下:
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, FileCtrl, ComCtrls, ImgList;
type
TForm1 = class(TForm)
DirTreeView: TTreeView;
FileListBox1: TFileListBox;
DriveComboBox1: TDriveComboBox;
ImageList1: TImageList;
procedure FormCreate(Sender: TObject);
procedure DirTreeViewExpanding(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 := DirTreeView.Items.GetFirstNode;
for index := 0 to ItemCount -1 do
begin
ItemStr:= DriveComboBox1.Items[index];
ItemStr:= copy(ItemStr,1,pos(':',ItemStr)) ;
DirNode := DirTreeView.Items.AddChild(FirstNode, ItemStr );
DirNode.HasChildren := true;
DirNode.ImageIndex := 0;
DirNode.SelectedIndex := 1;
end;
end;
procedure TForm1.DirTreeViewExpanding(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 := DirTreeView.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.
程序的运行效果如图所示:我们可以展开目录树中的任何一个节点,并且可以在任意
节点之间切换,就象我们在Windows资源管理器中所作的那样,而不需要逐级回退之后才
能进行切换。
delphi
treeview
加载时 改变字体颜色
delphi
treeview
加载时 改变字体颜色
Delphi
7.0 创建
TreeView
树
控件
菜单.rar
Delphi
实现一个实用的
树
控件
菜单,一个
Treeview
和数据库相结合的简单操作,在生成
Treeview
的过程
中
,需要操作数据库,创建记录currNode的AutoId表识标,创建标签,caption存放各分支的AutoId表识。从运行效果截图来看,这个
控件
的外观样式还是挺漂亮的,从功能上来说,可以支持三级的
树
形子节点展开、折叠,可满足大部分的应用。
将数据库数据添加到
TreeView
控件
中
(
DeLphi
)..rar
将数据库数据添加到
TreeView
控件
中
(
DeLphi
)..rar
delphi
treeview
的使用
delphi
关于
treeview
的使用,动态的创建,删除,修改。是学习
treeview
很有用。
目录
结构的应用。
delphi
用
TreeView
实现文件
树
delphi
用
TreeView
实现文件
树
demo
Delphi
5,927
社区成员
262,931
社区内容
发帖
与我相关
我的任务
Delphi
Delphi 开发及应用
复制链接
扫一扫
分享
社区描述
Delphi 开发及应用
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章