TreeView控件和数据库的操作(50分)

  • 主题发起人 主题发起人 wjh_wy
  • 开始时间 开始时间
W

wjh_wy

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在有一个数据库中有这几个字段:ID(维一标识)、SubID(指定父的维一标识,在ID中)、
SubText(在TreeView控件中显示的名称)、SubNode(可以用来保存数据)。
就像我在右边选择一个目录或子目录,然后在右边从数据中读出数据到RichEdit中。
在打开这个数据库时要生树目录。还有关于指针类型的定义。希望各位高手能给我发源
码。或者指点点。先谢谢了!!
 
进来了就发句话吧。让我也不会感到寂寞
 
你给个邮件,我给你发一个例子
 
你想要的在我今天开发的模块中基本都用到了,没时间整理了,全发给你
unit FrmUntYWXXCX;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls,untpubclient,untPubFunc,YZJYFXFWServer_c, ImgList, DB, DBClient;

type
Tree_Rec = record
sjdxno: integer;
dxno: integer;
flag: string;
name: string;
zzdw: string;
end;
type
TFrmYWXXCX = class(TForm)
PanXXCX: TPanel;
Panel1: TPanel;
Panel7: TPanel;
Panel8: TPanel;
treeview_dy: TTreeView;
Panel5: TPanel;
Panel9: TPanel;
TreeView_ywzt: TTreeView;
Panel2: TPanel;
Panel3: TPanel;
Panel10: TPanel;
Label1: TLabel;
Panel4: TPanel;
Panel6: TPanel;
ScrollBox1: TScrollBox;
Panel11: TPanel;
Label3: TLabel;
Edit1: TEdit;
Label4: TLabel;
Edit2: TEdit;
Button1: TButton;
Button3: TButton;
Label5: TLabel;
Label2: TLabel;
Label6: TLabel;
Label7: TLabel;
Button4: TButton;
ImageList1: TImageList;
ClientDataSet1: TClientDataSet;
RichEdit1: TRichEdit;
procedure FormCreate(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure TreeView_ywztMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure treeview_dyMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
Tree_R: array of Tree_Rec;
procedure CreateDyTree;
procedure CreateZtTree;
procedure AddHisChild(Treeview1: TTreeview; RootTreeNode: TTreeNode;
myarray: array of Tree_Rec; dxNo: integer);
function TimeIsTrue(sForm,sTo: string): boolean;
function codeToChinese(s: string): string;
{ Private declarations }
public
{ Public declarations }
end;

ptreerec = ^treerec;
treerec = record
id: integer;
name: string;
end;

var
FrmYWXXCX: TFrmYWXXCX;
ztbl: string;
arrayCnt,tmp_count: integer;
zt,ztCode,dy,dyCode,TimeForm,TimeTo: string;
implementation

uses FrmUnitMX;

{$R *.dfm}

procedure TFrmYWXXCX.FormCreate(Sender: TObject);
begin
label5.Caption := '查' + chr(13)+ chr(13) + '找'+chr(13)+ chr(13)+'信'+chr(13)+ chr(13)+'息';
ztbl := FormMx.ZTBL;
ztbl := ChangeZTBL(ztbl);
CreateZtTree;
CreateDyTree;
TreeView_ywzt.Items[0].Expanded := true;
end;

procedure TFrmYWXXCX.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not(key in ['0'..'9',chr(8)]) then
key := chr(0);
end;

procedure TFrmYWXXCX.CreateDyTree;
var
mSql: string;
oData: variant;
ArrayStr: array of string;
ArrCount,i: integer;
mNode,zNode: TTreeNode;
ArrRec: array of Tree_Rec;
Prec: ptreerec;
begin
mSql := 'select zgdw from dictionaryb where lower(dictname)=''cjjdkzd'' group by zgdw';
JYFXClient.ClientOpenSQL(mSql, oData);
clientdataset1.Data := oData;
setlength(ArrayStr,clientdataset1.RecordCount);
ArrCount := 0;
with clientdataset1 do
begin
while not(eof) do
begin
ArrayStr[ArrCount] := Trim(fieldbyname('zgdw').AsString);
inc(ArrCount);
next;
end;
end;

mSql := 'select d.dxno,d.dxmc,db.dm,db.nr,db.zgdw from '
+'dxlsb d, dictionaryb db where d.clsno=39 and d.dxxz=''dydx'''
+' and lower(db.dictname)=''cjjdkzd'' and rtrim(trim(db.nr), ''节点库'') = trim(dxmc)';
JYFXClient.ClientOpenSQL(mSql, oData);
clientdataset1.Data := oData;
setlength(ArrRec,clientdataset1.RecordCount);
i := 0;
with clientdataset1 do
begin
while not(eof) do
begin
ArrRec.dxno := fieldbyname('dxno').AsInteger;
ArrRec.name := Trim(fieldbyname('dxmc').AsString);
ArrRec.zzdw := Trim(fieldbyname('zgdw').AsString);
ArrRec.flag := Trim(fieldbyname('dm').AsString);
inc(i);
next;
end;
end;

for ArrCount := 0 to ArrCount - 1 do
begin
mNode := Treeview_dy.Items.Add(nil,ArrayStr[ArrCount]);
for i := 0 to i - 1 do
if (ArrRec.zzdw = ArrayStr[ArrCount]) then
begin
zNode := TreeView_dy.Items.AddChild(mNode,ArrRec.name);
zNode.StateIndex := 1;
new(Prec);
Prec.id := ArrRec.dxno;
Prec.name := ArrRec.name;
zNode.Data := Prec;
end;
end;
end;

procedure TFrmYWXXCX.CreateZtTree;
var
str, strname, Stmp_dxno: string;
ptree: ptreerec;
oData: Variant;
mygetdata: variant;
i, j, tmp_dxno: integer;
myrootnode: ttreenode;
begin
try
str := CoObjYSQ.PubFuncEntry('CXJMZTHS', USER + ',' + ZTBL +',1', oData);
mygetdata := oData;
if mygetdata[1] <> null then begin
strname := mygetdata[0];
strname := copy(strname, 1, pos(',', strname) - 1);
Stmp_dxno := copy(mygetdata[0], pos(',', mygetdata[0]) + 1, length(mygetdata[0]) - pos(',', mygetdata[0]));
if Stmp_dxno = '' then
tmp_dxno := 0
else
tmp_dxno := strtoint(Stmp_dxno);
clientdataset1.Data := mygetdata[1];
if clientdataset1.recordcount > 0 then begin
arraycnt := clientdataset1.RecordCount;
tmp_count := arraycnt;
if arraycnt > 0 then
setlength(Tree_R, 0);
setlength(Tree_R, arrayCnt);
clientdataset1.First;
j := 0;
while not clientdataset1.Eof do begin
Tree_R[j].sjdxno := clientdataset1.fieldbyname('sjdxno').AsInteger;
Tree_R[j].dxno := clientdataset1.fieldbyname('dxno').AsInteger;
Tree_R[j].name := trim(clientdataset1.fieldbyname('dxmc').AsString);
Tree_R[j].flag := trim(ClientDataSet1.FieldByName('dxsjflag').asstring);
inc(j);
clientdataset1.Next;
end;
myrootnode := TreeView_ywzt.Items.Add(nil, strname);
new(ptree);
ptree^.id := tmp_dxno;
ptree^.name := strname;
myrootnode.Data := ptree;
AddHisChild(TreeView_ywzt, myrootnode, Tree_R, tmp_dxno);
end;
end;
except
on E: EAnErr do
begin
DoException(E);
Exit;
end;
end;
if upperCase(str) <> 'OK' then
ShowErr(str);
end;

procedure TFrmYWXXCX.AddHisChild(Treeview1: TTreeview;
RootTreeNode: TTreeNode; myarray: array of Tree_Rec; dxNo: integer);
var i: integer;
tempNode: TTreeNode;
ptree: ptreerec;
begin
for i := 0 to arrayCnt - 1 do
begin
if Tree_R.sjdxno = dxNo then
begin
tempNode := TreeView_ywzt.Items.AddChild(RootTreeNode, Tree_R.name);
tempNode.StateIndex := 1;
new(ptree);
ptree^.id := Tree_R.dxno;
ptree^.name := Tree_R.name;
tempNode.Data := ptree;
if Tree_R.flag <> '1' then
tempNode.StateIndex := -1;
AddHisChild(TreeView_ywzt, tempNode, Tree_R, Tree_R.dxno);
end;
end;
end;

procedure TFrmYWXXCX.TreeView_ywztMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
HitTests: THitTests;
i: integer;
begin
HitTests := TreeView_ywzt.GetHitTestInfoAt(X, Y);
if htOnStateIcon in HitTests then
begin
if TreeView_ywzt.Selected.StateIndex = 2 then
TreeView_ywzt.Selected.StateIndex := 1
else
TreeView_ywzt.Selected.StateIndex := 2;
end;
end;

procedure TFrmYWXXCX.treeview_dyMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
HitTests: THitTests;
i: integer;
begin
HitTests := TreeView_dy.GetHitTestInfoAt(X, Y);
if htOnStateIcon in HitTests then
begin
if TreeView_dy.Selected.StateIndex = 2 then
TreeView_dy.Selected.StateIndex := 1
else
TreeView_dy.Selected.StateIndex := 2;
end;
end;

function TFrmYWXXCX.TimeIsTrue(sForm,sTo: string): boolean;
function dayInMonth(sYear,sMonth: integer): integer;
begin
case sMonth of
1,3,5,7,8,10,12:result := 31;
4,6,9,11:result := 30;
2:
if ((sYear mod 4 = 0) and (sYear mod 100 <> 0)) or ((sYear mod 100 = 0) and (sYear mod 400 = 0)) then
result := 29
else
result := 28;
end;
end;
function typeIsYes(s: string): boolean;
begin
result := true;
if length(s) <> 12 then result := false;
if (strtoint(copy(s,5,2)) > 12) or (strtoint(copy(s,5,2))<1) then result := false;
if (strtoint(copy(s,7,2)) > dayInMonth(strtoint(copy(s,1,4)),strtoint(copy(s,5,2)))) or (strtoint(copy(s,7,2))<1) then result := false;
if strtoint(copy(s,9,2)) > 23 then result := false;
if strtoint(copy(s,11,2)) > 59 then result := false;
end;
begin
result := true;
TimeForm := sForm;
TimeTo := sTo;
if sForm = '' then
begin
showerr('请输入起始时间!',2);
result := false;
exit;
end;
if (sForm <> '') and (sTo = '') then
begin
if typeIsYes(sForm) = false then
begin
result := false;
showerr('时间格式出错,请参考提示的格式',2);
end
else begin
timeTo := FormatDateTime('yyyymmddhhmm',now);
sTo := timeTo;
end;
end
else
begin
if not(typeIsYes(sForm) and typeIsYes(sTo)) then
begin
result := false;
showerr('时间格式出错,请参考提示的格式',2);
end;
if sTo < sForm then
begin
result := false;
showerr('结束时间必须不能小于开始时间!',2);
end;
end;
end;

procedure TFrmYWXXCX.Button1Click(Sender: TObject);
var
i: integer;
procedure setTextFont(s: string;int: integer = 0);
begin
richedit1.SelStart := pos(s,richedit1.text)-1;
if int = 1 then
richedit1.SelLength := 3
else
richedit1.SelLength := 10;
if int = 1 then
richedit1.SelAttributes.Color := clred
else
begin
richedit1.SelAttributes.Style := [fsbold];
richedit1.SelAttributes.Color := clblue;
end;
end;
begin
richedit1.Lines.Clear;
zt := '';
ztCode := '';
for i := 0 to TreeView_ywzt.Items.Count -1 do
begin
if TreeView_ywzt.Items.StateIndex = 2 then
begin
ztCode := ztCode + inttostr(ptreerec(treeview_ywzt.Items.data)^.id) + ',';
zt := zt + ptreerec(treeview_ywzt.Items.data)^.name + ',';
end;
end;

dy := '';
dyCode := '';
for i := 0 to treeview_dy.Items.Count -1 do
begin
if treeview_dy.Items.StateIndex = 2 then
begin
dyCode := dyCode + inttostr(ptreerec(treeview_dy.Items.data)^.id) + ',';
dy := dy + ptreerec(treeview_dy.Items.data)^.name + ',';
end;
end;

delete(ztCode,length(ztCode),1);
delete(zt,length(zt),1);
delete(dyCode,length(dyCode),1);
delete(dy,length(dy),1);
if zt<>'' then
begin
richedit1.Lines.Add('主题信息:' + zt);
setTextFont('主题信息:');
end;
if dy <> '' then
begin
richedit1.Lines.Add('地域信息:' + dy);
setTextFont('地域信息:');
end;
if TimeIsTrue(trim(edit1.text),trim(edit2.text)) then begin
richedit1.Lines.Add('时段信息:' + '从 '+ codeToChinese(TimeForm) +' 到 '+ codeToChinese(TimeTo));
setTextFont('时段信息:');
setTextFont('从 ',1);
setTextFont(' 到 ',1);
end;
end;

function TFrmYWXXCX.codeToChinese(s: string): string;
begin
result := copy(s,1,4)+'年'+ copy(s,5,2) + '月' + copy(s,7,2) +'日'
+ copy(s,9,2) +'时' + copy(s,11,2) + '分';
end;

procedure TFrmYWXXCX.FormShow(Sender: TObject);
begin
screen.Cursor := crDefault;
end;

procedure TFrmYWXXCX.Button3Click(Sender: TObject);
var
i: integer;
begin
zt := ''; ztCode := '';
dy := ''; dyCode := '';
TimeForm := ''; TimeTo := '';
richedit1.Lines.Clear;
for i := 0 to treeview_dy.Items.Count-1 do
if treeview_dy.Items.StateIndex = 2 then
treeview_dy.Items.StateIndex := 1;
for i := 0 to TreeView_ywzt.Items.Count-1 do
if TreeView_ywzt.Items.StateIndex = 2 then
TreeView_ywzt.Items.StateIndex := 1;
edit1.Clear;
edit2.Clear;
end;

end.
 
To 一少
谢谢你了
还有哪位兄弟有,不怕多
 
我写过一个treeview的,但和你不一样的,也许有用。
unit main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, Tabnotbk, Tabs, ExtCtrls, Db, DBTables, TeEngine,
Series, TeeProcs, Chart, DBChart, Grids, DBGrids,Unit2;

type
TForm1 = class(TForm)
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
RadioGroup1: TRadioGroup;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Query: TQuery;
Query1: TQuery;
Query11: TQuery;
Query111: TQuery;
Query1111: TQuery;
Database1: TDatabase;
Tv: TTreeView;
DBChart1: TDBChart;
Series1: TBarSeries;
QueryTZ: TQuery;
DataSource1: TDataSource;
Sg1: TStringGrid;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure PageControl1Change(Sender: TObject);
procedure Edit1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
private
procedure Ydd1Info(Node: TTreeNode);
procedure Ydd11Info(Node: TTreeNode;Yjxh: String);
procedure Ydd111Info(Node: TTreeNode;Yjxh: String;Dyxh: String);
procedure Ydd1111Info(Node: TTreeNode;Yyxh: String);
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
CurZhh: String;

implementation

{$R *.DFM}

procedure TForm1.Ydd1Info(Node:TTreeNode); //用检户信息查询。
var
StrYdd1Sql: string;
StrYdd1Info: string;
Ydd1Node: TTreeNode;
Count: Integer;
begin
StrYdd1Sql:='select a.yjxh,a.fhdj,a.sdd,a.zjrl,c.flsm jlfs,d.flsm dydj ';
StrYdd1Sql:=StrYdd1Sql+'from ydd1 a,ydm c,ydm d ';
StrYdd1Sql:=StrYdd1Sql+'where a.zhh='+CurZhh+' and a.jlfs=c.sjdm and a.dydj=d.sjdm';
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add(StrYdd1Sql);
Query1.Open;
Count:=Query1.RecordCount;
If Count > 0 then
while Count >=1 do
begin
StrYdd1Info:='用检序号:'+Query1.FieldByName('yjxh').AsString;
StrYdd1Info:=StrYdd1Info+'负荷等级:' +Query1.FieldByName('fhdj').AsString;
StrYdd1Info:=StrYdd1Info+'受电地点:' +Query1.FieldByName('sdd').AsString;
//StrYdd1Info:=StrYdd1Info+'运行方式:' +Query1.FieldByName('yxfs').AsString;
StrYdd1Info:=StrYdd1Info+'装接容量:' +Query1.FieldByName('zjrl').AsString;
StrYdd1Info:=StrYdd1Info+'计量方式:' +Query1.FieldByName('jlfs').AsString;
StrYdd1Info:=StrYdd1Info+'电压等级:' +Query1.FieldByName('dydj').AsString;
Ydd1Node:=Tv.Items.AddChild(Node,StrYdd1Info);
Ydd11Info(Ydd1Node,Query1.FieldByName('yjxh').AsString);
Count:=Count-1;
Query1.Next;
end
else
Ydd11Info(Node,'0');
end;


procedure TForm1.Ydd11Info(Node: TTreeNode;Yjxh: String); //电源户信息查询。
var
StrYdd11Sql: String;
StrYdd11Info: String;
Ydd11Node: TTreeNode;
//Counter: Integer;
begin
StrYdd11Sql:='select a.yjxh,a.dyxh,b.xlmc gdxl,a.pzrl,a.zjrl,a.ptb from Ydd11 a,ydmxl b where a.zhh='+CurZhh+' and yjxh='+Yjxh+' and a.gdxl=b.xldm';
Query11.Close;
Query11.SQL.Clear;
Query11.Sql.Add(StrYdd11Sql);
Query11.open;
//Counter:=Query11.RecordCount;

While not Query11.Eof do
begin
StrYdd11Info:='电源序号:' +Query11.FieldByName('dyxh').AsString;
StrYdd11Info:=StrYdd11Info+'供电线路:' +Query11.FieldByName('gdxl').AsString;
//StrYdd11Info:=StrYdd11Info+'配变编码:' +Query11.FieldByName('pbbm').AsString;
//StrYdd11Info:=StrYdd11Info+'配变编码:' +Query11.FieldByName('pbbm').AsString;
StrYdd11Info:=StrYdd11Info+'批准容量:' +Query11.FieldByName('pzrl').AsString;
StrYdd11Info:=StrYdd11Info+'装见容量:' +Query11.FieldByName('zjrl').AsString;
StrYdd11Info:=StrYdd11Info+'PT变比:' +Query11.FieldByName('ptb').AsString;
Ydd11Node:=Tv.Items.AddChild(Node,StrYdd11Info);
Ydd111Info(Ydd11Node,Query11.FieldByName('yjxh').AsString,Query11.FieldByName('dyxh').AsString);
//Counter:=Counter-1;
Query11.Next;
end
end;

procedure TForm1.Ydd111Info(Node: TTreeNode;Yjxh: String;Dyxh: String); //营业户信息查询。
var
StrYdd111Sql: String;
StrYdd111Info: String;
Ydd111Node: TTreeNode;
//Counter: Integer;
begin
StrYdd111Sql:='select a.yyxh,b.flsm hym,c.flsm jbdj,a.zbrl from Ydd111 a,ydm b,ydm c where a.zhh='+CurZhh+' and a.yjxh='+Yjxh+' and a.dyxh1='+Dyxh+' and a.hym=b.sjdm and a.jbdj=c.sjdm';
Query111.Close;
Query111.SQL.Clear;
Query111.Sql.Add(StrYdd111Sql);
Query111.open;
//Counter:=Query11.RecordCount;
While not Query111.Eof do
begin
StrYdd111Info:='营业序号:' +Query111.FieldByName('yyxh').AsString;
StrYdd111Info:=StrYdd111Info+'行业码:' +Query111.FieldByName('hym').AsString;
StrYdd111Info:=StrYdd111Info+'基本电价:' +Query111.FieldByName('jbdj').AsString;
//StrYdd111Info:=StrYdd111Info+'计量侧:' +Query111.FieldByName('jlc').AsString;
StrYdd111Info:=StrYdd111Info+'装表容量:' +Query111.FieldByName('zbrl').AsString;
Ydd111Node:=Tv.Items.AddChild(Node,StrYdd111Info);
Ydd1111Info(Ydd111Node,Query111.FieldByName('yyxh').AsString);
Query111.Next;
end
end;


procedure TForm1.Ydd1111Info(Node: TTreeNode;Yyxh: String); //营业户信息查询。
var
StrYdd1111Sql: String;
StrYdd1111Info: String;
//Counter: Integer;
begin
StrYdd1111Sql:='select b.lx blx,c.flsm xw,a.bh,a.cl from Ydd1111 a,jldblx b,ydm c where a.zhh='+CurZhh+' and a.yyxh='+Yyxh+' and a.blx=b.dlxm and a.xw=c.sjdm';
Query1111.Close;
Query1111.SQL.Clear;
Query1111.Sql.Add(StrYdd1111Sql);
Query1111.open;
While not Query1111.Eof do
begin
StrYdd1111Info:='表类型:' +Query1111.FieldByName('blx').AsString;
StrYdd1111Info:=StrYdd1111Info+'相位:' +Query1111.FieldByName('xw').AsString;
StrYdd1111Info:=StrYdd1111Info+'表号:' +Query1111.FieldByName('bh').AsString;
StrYdd1111Info:=StrYdd1111Info+'乘率:' +Query1111.FieldByName('cl').AsString;
Tv.Items.AddChild(Node,StrYdd1111Info);
Query1111.Next;
end
end;

procedure TForm1.Button1Click(Sender: TObject);
var
StrYddSql: String;
StrYddInfo: string;
YddNode: TTreeNode;
begin
If RadioGroup1.ItemIndex = 0 then
begin
StrYddSql:='select zhh,dh,dxh,dhhm,hm,dz,sfhm,lxr from ydd where hm LIKE ''%'+Edit1.Text+'%''';
Query.Close;
Query.Sql.Clear;
Query.Sql.Add(StrYddSql);
//owMessage(Query.SQL.Text);
Query.Open;
If Query.RecordCount > 1 then
begin
Form2.ShowModal;
end
else
CurZhh:=Query.FieldByName('zhh').AsString;
end
else
CurZhh:=Edit1.Text;

StrYddSql:='select zhh,hm,dz from ydd where zhh='+CurZhh;
Query.Close;
Query.Sql.Clear;
Query.Sql.Add(StrYddSql);
Query.Open;
StrYddInfo:='总户号:'+Query.FieldByName('zhh').AsString +'户名:'+Query.FieldByName('hm').AsString +'地址:'+Query.FieldByName('dz').asstring;
Tv.Items.Clear;
YddNode:=Tv.Items.Add(nil,StrYddInfo);
Ydd1Info(YddNode);
Series1.Clear;
end;
 
指针类型的定义: xx: ^integer;
解除指针: xx^.
无型指针类型的定义:xx: Pointer;

 
多人接受答案了。
 
后退
顶部