Treeview -node is not selected.

Peter Kiers
Peter Kiers used Ask the Experts™
on
Dear Experts,

I have an application with a Treeview on it and 2 buttons.
One button is for making a File-node in the treeview and
the other is for making a Folder-node in the treeview.
I have put the code of the 2 buttons in the code-section.
Everything works great, only when I want to make a
folder-node or a file-node in the treeview the node is
not selected.

Who can help me with this?

Greetings,

Peter Kiers

procedure TForm1.FileBtnClick(Sender: TObject);
begin
  if (TreeView1.Selected <> nil) and
     (TreeView1.Selected.ImageIndex = 15) then
  begin
    if NewItemFrm.ShowModal = mrOk then
     AddItem(NewItemFrm.Edit1.Text ,17,TreeView1.Selected); 
  end else 
    ShowMessage('You must select a folder first');
end;
 
procedure TForm1.FolderBtnClick(Sender: TObject);
 begin
  if (TreeView1.Selected <> nil) and
     (TreeView1.Selected.ImageIndex = 15) then
  begin
    if (NewFolderFrm.ShowModal = mrOk ) then
      AddItem(NewFolderFrm.Edit2.Text ,15,TreeView1.Selected);
  end else 
    ShowMessage('You must select a folder first');
end;
 
procedure TMainForm.AddItem(aText: string; aIndex: Integer; aParent: TTreeNode);
var n: TTreeNode;
  data: TItem;
  parentId: integer;
begin
  parentId := 0;
  if (aParent <> nil) and (TItem(aParent.Data) <> nil) then
    parentId := TItem(aParent.Data).ID;
  data := TItem.Create(AdoQuery1, -1, parentId, 0);
  data.NodeText := aText;
  data.ImageIndex := aIndex;
  n := TTreeNode.Create(TreeView1.Items);
  n.Text := data.NodeText;
  n.ImageIndex := data.ImageIndex;
  n.SelectedIndex := data.ImageIndex;
  TreeView1.Items.AddNode(n, aParent, aText, data, naAddChild);
end;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Geert GOracle dba
Top Expert 2009

Commented:
if you don't want to select the node on creation, call it with selectnode = False
for example:
AddItem(NewFolderFrm.Edit2.Text ,15,TreeView1.Selected, False);


procedure TMainForm.AddItem(aText: string; aIndex: Integer; aParent: TTreeNode; SelectNode: Boolean = True);
var n, m: TTreeNode;
  data: TItem;
  parentId: integer;
begin
  parentId := 0;
  if (aParent <> nil) and (TItem(aParent.Data) <> nil) then
    parentId := TItem(aParent.Data).ID;
  data := TItem.Create(AdoQuery1, -1, parentId, 0);
  data.NodeText := aText;
  data.ImageIndex := aIndex;
  n := TTreeNode.Create(TreeView1.Items);
  n.Text := data.NodeText;
  n.ImageIndex := data.ImageIndex;
  n.SelectedIndex := data.ImageIndex;
  m := TreeView1.Items.AddNode(n, aParent, aText, data, naAddChild);
  if SelectNode then 
  begin
    m.Focused := True;
    m.Selected := True;
  end;
end;

Open in new window

Peter KiersOperator

Author

Commented:
Hi, The node is made visible but not selected!

Greetings,

Peter Kiers
Peter KiersOperator

Author

Commented:
I know MakeVisible does the trick but I don't know where to put it!!!

PK
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Oracle dba
Top Expert 2009
Commented:
 m := TreeView1.Items.AddNode(n, aParent, aText, data, naAddChild);
  m.MakeVisible;
  if SelectNode then
  begin
    m.Focused := True;
    m.Selected := True;
  end;
Peter KiersOperator

Author

Commented:
Hi Geert it still doesn't work. Again the node is made visible but not selected.

Greetings, Peter Kiers
MerijnBSr. Software Engineer

Commented:
You might need to focus the tree itself, after Geert's code do:

Treeview1.SetFocus;

Peter KiersOperator

Author

Commented:
I like the solution Geert gave me, but I don't want to put code in my procedures if I want tom
make the node visible or not. I just want that when a user makes a folder-node or a file-node
that the node is selected. I have tried this:

