E
empty023
Unregistered / Unconfirmed
GUEST, unregistred user!
我在網上看見個關於treeview的程序,程序如下:
var
List: TStringList; //用于記錄各個id及其在樹中所對應的節點,從而實現快速查找
procedure MakeTree(Query: TADOQuery; TableName: string; TreeView: TTreeView);
begin
TreeView.Items.BeginUpdate;
list.Clear;
TreeView.items.clear;
if query.Active then query.Close;
Query.SQL.Text := 'SELECT * FROM ' + TableName + ' ORDER BY PID, ID';
Query.Open;
query.DisableControls;
TreeView.Items.Clear;
list.Clear;
List.Sorted := True;
query.First;
while not Query.Eof do
begin
addtreenode(Query, TreeView); //依次增加所有節點
Query.Next;
end;
TreeView.Items.EndUpdate;
query.EnableControls;
if treeview.Items.Count < 1 then exit;
treeview.Select(treeview.Items.Item[0]);
treeview.SetFocus;
end;
procedure addtreenode(Query: TADOQuery; TreeView: TTreeView; bj: boolean = false);
var
index: integer;
Node: TTreeNode;
begin
if Query.FieldByName('PID').AsInteger = 0 then { ParentID=0,頂層節點 }
Node := TreeView.Items.AddChildObject(nil, Query.FieldByName('CAPTION').AsString, query.GetBookmark)
//增加節點,並將本節點所對應的記錄標簽數據放到節點所提供的附加數據中
else
begin
Index := List.IndexOf(Query.FieldByName('PID').AsString);
Node := TreeView.Items.AddChildObjectFirst(TTreeNode(List.Objects[Index]),
Query.FieldByName('CAPTION').AsString, query.GetBookmark);
//增加子節點,并將本節點所對應的記錄標簽數據放到節點所提供的附加數據中
end;
//增加當前節點的信息到列表中,以實現在列表中快速查找節點的功能。
List.AddObject(Query.FieldByName('ID').AsString, Node);
if bj then
begin
treeview.Select(node);
treeview.SetFocus;
end;
end;
procedure AddChildNode(Query: TADOQuery; TreeView: TTreeView);
var
id: integer;
begin
id := query.FieldByName('ID').AsInteger; //記下當前的節點編號
query.Append;
query.FieldByName('PID').AsInteger := id; //新增加的子節點的父節點編號即為id
query.FieldByName('caption').AsString := '新子節點';
query.post;
addtreenode(query, treeview, true);
end;
procedure AddNode(Query: TADOQuery; TreeView: TTreeView);
var
pid: integer;
begin
pid := query.FieldByName('pID').AsInteger; //記下當前節點的父節點編號
query.Append;
query.FieldByName('PID').AsInteger := pid; //新增加的子節點的父節點編號即為pid
query.FieldByName('caption').AsString := '新節點';
query.post;
addtreenode(query, treeview, true);
end;
procedure Treechange(query: TADOQuery; node: TTreenode);
begin
query.GotoBookmark(node.Data);
end;
procedure TreeEdit(query: TADOQuery; text: string);
begin
query.Edit;
query.FieldByName('caption').AsString := text;
query.post;
end;
procedure treeselect(query: Tadoquery; TreeView: TTreeView);
var
index: integer;
Node: TTreeNode;
begin
Index := List.IndexOf(Query.FieldByName('ID').AsString);
Node := TTreeNode(List.Objects[Index]);
treeview.Selected := Node;
treeview.SetFocus;
end;
function TreeFind(TreeView: TTreeView; text: string): boolean;
var
i: integer;
begin
Result := false;
for i := 0 to treeview.Items.Count - 1 do
begin
if treeview.Items.Item.Text = text then
begin
treeview.Select(treeview.Items.Item);
treeview.SetFocus;
Result := true;
exit;
end;
end;
end;
initialization
List := TStringList.Create;
finalization
list.Free;
end.
程序能正常運行,但是它table裡的id與pid是integer型,我現在想把id與pid都換成string,請問要怎麼修改才能運行?
var
List: TStringList; //用于記錄各個id及其在樹中所對應的節點,從而實現快速查找
procedure MakeTree(Query: TADOQuery; TableName: string; TreeView: TTreeView);
begin
TreeView.Items.BeginUpdate;
list.Clear;
TreeView.items.clear;
if query.Active then query.Close;
Query.SQL.Text := 'SELECT * FROM ' + TableName + ' ORDER BY PID, ID';
Query.Open;
query.DisableControls;
TreeView.Items.Clear;
list.Clear;
List.Sorted := True;
query.First;
while not Query.Eof do
begin
addtreenode(Query, TreeView); //依次增加所有節點
Query.Next;
end;
TreeView.Items.EndUpdate;
query.EnableControls;
if treeview.Items.Count < 1 then exit;
treeview.Select(treeview.Items.Item[0]);
treeview.SetFocus;
end;
procedure addtreenode(Query: TADOQuery; TreeView: TTreeView; bj: boolean = false);
var
index: integer;
Node: TTreeNode;
begin
if Query.FieldByName('PID').AsInteger = 0 then { ParentID=0,頂層節點 }
Node := TreeView.Items.AddChildObject(nil, Query.FieldByName('CAPTION').AsString, query.GetBookmark)
//增加節點,並將本節點所對應的記錄標簽數據放到節點所提供的附加數據中
else
begin
Index := List.IndexOf(Query.FieldByName('PID').AsString);
Node := TreeView.Items.AddChildObjectFirst(TTreeNode(List.Objects[Index]),
Query.FieldByName('CAPTION').AsString, query.GetBookmark);
//增加子節點,并將本節點所對應的記錄標簽數據放到節點所提供的附加數據中
end;
//增加當前節點的信息到列表中,以實現在列表中快速查找節點的功能。
List.AddObject(Query.FieldByName('ID').AsString, Node);
if bj then
begin
treeview.Select(node);
treeview.SetFocus;
end;
end;
procedure AddChildNode(Query: TADOQuery; TreeView: TTreeView);
var
id: integer;
begin
id := query.FieldByName('ID').AsInteger; //記下當前的節點編號
query.Append;
query.FieldByName('PID').AsInteger := id; //新增加的子節點的父節點編號即為id
query.FieldByName('caption').AsString := '新子節點';
query.post;
addtreenode(query, treeview, true);
end;
procedure AddNode(Query: TADOQuery; TreeView: TTreeView);
var
pid: integer;
begin
pid := query.FieldByName('pID').AsInteger; //記下當前節點的父節點編號
query.Append;
query.FieldByName('PID').AsInteger := pid; //新增加的子節點的父節點編號即為pid
query.FieldByName('caption').AsString := '新節點';
query.post;
addtreenode(query, treeview, true);
end;
procedure Treechange(query: TADOQuery; node: TTreenode);
begin
query.GotoBookmark(node.Data);
end;
procedure TreeEdit(query: TADOQuery; text: string);
begin
query.Edit;
query.FieldByName('caption').AsString := text;
query.post;
end;
procedure treeselect(query: Tadoquery; TreeView: TTreeView);
var
index: integer;
Node: TTreeNode;
begin
Index := List.IndexOf(Query.FieldByName('ID').AsString);
Node := TTreeNode(List.Objects[Index]);
treeview.Selected := Node;
treeview.SetFocus;
end;
function TreeFind(TreeView: TTreeView; text: string): boolean;
var
i: integer;
begin
Result := false;
for i := 0 to treeview.Items.Count - 1 do
begin
if treeview.Items.Item.Text = text then
begin
treeview.Select(treeview.Items.Item);
treeview.SetFocus;
Result := true;
exit;
end;
end;
end;
initialization
List := TStringList.Create;
finalization
list.Free;
end.
程序能正常運行,但是它table裡的id與pid是integer型,我現在想把id與pid都換成string,請問要怎麼修改才能運行?