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.
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?
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?
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.
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.