Board index » delphi » Selecting a TreeView.node

Selecting a TreeView.node

I am going to use the following code to select a new node in a
TreeView after I have just created it. This would both show the node,
and allow it to be named.

Two questions, if I may.

(1) Is there a more elegant way (I am using the logic that each new
node created has the highest ItemID in the Tree).

(2) Having found the ItemID of the latest node, how do I then select
it?

I would appreciate any help on this. Even if there is a better way
than the one below, I would appreciate knowing why the last lines are
giving me grief.

    For TreeCount := 0 to TreeView1.Items.Count - 1 do
    begin

Quote
>    {iterate through the tree until I get the highest number.
>    The logic here is that the one we just made is the highest number}

        if TNNum < integer(TreeView1.Items[TreeCount].ItemID)then
        begin
                TNNum := integer(TreeView1.Items[TreeCount].ItemID);
        end;         {if TNNum <                    

integer(TreeView1.Selected.Item[TreeCount].ItemID)then}
     end; {For TreeCount := 0 to TreeView1.Items.Count -1 do}
     ShowMessage (IntToStr(TNNum));

Quote
>    {Ok. We now have an integer for the highest ItemID in the Tree}
>    {crash after this at syntax check}

       TreeView1.Selected := TTreeNode(TNNum);

       Node := TreeView1.Items.GetNode(HTreeItem(TNNum)  );

Quote
>    {this piece of code is straight out of the Dart info on Trees}
>    {either HTTreeItem is undeclared, or I have Integer to          
>    HTTreeItem mismatch error messages}

    !!
   <")
   _/ )
   (   )
  _//- \__/
 

Re:Selecting a TreeView.node


Hey Nick,

I'm not sure where you got this code, but it seems to me that there's a few
problems with it.  First of all, whenever you add a node to a tree the
return value of a function is the TTreeNode created.  So if I call

ATreeView.Items.Add(ATreeView.Selected, 'I''m a new node')

It returns a TTreeNode as a result (see the delphi help for more info)...

Of course keeping a copy is not always an option so you could use the code
below to try to find the last created node.  However this line is bad
news...

Quote
>> {crash after this at syntax check}
>       TreeView1.Selected := TTreeNode(TNNum);

You're trying to Cast a handle to a treenode into a tree node.

Try this instead...

TreeView1.Selected := TreeView1.Items.GetNode(TNNum);

That should do it I think, if your assumption is correct about the ItemID
variable.  If I was in
your position I would just keep track of the TTreeNode return value when I
created the
node and use it instead.  It creates one more variable but is much cleaner.

Dean Liske (TECSKOR)

Quote
Nick White wrote in message <358ba9cd.266162...@forums.borland.com>...
>I am going to use the following code to select a new node in a
>TreeView after I have just created it. This would both show the node,
>and allow it to be named.

>Two questions, if I may.

>(1) Is there a more elegant way (I am using the logic that each new
>node created has the highest ItemID in the Tree).

>(2) Having found the ItemID of the latest node, how do I then select
>it?

>I would appreciate any help on this. Even if there is a better way
>than the one below, I would appreciate knowing why the last lines are
>giving me grief.

>    For TreeCount := 0 to TreeView1.Items.Count - 1 do
>    begin
>>    {iterate through the tree until I get the highest number.
>>    The logic here is that the one we just made is the highest number}
>        if TNNum < integer(TreeView1.Items[TreeCount].ItemID)then
>        begin
>                TNNum := integer(TreeView1.Items[TreeCount].ItemID);
>        end;         {if TNNum <

>integer(TreeView1.Selected.Item[TreeCount].ItemID)then}
>     end; {For TreeCount := 0 to TreeView1.Items.Count -1 do}
>     ShowMessage (IntToStr(TNNum));
>> {Ok. We now have an integer for the highest ItemID in the Tree}

>> {crash after this at syntax check}
>       TreeView1.Selected := TTreeNode(TNNum);

>       Node := TreeView1.Items.GetNode(HTreeItem(TNNum)  );
>> {this piece of code is straight out of the Dart info on Trees}
>> {either HTTreeItem is undeclared, or I have Integer to
>> HTTreeItem mismatch error messages}

