📄 dbtreeview.pas
字号:
//*****************************************************************************
{* unit TDbtreeView d:/dbtreeview/
demo d:/dbtreeview/demo/
设计:农武宁
时间: 2004-8-18
控件名:数据感知treeview;
功 能:(1)可以将数据源与treeview一绑定;
设置datasource:数据源,dataField:显视字段,
mainDatashourc:数据源,dataIDfield,排序字段;
在数据库中排序分层的dataIDfield应以:
01,
0101,
0102,
02
0201
进行设置;
(2)可以对相应的数据库进行增加删除;上移,下移等操作;
提供了相应的接口;
(3)可以选择checkbox功能;通过设置相关的属性:
showcheckBox:=true可以对选种的节点进行设置true or flase;
相关事件;oncheckbox,
*}
//******************************************************************************
unit DBtreeView;
interface
uses
Dialogs,Windows,StrUtils, Messages, SysUtils, Classes, Controls, ComCtrls,DBCtrls,DB,Forms,StdCtrls,CommCtrl;
type
TDBtreeView = class(TCustomTreeView)
private
{ Private declarations }
FShowCheckBox:boolean;
OldState :Boolean;
FDataLink: TFieldDataLink;
fDatasource:Tdatasource;
FDataIdField:string;
FTreeCodeFormat:string; //编码格式为 XX XX XX
FTreeMaxLevel:integer; //最大层数;
FTreeRootText:string; //根结点标题;
Fonnodechecked:tnotifyEvent;
FonnodeUnchecked:tnotifyevent;
FOnChange: TTVChangedEvent;
Fversion:string;
fCheckFieldEnable:boolean;
fcheckfield:string;
fcheckFieldvalue:string;
function getFversion:string;
function GetDataField: string;
function GetDataSource: TDataSource;
procedure SetDataField(const Value: String);
procedure SetDataSource(const Value: TDataSource);
function GetDataIDField: string;
procedure SetDataIDField(const Value: string);
function GetTreeCodeFormat: string;
function GetTreeMaxLevel: integer;
function getTreeRootText: string;
procedure settreeMaxLevel(const Value: integer);
procedure setTreecodeFormat(const Value: string);
procedure setTreeRootText(const Value: string);
protected
{ Protected declarations }
procedure Notification(AComponent: TComponent;Operation:TOperation);override; //通知;
procedure CreateParams(var Params: TCreateParams); override;
procedure Change(Node: TTreeNode); override;
PROCEDURE RecurseChildren(Node: TTreeNode;
NodeChecked: Boolean);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Click; override;
procedure loadData;
procedure setVersion(str:string);
procedure setCheckFieldEnable(b:boolean);
function getCheckFieldEnable:boolean;
function GetNodeLevel(sFormat,sCode:string):integer;//获得节点层数
function getFcheckfield:string;
procedure setfcheckfield(str:string);
function getFcheckfieldvalue:string;
procedure setFcheckFieldvalue(str:string);
function GetTreeDM(CurNode:TTreeNode):string;//获得当前节点的编码
function IsNodeChecked(Node :TTreeNode) :Boolean; //获得当前节点的checked值;
procedure SetNodeChecked(Node :TTreeNode; Checked :Boolean);//设置当前节点的checked值;
// function GetLevelMaxId(Node:TTreeNOde):string;//取得当前层的最大ID;
published
{ Published declarations }
property DataIdField:string read GetDataIDField write SetDataIDField;
property DataField: String read GetDataField write SetDataField;
property DataSource: TDataSource read getDataSource write setDataSource;
property maindatasource:tdatasource read fdatasource write fdatasource;
property TreeCodeFormat: string read GetTreeCodeFormat write setTreecodeFormat;//编码格式为 XX XX XX
property TreeMaxLevel :integer read GetTreeMaxLevel write setTReeMaxLevel ; //最大编码层次
property TreeRootText :string read getTreeRootText write setTreeRootText;//树根结点名称
property ShowCheckBox:boolean read FShowCheckBox write FShowCheckBox;
property Version:string read GetFversion write setversion;
property Checkfield:string read GetFcheckfield write setFcheckfield;
property CheckFieldValue:string read getFcheckFieldvalue write setFcheckFieldValue;
property CheckFieldEnable:boolean read getCheckFieldEnable write setCheckFieldEnable default false;
property OnNodeChecked:TnotifyEvent read Fonnodechecked write Fonnodechecked;
property OnNodeUnChecked:Tnotifyevent read FonNOdeunchecked write FonNodeunchecked;
//与以下声明属性及事件Customtreeview相同;
property OnChange: TTVChangedEvent read FOnChange write FOnChange;
property Align;
property Anchors;
property AutoExpand;
property BevelEdges;
property BevelInner;
property BevelOuter;
property BevelKind default bkNone;
property BevelWidth;
property BiDiMode;
property BorderStyle;
property BorderWidth;
property ChangeDelay;
property Color;
property Ctl3D;
property Constraints;
property DragKind;
property DragCursor;
property DragMode;
property Enabled;
property Font;
property HideSelection;
property HotTrack;
property Images;
property Indent;
property MultiSelect;
property MultiSelectStyle;
property ParentBiDiMode;
property ParentColor default False;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property RightClickSelect;
property RowSelect;
property ShowButtons;
property ShowHint;
property ShowLines;
property ShowRoot;
property SortType;
property StateImages;
property TabOrder;
property TabStop default True;
property ToolTips;
property Visible;
property OnAddition;
property OnAdvancedCustomDraw;
property OnAdvancedCustomDrawItem;
//property OnChange;
property OnChanging;
property OnClick;
property OnCollapsed;
property OnCollapsing;
property OnCompare;
property OnContextPopup;
property OnCreateNodeClass;
property OnCustomDraw;
property OnCustomDrawItem;
property OnDblClick;
property OnDeletion;
property OnDragDrop;
property OnDragOver;
property OnEdited;
property OnEditing;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnExpanding;
property OnExpanded;
property OnGetImageIndex;
property OnGetSelectedIndex;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
{ Items must be published after OnGetImageIndex and OnGetSelectedIndex }
property Items;
end;
procedure Register;
implementation
function TDbtreeView.GetNodeLevel(sFormat,sCode:string):integer;//获得节点层数
var i,level,iLen:longint;
begin
level:=1 ;
iLen:=0;
if (sFormat<>'') and (sCode<>'') then
for i:=1 to Length(sFormat) do //分析编码格式,找出当前代码层次
begin
iLen:=iLen+StrToInt(sFormat[i]);
if Length(sCode)=iLen then
begin
level:=i;
break;
end;
end;
result:=level;
end;
procedure TDbtreeview.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (FDataLink <> nil) and
(AComponent = DataSource) then DataSource := nil;
end;
function TdbtreeView.GetDataField:string;
begin
Result := FDataLink.FieldName;
end;
function TDBTreeView.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
procedure TDBtreeview.SetDataSource(const Value: TDataSource);
begin
if not (FDataLink.DataSourceFixed and (csLoading in ComponentState)) then
begin
FDataLink.DataSource := Value;
end;
if Value <> nil then Value.FreeNotification(Self);
end;
constructor TDbtreeview.Create(AOwner: TComponent);
begin
inherited ;
FDataLink := TFieldDataLink.Create;
Fversion:='DBtreeView Build:2004-8 农武宁' ;
fcheckfield:='';
fcheckfieldvalue:='';
//Fdatasource.OnDataChange:=Datachange;
{ FDataLink.OnEditingChange := EditingChange;
FDataLink.OnUpdateData := UpdateData;
FDataLink.OnActiveChange := ActiveChange; }
end;
destructor tdbtreeview.Destroy;
begin
inherited ;
FDataLink.Free;
FDataLink := nil;
end;
procedure Register;
begin
RegisterComponents('Samples', [TDBtreeView]);
end;
procedure TDBtreeView.loadData;
var curID,nodeTxt:string;
level:integer;
mynode:array[0..8] of TTreenode;
begin //初始化变量
Screen.Cursor:=crHourGlass;
Enabled:=True;
Items.Clear;
level:=0 ;
//设置根节点
if FtreeRootText<>'' then
begin
mynode[level]:=items.add(Topitem,FTreeRootText);
mynode[level].ImageIndex:=1;
end ;
//遍历数据表,利用编码字段记录排序规律,依次添加树节点
if (Fdatasource<>nil ) and (fdatasource.DataSet.Active<>false) then
begin
try
if (FdataidField<>'' ) and (fdatalink.FieldName<>'') and (FtreecodeFormat<>'') then
begin
fdatasource.DataSet.First ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -