C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 27th, 2003, 09:05 AM
4dr14n's Avatar
4dr14n 4dr14n is offline
Huh?
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Scotland, UK
Posts: 223 4dr14n User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 4 h 16 m 20 sec
Reputation Power: 6
tree views in BCB

Hey Folks!

I am using tree views in BCB V6 for the first time.

They seem easy enough to use normally..ie add items and subitems.

What i want to know is whether it is possible to populate it with items taken dynamically from a database.

I have no code for trying this as i have not yet attempted it. I just need to know if it is possible and a starting point for how to do it if it is possible.

For example, if my database had:

item1
subitem 1.1
subitem 1.2
subitem 1.3

item 2
subitem 2.1
subitem 2.2

This would automatically be displayed i the tree view. However, if i then added item 3 to my database it would have to automatically come up on screen without me manually adding it to the treeview.

i hope this makes sense, if not just ask me to clear up any points and i will try!

Thanks!

Thought for the day:
A winner never quits, a quitter never wins.
__________________
-----------------------------------------------------------
4dr14n

Reply With Quote
  #2  
Old June 27th, 2003, 05:34 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,587 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 21 h 21 m 5 sec
Reputation Power: 997
You could try using a timer to periodically poll your database, to see if any new items have been added/deleted.

Reply With Quote
  #3  
Old June 30th, 2003, 03:10 AM
4dr14n's Avatar
4dr14n 4dr14n is offline
Huh?
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Scotland, UK
Posts: 223 4dr14n User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 4 h 16 m 20 sec
Reputation Power: 6
Thanks for the reply.

However, i dont think i made myself clear...

i need to know how to get the data from the database to appear in the tree view. At this point it does not matter if the database has been changed while it is being viewed.

I do not want the tree view items to be hardcoded, they must reflect items that are in a table in my database, and as these items can change i need code that will take whatever is in the database and put it in the treeview.

I hope this clears it up slightly but if not just ask!

Reply With Quote
  #4  
Old June 30th, 2003, 04:16 AM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 594 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 6 m 15 sec
Reputation Power: 21
Items can be inserted into a treeview with a SendMessage() and TVM_INSERTITEM msg. Fill in a TV_INSERTSTRUCT struct first.

An item is usually inserted after the last item ie

TV_INSERTSTRUCT TreeData;
TreeData.hInsertAfter=TVI_LAST;

To set the item as having root level (the first level, no a child of another item) use

TreeData.hParent=TVI_ROOT;

insert the item and catch the return as it is the HANDLE (HTREEITEM) of the new item. Check that it is a valid handle or call GetLastError()

hPrev=(HTREEITEM)SendMessage(hTreeView, TVM_INSERTITEM,0 ,(LPARAM)(LPTV_INSERTSTRUCT) &TreeData);
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

Frank Zappa

Reply With Quote
  #5  
Old June 30th, 2003, 08:22 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,587 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 21 h 21 m 5 sec
Reputation Power: 997
You could use TechNoFear's methods if you want, but BCB actually has wrapped most of those methods in the TTreeView object. You can call TreeView1->Items->AddChild() method to add children to a node and recursively call the function to build a tree. Here's some code to do so using BCB. Note that I just wrote the code, I haven't actually tested it, since I don't have a BCB compiler installed on this particular computer. However, the logic should be very easy to understand.
[edit]
Code:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	BuildNodes(NULL, 0);
}
//---------------------------------------------------------------------------
void TForm1::BuildNodes(TTreeNode *parent, int parent_id)
{
	TQuery *qry;
	char sql[1024];
	AnsiString name;
	int id;
	TTreeNode *Node;

	qry = new TQuery(this);
	qry->DatabaseName = "Database1";
	qry->SQL->Clear();
	sprintf(sql, "SELECT id, name FROM table WHERE parent_id = %d", parent_id);
	qry->SQL->Add(sql);
	qry->Open();
	// Might need to uncomment the next two lines
	// for certain DB engines, that lock tables when the result 
	// set is cached on the server end, until the client 
	// fetches all the results.
	// qry->Last();
	// qry->First();
	while (!qry->Eof) {
		id = qry->FieldByName("id")->AsInteger;
		name = qry->FieldByName("name")->AsString;
		Node = TreeView1->Items->AddChild(parent, name);
		// Now recurse into function to build children
		BuildNodes(Node, id);
                                
		// Get the next sibling
		qry->Next();
	}
	qry->Close();
	delete qry;
}

[/edit]
Hope this helps

Last edited by Scorpions4ever : July 1st, 2003 at 12:56 AM.

Reply With Quote
  #6  
Old July 1st, 2003, 02:38 AM
4dr14n's Avatar
4dr14n 4dr14n is offline
Huh?
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Scotland, UK
Posts: 223 4dr14n User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 4 h 16 m 20 sec
Reputation Power: 6
Thanks for the info...

Managed to do it using a similar method to what Scorpions4ever suggested. My database design is now causing me a bit of a headache..but thats a story for another day!

Thanks for the help.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > tree views in BCB


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT