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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TreeView Basics Part 2 1

Status
Not open for further replies.

safra

Technical User
Jan 24, 2001
319
NL
Hi,

I now understand the basics of the TreeView Component. The next step is inserting images. I found the following problems so far.

1. When you use 'AddChild' or 'AddChildObject' to add a node, The manual says that the new node is returned after the function is executed. But how can you instantly access this new node to set the ImageIndex?

2. When selecting a node in run time the ImageIndex is always set to 0. Why does this happen and how can you make sure that the ImageIndex remains the same?

Thanks for any advice!
 
The new node is returned by the function. The easiest way to instantly access the new node is to assign the return value of the function to a local variable and use the local variable as this example demonstrates:
Code:
procedure TForm1.AddNodeButtonClick(Sender: TObject);
var
  tn: TTreeNode;
begin
  tn := TV.Items.AddChild ( TV.Items[1], 'New Node' );
  tn.ImageIndex := 1;
end;

The ImageIndex does not change value when the node is selected. When a node is selected, the image that is displayed is that of the SelectedIndex. This defaults to 0. So if you want the same image to be displayed when the node is selected as when it is unselected you should assign the same value to SelectedIndex as you have to ImageIndex. So your code above might look like this:

Code:
procedure TForm1.Button3Click(Sender: TObject);
var
  tn: TTreeNode;
begin
  tn := TV.Items.AddChild ( tv.Items[1], 'New Node' );
  tn.ImageIndex := 1;
  tn.SelectedIndex := tn.ImageIndex;
end;

Andrew

 
Thanks again for the help, Andrew!

Got it working now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top