荷
荷塘新月
Unregistered / Unconfirmed
GUEST, unregistred user!
这个控件是根据 一生中最爱 大侠的DBTREEVIEW控件改写。 虽然改的不好,但是挺实用。希望大家能够喜欢。
要求表结构中至少有有两个字段,并且在DATASET中必需为第一个和第二个字段,且第一个字段为CHAR或者为VARCHAR类型。
例:在某个单位设置中用TREEVIEW分级显示。设置单位编号为6位,2位一级,可以这样做:
ADOQUERY1.CLOSE;
ADOQUERY1.SQL.TEXT:='SELECT DWBH,DWMC FROM DWMCB ORDER BY DWBH';
ADOQUERY1.OPEN;
HWKDBTREE1.DATASET:=ADOQUERY1;
HWKDBTREE1.ACTIVE:=TRUE;
如果想取选中的单位编号,直接取KEYSTRING就可以了。
unit hwkdbtree;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, ComCtrls,db,Graphics;
type
Thwkdbtree = class(TTreeView)
private
FActive:boolean;
FKeyNum:smallint; //每节的长度
FDataSet:TDataSet;
FKeyString:string;
KeyTree:TSTRINGLIST;
FFont:TFont;
procedure SetDataSource(InVal:TDataSet);
procedure SetActive(InVal:boolean);
procedure FillTree(tv1:ttreenode;bh,mch:string);
procedure ActiveTree;
procedure WMAfterClick(var msg:TWMMouse);message MK_LBUTTON;//处理鼠标点击消息
protected
public
constructor Create(AOwner: TComponent);override;
destructor Destroy;override;
function GetKey:string;//获取编号
published
property Active:boolean read FActive write setactive;
property KeyNum:smallint read FKeyNum write FKeyNum;
property DataSet:TDataSet read FDataSet Write SetDatasource;
property KeyString:string read GetKey Write FKeyString;
property Font:TFont read FFont write FFont;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [Thwkdbtree]);
end;
constructor THwkDBTree.Create(AOwner: TComponent);
begin
inherited;
keytree:=tstringlist.Create;
FActive:=false;
Fkeynum:=2;
FFont:=TFont.Create;
FFont.Name:='宋体';
FFont.Size:=10;
end;
destructor THwkDBTree.Destroy;
begin
keytree.Free;
FFont.Free;
inherited;
end;
procedure THwkDBTree.WMAfterClick(var msg:TWMMouse);
begin
if msg.Msg = MK_LBUTTON then GetKey();
inherited;
end;
procedure THwkDBTree.SetDataSource(InVal:TDataSet);
begin
if (InVal<>nil) and (FDataSet<>InVal) then
begin
FDataSet:=InVal;
if FActive=true then Activetree();
end;
end;
procedure THwkDBTree.FillTree(tv1:ttreenode;bh,mch:string);
begin
if tv1<>nil then self.Selected:=Items.AddChild(tv1,mch) else self.Selected:=Items.Add(tv1,mch);
keytree.Insert(self.Selected.AbsoluteIndex,bh);
end;
procedure THwkDBTree.ActiveTree;
var p1:ttreenode;
currentlen,parentlen:smallint;
s:string;
begin
try
if fDataSet.Active=false then fDataSet.Open;
parentlen:=0;
self.Items.Clear;
self.Items.BeginUpdate;
fDataSet.First;
while not fDataSet.Eof do
begin
s:=trim(fDataSet.Fields[0].AsString);
currentlen:=length(s);
p1:=self.Selected;
if p1<>nil then parentlen:=length(keytree.Strings[self.Selected.AbsoluteIndex]);
while (currentlen<=parentlen) do
begin
currentlen:=currentlen+Fkeynum;
p1:=p1.Parent;
end;
filltree(p1,s,trim(fDataSet.Fields[1].AsString));
fDataSet.Next;
end;
self.Items.EndUpdate;
self.FullCollapse;
if self.Items.Count>0 then self.Selected:=self.Items[0];
self.Selected.Expand(true);
self.Refresh;
except
on e:exception do
begin
self.Items.EndUpdate;
messagebox(handle,pchar('数据处理失败!'+#13+#13+e.Message),'错误信息',16);
end;
end;
end;
procedure THwkDBTree.SetActive(InVal:Boolean);
begin
FActive:=inval;
self.Items.Clear;
if InVal=false then exit;
if FDataSet=nil then
begin
FActive:=false;
exit;
end;
activetree;
end;
function THwkDBTree.getkey:string;
begin
result:='';
if self.Items.Count>0 then
result:=keytree.strings[self.Selected.AbsoluteIndex];
end;
end.
要求表结构中至少有有两个字段,并且在DATASET中必需为第一个和第二个字段,且第一个字段为CHAR或者为VARCHAR类型。
例:在某个单位设置中用TREEVIEW分级显示。设置单位编号为6位,2位一级,可以这样做:
ADOQUERY1.CLOSE;
ADOQUERY1.SQL.TEXT:='SELECT DWBH,DWMC FROM DWMCB ORDER BY DWBH';
ADOQUERY1.OPEN;
HWKDBTREE1.DATASET:=ADOQUERY1;
HWKDBTREE1.ACTIVE:=TRUE;
如果想取选中的单位编号,直接取KEYSTRING就可以了。
unit hwkdbtree;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, ComCtrls,db,Graphics;
type
Thwkdbtree = class(TTreeView)
private
FActive:boolean;
FKeyNum:smallint; //每节的长度
FDataSet:TDataSet;
FKeyString:string;
KeyTree:TSTRINGLIST;
FFont:TFont;
procedure SetDataSource(InVal:TDataSet);
procedure SetActive(InVal:boolean);
procedure FillTree(tv1:ttreenode;bh,mch:string);
procedure ActiveTree;
procedure WMAfterClick(var msg:TWMMouse);message MK_LBUTTON;//处理鼠标点击消息
protected
public
constructor Create(AOwner: TComponent);override;
destructor Destroy;override;
function GetKey:string;//获取编号
published
property Active:boolean read FActive write setactive;
property KeyNum:smallint read FKeyNum write FKeyNum;
property DataSet:TDataSet read FDataSet Write SetDatasource;
property KeyString:string read GetKey Write FKeyString;
property Font:TFont read FFont write FFont;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [Thwkdbtree]);
end;
constructor THwkDBTree.Create(AOwner: TComponent);
begin
inherited;
keytree:=tstringlist.Create;
FActive:=false;
Fkeynum:=2;
FFont:=TFont.Create;
FFont.Name:='宋体';
FFont.Size:=10;
end;
destructor THwkDBTree.Destroy;
begin
keytree.Free;
FFont.Free;
inherited;
end;
procedure THwkDBTree.WMAfterClick(var msg:TWMMouse);
begin
if msg.Msg = MK_LBUTTON then GetKey();
inherited;
end;
procedure THwkDBTree.SetDataSource(InVal:TDataSet);
begin
if (InVal<>nil) and (FDataSet<>InVal) then
begin
FDataSet:=InVal;
if FActive=true then Activetree();
end;
end;
procedure THwkDBTree.FillTree(tv1:ttreenode;bh,mch:string);
begin
if tv1<>nil then self.Selected:=Items.AddChild(tv1,mch) else self.Selected:=Items.Add(tv1,mch);
keytree.Insert(self.Selected.AbsoluteIndex,bh);
end;
procedure THwkDBTree.ActiveTree;
var p1:ttreenode;
currentlen,parentlen:smallint;
s:string;
begin
try
if fDataSet.Active=false then fDataSet.Open;
parentlen:=0;
self.Items.Clear;
self.Items.BeginUpdate;
fDataSet.First;
while not fDataSet.Eof do
begin
s:=trim(fDataSet.Fields[0].AsString);
currentlen:=length(s);
p1:=self.Selected;
if p1<>nil then parentlen:=length(keytree.Strings[self.Selected.AbsoluteIndex]);
while (currentlen<=parentlen) do
begin
currentlen:=currentlen+Fkeynum;
p1:=p1.Parent;
end;
filltree(p1,s,trim(fDataSet.Fields[1].AsString));
fDataSet.Next;
end;
self.Items.EndUpdate;
self.FullCollapse;
if self.Items.Count>0 then self.Selected:=self.Items[0];
self.Selected.Expand(true);
self.Refresh;
except
on e:exception do
begin
self.Items.EndUpdate;
messagebox(handle,pchar('数据处理失败!'+#13+#13+e.Message),'错误信息',16);
end;
end;
end;
procedure THwkDBTree.SetActive(InVal:Boolean);
begin
FActive:=inval;
self.Items.Clear;
if InVal=false then exit;
if FDataSet=nil then
begin
FActive:=false;
exit;
end;
activetree;
end;
function THwkDBTree.getkey:string;
begin
result:='';
if self.Items.Count>0 then
result:=keytree.strings[self.Selected.AbsoluteIndex];
end;
end.