procedure TForm1.FileBtnClick(Sender: TObject);
begin
  if (TreeView1.Selected <> nil) and
     (TreeView1.Selected.ImageIndex = 15) then
  begin
    if NewItemFrm.ShowModal = mrOk then
     AddItem(NewItemFrm.Edit1.Text ,17,TreeView1.Selected);
  end else
    ShowMessage('You must select a folder first');
end;
(*---------------------------------------------------*)
procedure TForm1.FolderBtnClick(Sender: TObject);
 begin
  if (TreeView1.Selected <> nil) and
     (TreeView1.Selected.ImageIndex = 15) then
  begin
    if (NewFolderFrm.ShowModal = mrOk ) then
      AddItem(NewFolderFrm.Edit2.Text ,15,TreeView1.Selected);
  end else
    ShowMessage('You must select a folder first');
end;

procedure TForm1.AddItem(aText: string; aIndex: Integer; aParent: TTreeNode);
var n: TTreeNode;
  data: TItem;
  parentId: integer;
begin
  parentId := 0;
  if (aParent <> nil) and (TItem(aParent.Data) <> nil) then
    parentId := TItem(aParent.Data).ID;
  data := TItem.Create(AdoQuery1, -1, parentId, 0);
  data.NodeText := aText;
  data.ImageIndex := aIndex;
  n := TTreeNode.Create(TreeView1.Items);
  n.Text := data.NodeText;
  n.ImageIndex := data.ImageIndex;
  n.SelectedIndex := data.ImageIndex;
  TreeView1.Items.AddNode(n, aParent, aText, data, naAddChild);
  n. MakeVisible;
  Treeview1.SetFocus;
end;

But in the code above only the parent node is selected of the node i created.

Peter Kiers

Commented:
Hi,

I 'm using treeview in lot of programs, but data handling differs from yours.
Usually I never made changes on treeview itself (insert, update, delete), but in dataset.
Here is how I'm using treeview

On form Create
   Read data - fill treeview

On update, insert, delete
   make changes in dataset,
   Read data - fill treeview
   in case of insert, update  :  select new or changed data treeview

By the way, related to your earlier question about inputquery,
here is improved code with message dialog (with or without : editbox, icon, error message...)
Check it, perhaps you can use it somewhere.

regards,
  steve


  private
    function MyMsgDialog(const Title: string;
                         const Tekst: string;
                         const Error_Text: string;
                         My_Icon : TImage;
                         DlgType: TMsgDlgType;
                         Buttons: TMsgDlgButtons;
                         Btn_Captions: array of string;
                         ShowEditBox, Show_My_Icon: Boolean): Integer;
 
var
  aMsgDlg: TForm;
  dlgImage: TImage;
  dlgEdit: TEdit;
  dlgPanel: TPanel;
  dlgLabel: TLabel;
  dlgButton: TButton;
  My_Picture : TImage;
 
function TForm1.MyMsgDialog(const Title: string;
                            const Tekst: string;
                            const Error_Text: string;
                            My_Icon : TImage;
                            DlgType: TMsgDlgType;
                            Buttons: TMsgDlgButtons;
                            Captions: array of string;
                            ShowEditBox, Show_My_Icon: Boolean): Integer;
var
  i : Integer;
  CaptionIndex : Integer;
