希望下面的代码能够对你有帮助
procedure TForm1.FormCreate(Sender: TObject);
var
//SelNode,
MyTreeNode1, MyTreeNode2:TTreeNode;
p
MyRec;
temp:string;
begin
query1.Open;
with treeview1.items do
begin
clear;
new(p);
p^.FName :='12';
mytreenode2:=addobject(nil,'商流本部',p);
query1.First;
while not query1.Eof do
begin
temp:=query1.fieldbyname('编码').asstring;
new(p);
p^.FName :=temp;
mytreenode1:=addCHILDobject(MyTreeNode2,query1.fieldbyname('名称').asstring,p);
Query2.Close;
Query2.SQL.Clear;
Query2.SQL.Add('SELECT 编码,名称 FROM JCSJ_销售事业部 WHERE LEN(编码)=5');
Query2.SQL.Add(' AND SUBSTRING(编码,1,3)='''+TEMP+'''');
Query2.SQL.Add('order by 编码');
Query2.OPEN;
Query2.FIRST;
WHILE NOT QUERY2.Eof DO
BEGIN
temp:=query2.fieldbyname('编码').asstring;
new(p);
p^.FName :=temp;
temp:=query2.fieldbyname('名称').asstring;
AddChildobject(MyTreeNode1,temp,p);
Query2.Next;
END;
query1.Next;
end;
end;
TREEVIEW1.selected:=mytreenode2;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if flag1 then
begin
if treeview1.Selected.Level=2 then
ModalResult:=mrok
else
application.messagebox(('必须选择工贸公司!'),'吉大工易',mb_ok);
end
else
begin
if (treeview1.Selected.Level=1) or (treeview1.Selected.Level=2) then
ModalResult:=mrok
else
//label1.Caption:='必须选'+#10#13+'择'+#10#13+'公司';
application.messagebox(('必须选择事业部或工贸公司!'),'吉大工易',mb_ok);
end;
end;