請問TreeView的用法 |
答題得分者是:Justmade
|
nethawk
一般會員 發表:7 回覆:11 積分:3 註冊:2003-01-16 發送簡訊給我 |
|
turboted
版主 發表:95 回覆:754 積分:452 註冊:2002-07-23 發送簡訊給我 |
以下,是一個由BCB5 Help-OnLine捉出來的例子
因為您已經把ID資料寫入到了TreeView的Data裏面
所以,您需要的,應該是如何,去應用這一個Data..
The following code defines a record type of TMyRec and a record pointer type of PMyRec. typedef struct MyRec { AnsiString FName, LName; } TMyRec; typedef TMyRec* PMyRec; Assuming these types are used, the following code adds a node to TreeView1 as the last sibling of a specified node. A TMyRec record is associated with the added item. The FName and LName fields are obtained from edit boxes Edit1 and Edit2. The Index parameter is obtained from edit box Edit3. The item is added only if the Index is a valid value. void __fastcall TForm1::Button1Click(TObject *Sender) { PMyRec MyRecPtr; int TreeViewIndex; TTreeNodes* pItems; MyRecPtr = new TMyRec; MyRecPtr->FName = Edit1->Text; MyRecPtr->LName = Edit2->Text; TreeViewIndex = StrToInt(Edit3->Text); pItems = TreeView1->Items; if (pItems->Count == 0) pItems->AddObject(NULL, "Item" IntToStr(TreeViewIndex), MyRecPtr); else if ((TreeViewIndex < pItems->Count) && (TreeViewIndex >= 0)) pItems->AddObject(pItems->Item[TreeViewIndex], "Item" IntToStr(TreeViewIndex), MyRecPtr); } After an item containing a TMyRec record has been added, the following code retrieves the FName and LName values associated with the item and displays the values in a label. void __fastcall TForm1::Button2Click(TObject *Sender) { Label1->Caption = PMyRec(TreeView1->Selected->Data)->FName " " PMyRec(TreeView1->Selected->Data)->LName; } |
Justmade
版主 發表:94 回覆:1934 積分:2030 註冊:2003-03-12 發送簡訊給我 |
不可以將不是 Object 的 Typecast 成Object
TObject(ID) 是不可以的
你要做一個Pointer Record 來記錄資料 使用時可用 :
PNodeRec(Node.Data)^.Name
PNodeRec(Node.Data)^.ID
在 Delphi 7 可不加 ^ 亦可但我忘了舊版的是否一定要有 ^ 來在Pointer 提取資料 全程式碼 :
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls; type TNodeRec = Record // 做一個 Record ID : String; Name : String; end; PNodeRec = ^TNodeRec; // 轉成 Pointer 以放入 TreeNode TForm1 = class(TForm) TreeView1: TTreeView; procedure TreeView1Changing(Sender: TObject; Node: TTreeNode; var AllowChange: Boolean); procedure FormShow(Sender: TObject); private procedure AddNode(ID,Name : String); { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormShow(Sender: TObject); begin AddNode('A001','陳大文'); AddNode('A002','李中人'); AddNode('B001','張小強'); end; procedure TForm1.AddNode(ID, Name: String); var Rec : PNodeRec; begin New(Rec); //建一個新的 PNodeRec Rec.ID := ID; Rec.Name := Name; TreeView1.Items.AddChildObject(nil,Name,Rec); end; procedure TForm1.TreeView1Changing(Sender: TObject; Node: TTreeNode; var AllowChange: Boolean); begin if Assigned(TreeView1.Selected) then TreeView1.Selected.Text := PNodeRec(TreeView1.Selected.Data)^.Name; Node.Text := PNodeRec(Node.Data)^.Name ' ' PNodeRec(Node.Data)^.ID; end; end.註 : 由於現時本站 [ code ] 段有Bug 一個 ' 會自動變四個所以請自行調回 |
nethawk
一般會員 發表:7 回覆:11 積分:3 註冊:2003-01-16 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |