Copyright
©
2010 Promsite. All Rights Reserved.
стр. 6
Изучение компонента TTreeView, часть I
Listing A: Unit1.cpp
//-------------------------------------------------- #include
#pragma hdrstop #include "Unit1.h" //-------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //-------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { tomGordonNodeAdd = NULL; tomGordonNodeInsert = NULL; } //-------------------------------------------------- void __fastcall TForm1::CreateFigAClick(TObject *Sender) { // Add the root nodes al = TreeView1->Items->Add(NULL, "American League"); nl = TreeView1->Items->Add(NULL, "National League"); // Add the divisions in both leagues alEast = TreeView1->Items->AddChild(al, "East"); alCentral = TreeView1->Items->AddChild(al, "Central"); alWest = TreeView1->Items->AddChild(al, "West"); nlEast = TreeView1->Items->AddChild(nl, "East"); nlCentral = TreeView1->Items->AddChild(nl, "Central"); nlWest = TreeView1->Items->AddChild(nl, "West"); // Add teams only to American League East to save space bosNode = TreeView1->Items->AddChild(alEast, "Boston"); TreeView1->Items->AddChild(alEast, "New York"); TreeView1->Items->AddChild(alEast, "Toronto"); TreeView1->Items->AddChild(alEast, "Baltimore"); TreeView1->Items->AddChild(alEast, "Tampa Bay"); // Insert player categories under each team for ( int i = 0; i < alEast->Count; i++ ) { TreeView1->Items->AddChild(alEast->Item[i], "Pitchers"); TreeView1->Items->AddChild(alEast->Item[i], "Catchers"); TreeView1->Items->AddChild(alEast->Item[i], "Infielders"); TreeView1->Items->AddChild(alEast->Item[i], "Outfielders"); } // Add the Boston pitchers pitNode = bosNode->getFirstChild(); pedroMartinezNode = TreeView1->Items-> AddChild(pitNode, "Pedro Martinez"); timWakefieldNode = TreeView1->Items-> AddChild(pitNode, "Tim Wakefield"); TreeView1->Items-> AddChild(pitNode, "(etc)"); // Find the infield for use later // pitchers bostonInfieldersNode = bosNode->getFirstChild(); // Catchers bostonInfieldersNode = bostonInfieldersNode->getNextSibling(); // Infielders! bostonInfieldersNode = bostonInfieldersNode->getNextSibling(); // Show the entire tree from the top TreeView1->FullExpand(); TreeView1->TopItem = TreeView1->Items->GetFirstNode(); } //-------------------------------------------------- void __fastcall TForm1::QuitClick(TObject *Sender) { Close(); } //-------------------------------------------------- void __fastcall TForm1::AddTomGordonClick(TObject *Sender) { // Add a Tom Gordon and put Stats in the Data property Stats* lclStats = new Stats(1); tomGordonNodeAdd = TreeView1-> Items->Add(pedroMartinezNode, "Tom Gordon"); tomGordonNodeAdd->Data = (void *) lclStats; ListBox1->Items->Add("Inserted Tom with a new stats object"); ListBox1->Items->Add("containing the number 1"); } //-------------------------------------------------- void __fastcall TForm1::InsertTomGordonClick(TObject *Sender) { // Insert a Tom Gordon with a Stats object Stats* lclStats = new Stats(2); tomGordonNodeInsert = TreeView1-> Items->InsertObject(timWakefieldNode,"Tom Gordon", lclStats); ListBox1->Items->Add("Inserted Tom with a new stats object"); ListBox1->Items->Add("containing the number 2"); } //-------------------------------------------------- void __fastcall TForm1::DeleteGordonClick(TObject *Sender) { if ( tomGordonNodeAdd != NULL ) { // Retrieve Stats object from Data property Stats* statsInstance = (Stats*) tomGordonNodeAdd->Data; String number = statsInstance->getStats(); ListBox1->Items->Add("Stats contained a " + number); // Delete Stats and then the Tom Gordon node. delete statsInstance; tomGordonNodeAdd->Data = (void*) NULL; tomGordonNodeAdd->Delete(); ListBox1->Items->Add("Deleted the added Tom Gordon node.") tomGordonNodeAdd = NULL; } if ( tomGordonNodeInsert != NULL ) { // Retrieve Stats object from Data property Stats* statsInstance = (Stats*) tomGordonNodeInsert->Data; String number = statsInstance->getStats(); ListBox1->Items->Add("Stats contained a " + number); // Delete Stats and then the Tom Gordon node. delete statsInstance; tomGordonNodeInsert->Data = (void*) NULL; tomGordonNodeInsert->Delete(); ListBox1->Items-> Add("Deleted the inserted Tom Gordon node."); tomGordonNodeInsert = NULL; } } //-------------------------------------------------- void __fastcall TForm1::AddVaughnClick(TObject *Sender) { // Add Vaughn as child to Boston's infielders. Test handle // object with Vaughn. Adapted from calander example. nodeIdHandle* moData = new nodeIdHandle; nodeIdHandle* moDataOut; nodeIdHandle* moDataIn; moData->nodeType = ev_player; moData->obj = NULL; moVaughn = TreeView1->Items->AddChild(bostonInfieldersNode, "Mo Vaughn"); Stats* moStats = new Stats(3); moVaughn->Data = (void*) moData; moDataOut = (nodeIdHandle*) moVaughn->Data; moDataOut->obj = (void*) moStats; moStats = NULL; ListBox1->Items->Add("Added Mo with a Stats value of 3."); moDataIn = (nodeIdHandle*) moVaughn->Data; if ( moDataIn->nodeType == ev_player ) ListBox1->Items->Add("Retrieved Mo -- he's a player"); moStats = (Stats*) moDataIn->obj; String moVal = moStats->getStats(); ListBox1->Items->Add("Mo's stats value is a " + moVal); } //-------------------------------------------------- void __fastcall TForm1::DeleteChildrenClick(TObject *Sender) { // Delete all Boston pitchers pitNode->DeleteChildren(); } //-------------------------------------------------- void __fastcall TForm1::GetFirstRootClick(TObject *Sender) { // Show the name of the first root node ListBox1->Items->Add("First root node is " + TreeView1->Items->GetFirstNode()->Text); } //-------------------------------------------------- void __fastcall TForm1::ShowLevelClick(TObject *Sender) { // Show the level of the selected node TTreeNode *fc = TreeView1->Selected; if ( fc != NULL ) ListBox1->Items->Add("Node " + fc->Text + " is at level " + fc->Level); else ListBox1->Items->Add("Nothing selected!"); } //-------------------------------------------------- void __fastcall TForm1::WalkRootsClick(TObject *Sender) { // Get the first tree node (root) TTreeNode* rootNode = TreeView1->Items->GetFirstNode(); while ( rootNode ) { ListBox1->Items->Add(rootNode->Text); // Continue as long as there are siblings rootNode = rootNode->getNextSibling(); } } //-------------------------------------------------- void __fastcall TForm1::WalkChildrenDownClick(TObject *Sender) { // Get the first AL East child (team) TTreeNode* team = alEast->getFirstChild(); while ( team ) { ListBox1->Items->Add(team->Text); // Continue as long as there are children team = alEast->GetNextChild(team); } } //-------------------------------------------------- void __fastcall TForm1::WalkChildrenUpClick(TObject *Sender) { // Get the last child node in the AL East (team) TTreeNode* team = alEast->GetLastChild(); while ( team ) { ListBox1->Items->Add(team->Text); // Walk backwards as long as there are previous children team = alEast->GetPrevChild(team); } } //-------------------------------------------------- void __fastcall TForm1::WalkChildrenForClick(TObject *Sender) { // If AL East has children, add names to ListBox1 if ( alEast->HasChildren ) { ListBox1->Items->Add("Here are the teams in the AL East:"); for ( int team = 0; team < alEast->Count; team++ ) ListBox1->Items->Add(" " + alEast->Item[team]->Text); } else ListBox1->Items->Add("There are on AL East teams!"); } //-------------------------------------------------- void __fastcall TForm1::ChildIndexClick(TObject *Sender) { // Get the index of the selected node TTreeNode *fc = TreeView1->Selected; if ( fc != NULL ) ListBox1->Items->Add("Child node index for " + fc->Text + " is " + fc->Index); else ListBox1->Items->Add("Nothing selected!"); } //-------------------------------------------------- void __fastcall TForm1::TestParentChildClick(TObject *Sender) { // Test out Parent, HasAsParent, and Index TTreeNode* parent = bosNode->Parent; bool isparent = bosNode->HasAsParent(alEast); int bosPos = alEast->IndexOf(bosNode); if ( isparent ) { String bosLoc = bosPos; ListBox1->Items-> Add("Checked to see if Boston's parent was"); ListBox1->Items->Add("AL East...it was! Its child index is " + bosLoc); ListBox1->Items->Add("Boston's parent name via the Parent"); ListBox1->Items->Add("was " + parent->Text); } } //-------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { // Empty the ListBox ListBox1->Items->Clear(); }
P R O M S I T E
Статьи по С++Builder 6
страницы:
1
2
3
4
5
6
7
8
стр:
1
2
3
4
5
6
7
8
стр. 6
Для отображения Облака ссылок
необходим
Adobe Flash Player 9
или выше.