TTreeView的使用方法请教
在c++ builder中的TTreeView控件如何添加子节点,根节点为Root,我想在添加子节点时用AddChildObject添加一个字符串和一个整数,当选中某个节点时能取出对应的整数值,请问怎么取出这个值呢?
[解决办法]
在建每个node的时候把他的AbsoluteIndex和数字一一对应放在一个TStringList中sg
在选中某个node的时候根据他的AbsoluteIndex去找数字
TTreeNode *node = TreeView1->Selected;
ShowMessage(sg->Values[IntToStr(node->AbsoluteIndex)]);
[解决办法]
[code=C/C++][/code]
#ifndef Unit1H
#define Unit1H
//---------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
//---------------------------------------
class TForm1 : public TForm
{
__published:// IDE-managed Components
TTreeView *TreeView1;
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall TreeView1Change(TObject *Sender, TTreeNode *Node);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private:// User declarations
public:// User declarations
TStringList *ListTmp;
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------
#endif
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
ListTmp = new TStringList();
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TTreeNode *Node[2];
TreeView1->Items->Clear();
Node[0] = TreeView1->Items->AddChild(NULL,"测试根结点1"); //AbsoluteIndex = 0
ListTmp->Add("0=3");
TreeView1->Items->AddChild(Node[0],"结点1"); //AbsoluteIndex = 1
ListTmp->Add("1=4");
TreeView1->Items->AddChild(Node[0],"结点2");
ListTmp->Add("2=10");
TreeView1->Items->AddChild(Node[0],"结点3");
ListTmp->Add("3=101");
Node[1] = TreeView1->Items->AddChild(NULL,"测试根结点2");
ListTmp->Add("4=120");
TreeView1->Items->AddChild(Node[1],"结点1");
ListTmp->Add("5=140");
TreeView1->Items->AddChild(Node[1],"结点2");
ListTmp->Add("6=105");
TreeView1->Items->AddChild(Node[1],"结点3");
ListTmp->Add("7=107");
}
//---------------------------------------
void __fastcall TForm1::TreeView1Change(TObject *Sender, TTreeNode *Node)
{
String StrTmp;
if (Node == NULL) return;
StrTmp = Node->AbsoluteIndex;
Caption = ListTmp->Values[StrTmp];
}
//---------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
delete ListTmp;
}
//---------------------------------------