begin
  captionIndex    := 0 ;
  aMsgDlg := CreateMessageDialog(Tekst, DlgType, Buttons);
  aMsgDlg.Caption := Title ;
  aMsgDlg.Height := 120;
  if ShowEditBox then
     begin
     dlgEdit := TEdit.Create(Self);
     dlgEdit.Parent := aMsgDlg;
     dlgEdit.Top := 30;
     dlgEdit.Left := 70;
     dlgEdit.Width := 150;
     aMsgDlg.Height := 140;
  end;
  if length(trim(Error_text)) > 0 then
     begin
     dlgPanel := TPanel.Create(Self);
     dlgPanel.Parent := aMsgDlg;
     dlgPanel.Height := 20;
     dlgPanel.Color := clRed;
     dlgPanel.Font.Color := clWhite;
     dlgPanel.Font.Style := [fsBold];
     dlgPanel.Caption := Error_text;
     dlgPanel.Visible := True;
     dlgPanel.Align := alBottom;
     aMsgDlg.Height := 140;
  end;
  for i := 0 to aMsgDlg.ComponentCount - 1 do
     begin
     if (aMsgDlg.Components[i] is TButton) then
        begin
        dlgButton := TButton(aMsgDlg.Components[i]);
        if CaptionIndex > High(Btn_Captions) then  Break;
        dlgButton.Caption := Btn_Captions[CaptionIndex];
        dlgButton.Font.Size := Application.MainForm.Font.Size ;
        dlgButton.Top := aMsgDlg.Height - 70;
        Inc(CaptionIndex);
     end;
     if (aMsgDlg.Components[i] is TLabel) then
        begin
        dlgLabel := TLabel(aMsgDlg.Components[i]) ;
        dlgLabel.Font.size := Application.MainForm.font.size ;
     end ;
     if (aMsgDlg.Components[i] is TEdit) then
        begin
        DlgEdit := TEdit(aMsgDlg.Components[i]) ;
        DlgEdit.Font.size := Application.MainForm.font.size ;
     end ;
     if (aMsgDlg.Components[i] is TPanel) then
        begin
        DlgPanel := TPanel(aMsgDlg.Components[i]) ;
        DlgPanel.Font.size := Application.MainForm.font.size ;
     end ;
     if Show_My_Icon then
        begin
        if (aMsgdlg.Components[i] is TImage ) then
           begin
           dlgImage := TImage(aMsgdlg.Components[i]) ;
           dlgImage.Picture := My_Icon.Picture;
        end ;
     end ;
   end;
   Result := aMsgDlg.ShowModal;
end;
 
usage :
 
procedure TForm1.ButtonClick(Sender: TObject);
begin
  if MyMsgDialog('           Title here                        ',
                 '           Msg body here                     ', '',  nil,
                 mtCustom, mbOKCancel, ['OK', 'Cancel'],False, False) = mrOk then //simple message without icon
//               mtCustom, mbOKCancel, ['OK', 'Cancel'],True, False) = mrOk then  //message with edit, without icon
//               mtConfirmation, mbOKCancel, ['OK', 'Cancel'],True, False) = mrOk then   //edit with icon 
     begin
     if length(trim(dlgEdit.Text)) = 0 then
        repeat
        if MyMsgDialog('           Title here                  ',
           '           Msg body here               ', 'You must enter name !!', nil,        //With error message 
//         '           Msg body here               ', 'You must enter name !!', My_picture, //with custom TImage
                       mtWarning, mbOKCancel, ['OK', 'Cancel'],True, True) = mrCancel then Break;
        until length(trim(dlgEdit.Text)) > 0;
     if length(trim(dlgEdit.Text)) = 0 then  Exit;
     showmessage('Edit - Ok !');
  end;
end;

Open in new window

Peter KiersOperator

Author

Commented:
I found the example from Ciuly on EE. I have used it as bases to create my
own knowlegdebase. I have put the code in the code-section:

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, ComCtrls, StdCtrls, ImgList, ExtCtrls;
 
type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Splitter1: TSplitter;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    ADOQuery1: TADOQuery;
    ADOConnection1: TADOConnection;
    FolderBtn: TButton;
    FileBtn: TButton;
    ImageList1: TImageList;
    DeleteButton: TButton;
    RichEdit1: TRichEdit;
    procedure FolderBtnClick(Sender: TObject);
    procedure FileBtnClick(Sender: TObject);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
 
  private
    { Private declarations }
    procedure savetree(t: ttreeview);
    procedure loadtree(t: TTreeView);
    procedure saveNode(n: TTreeNode);
    function findNode(t:TTreeView; id: integer): TTreeNode;
    procedure AddItem(aText: string; aIndex: Integer; aParent: TTreeNode);
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
uses NewFolder, NewItem;
 
{$R *.dfm}
 
