Added Tree items won't show when tree view is expanded

1k Views Asked by At

I have an application for iOS/android developed with Delphi XE8 FMX.

In the app I have a treeview with tree items. When I add a tree item to a node when that same parent node is expanded, then I need to either a) collapse and expand the parent node from the app itself or b) do that programmatically ( see below ) to get the tree item to show right away. I tried calling 'repaint', but that did not work. Is there a better work around? Sometimes when calling the collapseall, expandall like I do below, then some of the tree nodes become unresponsive ( non-selectable ) until after I first select the top most tree node.

procedure TnewForm.AddTreeItemClick(Sender: TObject);
var
  t:TTreeViewItem;
begin
  t:=TTreeViewItem.Create(nil);
  t.Text:=NewTreeItemEdit.Text;
  if TreeView.Selected<>nil then
  begin
    t.Parent:=TreeView.Selected
  end else
    t.Parent:=TreeView;
  //Treeview.Repaint;
  treeview.CollapseAll;
  treeview.ExpandAll;
  NewTreeItemEdit.Text:='';
end;

How can I make dynamically added tree items show right away without collapsing/expanding the treeview?

3

There are 3 best solutions below

0
Dsm On

You could try this.

procedure TnewForm.AddTreeItemClick(Sender: TObject);
var
  t:TTreeViewItem;
begin
  t:=TTreeViewItem.Create(nil);
  t.Text:=NewTreeItemEdit.Text;
  if TreeView.Selected<>nil then
  begin
    t.Parent:=TreeView.Selected
  end else
    t.Parent:=TreeView;
  //Treeview.Repaint;
  treeview.Selected := t;
  NewTreeItemEdit.Text:='';
end;
3
NGLN On

Dsm's solution works, but changes the TreeView's selection. If you want the selection to remain unchanges, but instead just that the added item is instantly visible, then expand its parent:

procedure TForm1.AddTreeItemClick(Sender: TObject);
var
  T :TTreeViewItem;
begin
  T := TTreeViewItem.Create(nil);
  T.Text := NewTreeItemEdit.Text;
  if TreeView.Selected <> nil then
  begin
    T.Parent := TreeView.Selected;
    TreeView.Selected.IsExpanded := True;
  end else
    T.Parent := TreeView;
  NewTreeItemEdit.Text := '';
end;
0
Hans On

Similar problem with Windows / XE8. The workaround that did it for me:

  if Assigned(TV.Selected) then
    begin
      N := TTreeviewItem.Create(Self);
      N.Text := 'Another child';
      N.Parent := TV.Selected;
      N.IsExpanded := TRUE; // This seems to do the trick
    end;

Hopefully this will get attention in next releases.