Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tree node not selected with popup 1

Status
Not open for further replies.

Dooda

IS-IT--Management
May 14, 2001
76
GB
When I right-click on a tree node to get a popup menu, the node is not selected (unless it was already selected). Programs I use (eg. Win explorer) do select on popup so I was expecting it to happen - how do I get selection without writing lots of code?
 
In your code select the item in the node first and then show the popup menu. I know this is a bit vague, but I have done this my self only it was some time ago
 
Thanks for the response, but the difficulty is that within the code I do not know which node was under the cursor when right-clicked, because it is not returned by TreeView1.Selected. Whatever was selected beore the rigth-click is returned.
In theory one might be able to work it out from the cursor position at the click event but as far as I can see it will be difficult and long-winded.
 
If you are using Delphi 5, set the the ListView's property RightClickSelects := True. I can not remember if this works in earlier versions. If not, you will have to create the following event:

procedure TForm1.TreeView1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var N: TTreeNode;
begin
with TTreeView(Sender) do
begin
N := GetNodeAt(x,y);
if Assigned(N) then Selected := N;
end;
end;

 
There is a property on the TTreeview SelectOnRightClick (I believe that is the property) that turns on support for selecting a node when the node is right clicked on.

Brett Parkhurst
brettparkhurst@uswest.net
 
Hi,

Make sure that the RIGHT CLICK SELECT proerty is set to True.

I have only srarted using the TreeView object recently. I am starting to get used to it. One question. Can someone tell me the command that allows a new node to be created during runtime. (and any other useful command if you no them)

tree.Items[1].Delete;

works fine when deleting a node, but some parameters are required when using the create command.

Finally, is it possible to link the TreeView to a database? For instance build a father-son relationship between hierarchial related records?

Thanks
Martin
 
Hi
I have only srarted using the TreeView object recently. I am starting to get used to it. One question. Can someone tell me the command that
allows a new node to be created during runtime. (and any other useful
command if you no them)

tree.Items[1].Delete;

works fine when deleting a node, but some parameters are required
when using the create command.

Finally, is it possible to link the TreeView to a database? For instance
build a father-son relationship between hierarchial related records?

Thanks
Martin
 
Martin if you are still having problems linking the tree node to a database let me know and I will sen you the code we use. BUT it does mean using classes.
Slim
 
1- Set RightClickSelect property in TreeView to TRUE
2- Think of using OnContextPopup event for your TreeView to control PopupMenu. It
occurs right before showing PopupMenu only it does not hold the selected TreeNode
ypu're worrying about, however...go to step 3 and you'll find a solution
3- In the event handler employ Selected, this TreeView property represents the TreeNode the user just right-clicked.

With this you get a behaviour very similar to that shown by Windows Explorer.
 
Martin,

To add a tree node at runtime use the methods of the TTreeNodes class (AddFirst, AddChild, Add etc). For example:

procedure TForm1.Button1Click(Sender: TObject);
var
ParentNode: TTreeNode;
begin
ParentNode := MyTreeView.Items.AddFirst(nil, 'Parent Node');
MyTreeView.Items.AddChildFirst(ParentNode, 'Child Node 1');
MyTreeView.Items.AddChild(ParentNode, 'Child Node 2');
end;

If you are using the TTreeNode.Data property to attach pointers to your nodes then it is much faster to use AddChildObject than to attach the pointer after the node has been created using AddChild etc.


If you are using Oracle then you can run a query that will return data structured in a parent-child relationship using Start With and Connect By. For example:

select id, parent_id, company_name
from company
start with parent_id = 0
connect by parent_id = prior id

will return a set of companies, where any parent company has a parent_id of 0, and any child company has a parent_id equal to the id of its parent. Did that make sense? In addition to the columns specified this query will also return a column called 'level', which tells you which level of the tree the record is at. You will still need to manually loop through the query data set and add nodes to the treeview though, there is no DBTreeView component.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top