>    !!
>   <")
>   _/ )
>   (   )
>  _//- \__/

Re:Selecting a TreeView.node


Quote
"Dean Liske" <dli...@planet.eon.net> wrote:
>Hey Nick,

>I'm not sure where you got this code, but it seems to me that there's a few

I am completely lost! But thanks anyway.

    !!
   <")
   _/ )
   (   )
  _//- \__/

Re:Selecting a TreeView.node


Quote
"Dean Liske" <dli...@planet.eon.net> wrote:
>Hey Nick,

>I'm not sure where you got this code, but it seems to me that there's a few
>problems with it.  

Was this the one where I was sking for a more elegant solution?
<G>....ahah! YES! At least I got that far.

Quote
>First of all, whenever you add a node to a tree the
>return value of a function is the TTreeNode created.  So if I call

>ATreeView.Items.Add(ATreeView.Selected, 'I''m a new node')

That's the elegant solution I was looking for. Oh well...at least I'm
learing all sorts of new reasons NOT to do things.....(|)

    !!
   <")
   _/ )
   (   )
  _//- \__/

Re:Selecting a TreeView.node


Quote
"Dean Liske" <dli...@planet.eon.net> wrote:
>Of course keeping a copy is not always an option so you could use the code
>below to try to find the last created node.  However this line is bad
>news...

>>> {crash after this at syntax check}
>>       TreeView1.Selected := TTreeNode(TNNum);

>You're trying to Cast a handle to a treenode into a tree node.

>Try this instead...

>TreeView1.Selected := TreeView1.Items.GetNode(TNNum);

The problem here is that TNNum is an integer, and GetNode is
apparently an HTTreeItem. This is where I strike my problem (your code
is almost what I had also tried on the next line as an alternative).
If I try above line of code, I get Incompatible types. If I try
casting to an HTTreeItem, I get "undeclared Identifier"

I ask because I am trying to understand. You posted the code that
makes the rest (and the offending line) redundant (well the rest of my
example anyway. If you can write "THAT ONE LINE", then I imagine we
will all disappear in a flash of overpowering light <G>), but I would
like to know what goes here, as it may be useful.

Quote
>That should do it I think, if your assumption is correct about the ItemID
>variable.  If I was in
>your position I would just keep track of the TTreeNode return value when I
>created the
>node and use it instead.  It creates one more variable but is much cleaner.

>Dean Liske (TECSKOR)

>Nick White wrote in message <358ba9cd.266162...@forums.borland.com>...
>>I am going to use the following code to select a new node in a
>>TreeView after I have just created it. This would both show the node,
>>and allow it to be named.

>>Two questions, if I may.

>>(1) Is there a more elegant way (I am using the logic that each new
>>node created has the highest ItemID in the Tree).

>>(2) Having found the ItemID of the latest node, how do I then select
>>it?

>>I would appreciate any help on this. Even if there is a better way
>>than the one below, I would appreciate knowing why the last lines are
>>giving me grief.

>>    For TreeCount := 0 to TreeView1.Items.Count - 1 do
>>    begin
>>>    {iterate through the tree until I get the highest number.
>>>    The logic here is that the one we just made is the highest number}
>>        if TNNum < integer(TreeView1.Items[TreeCount].ItemID)then
>>        begin
>>                TNNum := integer(TreeView1.Items[TreeCount].ItemID);
>>        end;         {if TNNum <

>>integer(TreeView1.Selected.Item[TreeCount].ItemID)then}
>>     end; {For TreeCount := 0 to TreeView1.Items.Count -1 do}
>>     ShowMessage (IntToStr(TNNum));
>>> {Ok. We now have an integer for the highest ItemID in the Tree}

>>> {crash after this at syntax check}
>>       TreeView1.Selected := TTreeNode(TNNum);

>>       Node := TreeView1.Items.GetNode(HTreeItem(TNNum)  );
>>> {this piece of code is straight out of the Dart info on Trees}
>>> {either HTTreeItem is undeclared, or I have Integer to
>>> HTTreeItem mismatch error messages}

>>    !!
>>   <")
>>   _/ )
>>   (   )
>>  _//- \__/

    !!
   <")
   _/ )
   (   )
  _//- \__/

Other Threads