主题:treeview编程求助
caols
[专家分:0] 发布于 2006-09-26 11:37:00
我是一个新手,正在学习一个人事管理程序,用treeview控件,许多天也不得要领,请高手帮帮忙!
1.access数据库只有姓名和部门两个数据单元.
2.左边窗口是treeview控件,右边窗口一个edit1:输入姓名 ,一个edit2:输入部门,两个button,一个增加一个删除.
3.treeview控件分三级,一级:公司名称(固定一个名称),二级部门,三级姓名.
当窗口show时treeview第二级展开,显示部门列表.如果数据库里没有部门就不显示.
4.在右边窗口输入姓名和部门,如果此部门一存在,则把此姓名加到treeview中的此部门下,同时此部门展开,焦点集中到刚输入的姓名.
5.如果此部门不存在,增加此部门节点,把姓名再加到此节点下,焦点集中到刚输入的姓名.
6.删除姓名同上,当某部门的最后一个姓名被删除,部门和姓名一起删除.
因水平有限,我编了多少天也搞不定,请高手在百忙之中花半小时,帮我编一下,万分感谢!
我的邮箱:caoliansheng@163.com.
回复列表 (共3个回复)
沙发
xiaohongna [专家分:180] 发布于 2006-09-27 13:11:00
我也做过这样的东西~~我是每次数据集更新的时候都把 treeview.items重新添加一遍~功能是实现了~~可就是觉得太笨了点~~我的那两个相近的字段是在两个表里面的,因为添加以后他们的index就变了~~失去了开始时候的对应关系~所以我就只好重新添加一遍了~~
谁有好的方法了也教教俺
板凳
keyiwangyou [专家分:80] 发布于 2006-09-29 09:12:00
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, ComCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
procedure AddClass(AId:integer;FatherNode:TTreeNode);//添加分类过程
procedure AddDataToDB(CurrNode,FatherNode:TTreeNode);//添加实际数据到数据库
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.AddClass(AId: integer;FatherNode:TTreeNode);
var
QryTmp:TADOQuery;
myNode:TTreeNode;
myLabel:TLabel;
begin
QryTmp:=TADOQuery.Create(self);
QryTmp.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+extractfilepath(application.ExeName)+'test.mdb;Persist Security Info=False';
QryTmp.SQL.Add('select * from tb1');
QryTmp.SQL.Add('where FatherId='+inttostr(AId));
QryTmp.Open;
while not QryTmp.Eof do
begin
myNode:=Treeview1.Items.AddChild(FatherNode,QryTmp.fieldbyname('CName').AsString);
//创建标签,caption存放各分支的AutoId表识
myLabel:=TLabel.Create(self);
myLabel.Visible:=false;
myLabel.Caption:=QryTmp.fieldbyname('AutoId').AsString;
myNode.Data:=myLabel;
AddClass(QryTmp.fieldbyname('AutoId').AsInteger,myNode); //递归调用过程
QryTmp.Next;
end;
QryTmp.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
AddClass(0,nil);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
strName:string;
myNode:TTreeNode;
begin
strName:=inputbox('新增公司','请输入分类名称: ','');
strName:=trim(strName);
if strName='' then
exit;
myNode:=treeview1.Items.Add(treeview1.Selected,strName);
if assigned(treeview1.Selected) then
AddDataToDB(myNode,treeview1.Selected.Parent)
else
AddDataToDB(myNode,nil);
myNode.selected:=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
strName:string;
myNode:TTreeNode;
begin
strName:=inputbox('新增部门','请输入分类名称: ','');
strName:=trim(strName);
if strName='' then
exit;
myNode:=treeview1.Items.AddChild(treeview1.Selected,strName);
AddDataToDB(myNode,treeview1.Selected);
myNode.selected:=true;
end;
procedure TForm1.AddDataToDB(CurrNode,FatherNode: TTreeNode);
var
myLabel:TLabel;
QryTmp:TADOQuery;
AId:integer;
begin
if not assigned(FatherNode) then
AId:=0
else if not assigned(FatherNode.Data) then
AId:=0
else
AId:=strtoint(TLabel(FatherNode.Data).caption);
QryTmp:=TADOQuery.Create(self);
QryTmp.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+extractfilepath(application.ExeName)+'test.mdb;Persist Security Info=False';
QryTmp.SQL.Add('select * from tb1');
QryTmp.SQL.Add('where FatherId='+inttostr(AId));
QryTmp.Open;
QryTmp.Append;
QryTmp['FatherId']:=AId;
QryTmp['CName']:=CurrNode.Text;
QryTmp.Post;
//创建记录currNode的AutoId表识标
myLabel:=TLabel.Create(self);
myLabel.Visible:=False;
MyLabel.Caption:=QryTmp.fieldbyname('AutoId').AsString;
CurrNode.Data:=myLabel;
QryTmp.Free;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
QryTmp:TADOQuery;
begin
if not assigned(treeview1.Selected) then
exit;
if application.MessageBox('是否删除分类及下级分类?','提示',mb_yesno+mb_iconquestion)=idno then
exit;
//删除下级别分类
QryTmp:=TADOQuery.Create(self);
QryTmp.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+extractfilepath(application.ExeName)+'test.mdb;Persist Security Info=False';
QryTmp.SQL.Add('delete * from tb1');
QryTmp.SQL.Add('where FatherId='+TLabel(Treeview1.Selected.data).Caption);
QryTmp.ExecSQL;
QryTmp.SQL.Clear;
QryTmp.SQL.Add('delete * from tb1');
QryTmp.SQL.Add('where AutoId='+TLabel(Treeview1.Selected.data).Caption);
QryTmp.ExecSQL;
Treeview1.Selected.Delete;
end;
procedure TForm1.Button4Click(Sender: TObject);
var
QryTmp:TADOQuery;
strName:string;
begin
if not assigned(treeview1.Selected) then
exit;
strName:=inputbox('修改','请输入新的名称: ','');
strName:=trim(strName);
if strName='' then
exit;
//删除下级别分类
QryTmp:=TADOQuery.Create(self);
QryTmp.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+extractfilepath(application.ExeName)+'test.mdb;Persist Security Info=False';
QryTmp.SQL.Add('update tb1 set CName='+''''+strName+'''');
QryTmp.SQL.Add('where AutoId='+TLabel(Treeview1.Selected.data).Caption);
QryTmp.ExecSQL;
Treeview1.Selected.Text:=strName;
end;
end.
3 楼
keyiwangyou [专家分:80] 发布于 2006-09-29 09:19:00
给你以前的一段代码,你建个新工程,在窗体上加四个按钮,加上上面的代码,再根据你的具体要求,就可以实现了
我来回复