type
  TItem = class(TObject)
  private
    fStatus: byte;
    fParentID: integer;
    fNodeText: string;
    fID: integer;
    fData: string;
    fSaver: TAdoQuery;
    fChanged: Boolean;        
    fSaveImmediate: boolean;
    fIsNew: Boolean;
    fImageIndex: integer;
    procedure SetData(const Value: string);
    procedure SetNodeText(const Value: string);
    procedure SetParentID(const Value: integer);
    procedure SetStatus(const Value: byte);
    procedure SetImageIndex(const Value: integer);
  protected
    procedure Changed; dynamic;
  public
    constructor Create(aSaver: TAdoQuery; AId, AParentID: integer; AStatus: byte);
    constructor LoadFromDataset(aSaver: TAdoQuery);
    procedure BeginUpdate;
    procedure EndUpdate;
    procedure Delete;
    procedure Save;
  published
    property ID: integer read fID write fID;
    property ParentID: integer read fParentID write SetParentID;
    property ImageIndex: integer read fImageIndex write SetImageIndex;
    property Status: byte read FStatus write SetStatus;
    property Data: string read fData write SetData;
    property NodeText: string read fNodeText write SetNodeText;
  end;
 
{ TItem }
 
procedure TItem.BeginUpdate;
begin
  fSaveImmediate := false;                 
end;
(*---------------------------------------------------*)
procedure TItem.Changed;
begin
  fChanged := True;
  if fSaveImmediate then Save;
end;
(*---------------------------------------------------*)
constructor TItem.Create(aSaver: TAdoQuery; AId, AParentID: integer;
  AStatus: byte);
begin
  inherited Create;
  fChanged := False;
  fSaveImmediate := True;
  fIsNew := True;
  fSaver := aSaver;
  fID := AID;
  fParentID := AParentID;
  fStatus := AStatus;
  fNodeText := '';
  fData := '';
end;
(*---------------------------------------------------*)
procedure TItem.Delete;
begin
//
end;
(*---------------------------------------------------*)
procedure TItem.EndUpdate;
begin
  fSaveImmediate := True;
  if fChanged then Save;
end;
(*---------------------------------------------------*)
constructor TItem.LoadFromDataset(aSaver: TAdoQuery);
begin
  Create(aSaver, 0, 0, 0);
  if not aSaver.IsEmpty then
  begin
    fId := aSaver.FieldByName('ID').AsInteger;
    fParentID := aSaver.FieldByName('PARENT').AsInteger;
    fStatus := aSaver.FieldByName('STATUS').AsInteger;
    fNodeText := aSaver.FieldByName('NAME').AsString;
    if aSaver.FindField('DATA') <> nil then
      fData := aSaver.FieldByName('DATA').AsString;
    fImageIndex := aSaver.FieldByName('IMAGE_INDEX').AsInteger;
    fIsNew := False;
  end;
end;
(*---------------------------------------------------*)
procedure TItem.Save;
begin
  if fIsNew then
    fSaver.SQL.Text :=
      'insert into tree (parent, name, status, image_index, data) values (:v1, :v2, :v3, :v4, :v5)'
  else
    fSaver.SQL.Text :=
      'update tree set parent = :v1, name = :v2, status = :v3, image_index = :v4, data = :v5 where id = :v6';
  fSaver.Parameters.ParamByName('v1').Value := fparentID;
  fSaver.Parameters.ParamByName('v2').Value := fNodeText;
  fSaver.Parameters.ParamByName('v3').Value := fStatus;
  fSaver.Parameters.ParamByName('v4').Value := fImageIndex;
  fSaver.Parameters.ParamByName('v5').Value := fData;
  if not fIsNew then
    fSaver.Parameters.ParamByName('v6').Value := fId;
  fSaver.ExecSQL;
  if fIsNew then
  begin
    fSaver.SQL.Text :=
      'SELECT id from tree order by id desc';
    fSaver.Open;
    fId := fSaver.FieldByName('ID').asInteger;
    fSaver.Close;
    fIsNew := False;
  end;
  fChanged := False;
end;
(*---------------------------------------------------*)
procedure TItem.SetData(const Value: string);
begin
  if fData <> Value then
  begin
    fData := Value;
    Changed;
  end;
end;
(*---------------------------------------------------*)
procedure TItem.SetImageIndex(const Value: integer);
begin
  if fImageIndex <> Value then
  begin
    fImageIndex := Value;
    Changed;
  end;
end;
(*---------------------------------------------------*)
procedure TItem.SetNodeText(const Value: string);
begin
  if fNodeText <> Value then
  begin
    fNodeText := Value;
    Changed;
  end;
end;
(*---------------------------------------------------*)
procedure TItem.SetParentID(const Value: integer);
begin
  if fParentId <> Value then
  begin
    fParentID := Value;
    Changed;
  end;
end;
(*---------------------------------------------------*)
procedure TItem.SetStatus(const Value: byte);
begin
  if fStatus <> Value then
  begin
    FStatus := Value;
    Changed;
  end;
end;
(*---------------------------------------------------*)
procedure TForm1.AddItem(aText: string; aIndex: Integer; aParent: TTreeNode);
var n: TTreeNode;
  data: TItem;
  parentId: integer;
begin
  parentId := 0;
  if (aParent <> nil) and (TItem(aParent.Data) <> nil) then
    parentId := TItem(aParent.Data).ID;
  data := TItem.Create(AdoQuery1, -1, parentId, 0);
  data.NodeText := aText;
  data.ImageIndex := aIndex;
  n := TTreeNode.Create(TreeView1.Items);
  n.Text := data.NodeText;
  n.ImageIndex := data.ImageIndex;
  n.SelectedIndex := data.ImageIndex;
  TreeView1.Items.AddNode(n, aParent, aText, data, naAddChild);
end;
(*---------------------------------------------------*)
procedure TForm1.Button1Click(Sender: TObject);
begin
  loadTree(treeview1);
end;
(*---------------------------------------------------*)
procedure TForm1.Button2Click(Sender: TObject);
begin
  savetree(treeview1);
end;
(*---------------------------------------------------*)
procedure TForm1.FileBtnClick(Sender: TObject);
begin
  if (TreeView1.Selected <> nil) and
     (TreeView1.Selected.ImageIndex = 15) then
  begin
    if NewItemFrm.ShowModal = mrOk then
     AddItem(NewItemFrm.Edit1.Text ,17,TreeView1.Selected);
  end else
    ShowMessage('You must select a folder first');
end;
 
(*---------------------------------------------------*)
function TForm1.findNode(t: TTreeView; id: integer): TTreeNode;
var i:integer;
begin
  i:=0;
  while (i<t.items.count) and (TItem(t.items[i].data).ID<>id) do
    inc(i);
  if i<t.items.count then result:=t.items[i]
                     else result:=nil;
end;
(*---------------------------------------------------*)
procedure TForm1.FolderBtnClick(Sender: TObject);
 begin
  if (TreeView1.Selected <> nil) and
     (TreeView1.Selected.ImageIndex = 15) or
      (TreeView1.Selected.ImageIndex = 18) then
  begin
    if (NewFolderFrm.ShowModal = mrOk ) then
      AddItem(NewFolderFrm.Edit2.Text ,15,TreeView1.Selected);
  end else
    ShowMessage('You must select a folder first');
end;
(*---------------------------------------------------*)
procedure TForm1.FormCreate(Sender: TObject);
var
  conn_str:string;
begin
  conn_str:='provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=';
  conn_str:=conn_str+IncludeTrailingPathDelimiter(ExtractFilePath(Application.Exename));
  conn_str:=conn_str+'test.pkd;';
  ADOConnection1.Connected:=False;
  ADOConnection1.ConnectionString:=conn_str;
  ADOConnection1.Connected:=True;
  ADOConnection1.Open;
end;
(*---------------------------------------------------*)
procedure TForm1.loadtree(t: TTreeView);
var
  p, n: TTreeNode;
  data: TItem;
begin
  t.Items.Clear;
  ADOQuery1.SQL.Text := 'select * from tree order by parent asc';
  ADoQuery1.Open;
  while not ADoQuery1.Eof do
  begin
    data := TItem.LoadFromDataset(AdoQuery1);
    p := FindNode(t, data.ParentID);
    n := t.items.AddChildObject(p, data.NodeText, data);
    n.ImageIndex := data.ImageIndex;
    n.SelectedIndex := data.ImageIndex;
    AdoQuery1.Next;
  end;
  AdoQuery1.Close;
end;
(*---------------------------------------------------*)
procedure TForm1.saveNode(n: TTreeNode);
var i, aParent: integer;
  item: TItem;
begin
  item := TItem(n.Data);
  if item <> nil then
  begin
    item.BeginUpdate;
    try
      Item.NodeText := n.Text;
      aParent := 0;
      if (n.Parent <> nil) and (TItem(n.Parent.Data) <> nil) then
        aParent := TItem(n.Parent.Data).ID;
      Item.ParentID := aParent;
      Item.ImageIndex := n.ImageIndex;
    finally
      Item.EndUpdate;
    end;
  end;
  for i := 0 to n.Count-1 do SaveNode(n.Item[i]);
end;
(*---------------------------------------------------*)
procedure TForm1.savetree(t: ttreeview);
var n: TTreeNode;
begin
  n := t.items.getFirstNode;
  while n <> nil do
  begin
    SaveNode(n);
    n := n.getNextSibling;
  end;
end;
(*---------------------------------------------------*)
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
  RichEdit1.Text := '';
  if (Node <> nil) and (Node.ImageIndex = 17) then
  begin
    RichEdit1.Enabled := True;
    RichEdit1.Text := TItem(Node.Data).Data
  end
    else RichEdit1.Enabled := False;
end;
(*---------------------------------------------------*)
 
end.

Open in new window

Here is an example on adding a node in a TreeView in runtime and making it visible
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;
 
type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    btnAddNode: TButton;
    procedure btnAddNodeClick(Sender: TObject);
  private
    { Private declarations }
    procedure AddNode(ParentNode : TTreeNode ; NodeText : string; ImageIndex : Integer);
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
{----------------------------------------------------------------------}
procedure TForm1.AddNode(ParentNode: TTreeNode; NodeText: string;ImageIndex: Integer);
var
  NewNode : TTreeNode;
begin
  if Assigned(ParentNode) then
  begin
   NewNode := TreeView1.Items.AddChild(ParentNode,NodeText);
   NewNode.ImageIndex    := ImageIndex;//Node ImageIndex
   NewNode.SelectedIndex := ImageIndex;//Node ImageIndex when selected
   NewNode.StateIndex    := ImageIndex;//Node ImageIndex when state changed;
   TreeView1.SetFocus;
   TreeView1.Selected := NewNode;
   NewNode.MakeVisible;
  end;
end;
{----------------------------------------------------------------------}
procedure TForm1.btnAddNodeClick(Sender: TObject);
begin
  //if the treeview has a selected node and the node level  is zero which means that is a root node.
 if Assigned(Treeview1.Selected) and (TreeView1.Selected.Level = 0) then
  begin
    AddNode(TreeView1.Selected,'New Node',0);
  end;
end;
{----------------------------------------------------------------------}
 
end.

Open in new window

Peter KiersOperator

Author

Commented:
gtrifidis, I don't want to be a pain in the...
But could you use adjust my code.

Thank you in advanced. Peter Kiers
Peter KiersOperator

Author

Commented:
I did some modification myself and i got this:

procedure TForm1.AddItem(aText: string; aIndex: Integer; aParent: TTreeNode);
var n: TTreeNode;
  data: TItem;
  parentId: integer;
begin
  parentId := 0;
  if (aParent <> nil) and (TItem(aParent.Data) <> nil) then
    parentId := TItem(aParent.Data).ID;
  data := TItem.Create(AdoQuery1, -1, parentId, 0);
  data.NodeText := aText;
  data.ImageIndex := aIndex;
  n := TTreeNode.Create(TreeView1.Items);
  n.Text := data.NodeText;
  n.ImageIndex := data.ImageIndex;
  n.SelectedIndex := data.ImageIndex;
  TreeView1.Items.AddNode(n, aParent, aText, data, naAddChild);
  TreeView1.SetFocus;
  TreeView1.Selected := n;
  n.MakeVisible;
end;

And it works.

Greetings, Peter Kiers

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial