Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

TElXTree: New last item is automatically selected after deleting the last item

I used the following to delete items:

    with ItemTree.Items do
    begin
      for i := Pred(Count) downto 0 do
      begin
        if Item[i].Checked then
        begin
          Continue;
        end;

        if Item[i].Selected then
        begin
          DeleteTimeData(Item[i]);
          Delete(Item[i]);
        end;
      end;
    end;

This works as long as the last item is not included. Why?

Because after deleting the last item, the new last item becomes automatically selected.

If the last item is not included, then no item becomes automatically selected, so this is an anomaly that may need to be corrected.

Raymond.

Comments

  • 2 Comments sorted by Votes Date Added
  • >>This works as long as the last item is not included. Why?<<
    This depends on your settings for AlwaysKeepsFocus and AlwaysKeepSelection.

    >>If the last item is not included, then no item becomes automatically selected, so this is an anomaly that may need to be corrected.<<
    No. This is a feature. Simply adapt your deletion method. Here is a quick&drirty example which should work with almost all property setting combinations:

    var
      i: Integer;
      obj: TElXTreeItem;
      list: TList;
    begin
      ItemTree.Items.BeginUpdate;
      try
        list := TList.Create;
        try
          for i := 0 to ItemTree.Items.Count -1 do
            if ItemTree.Items[i].Selected then list.Add(ItemTree.Items[i]);
          while list.Count > 0 do
          begin
            Itemtree.Items.DeleteItem(TElXTreeItem(list.First));
            list.Remove(list.First);
          end;
        finally
          list.Free;
        end;
      finally
        ItemTree.Items.EndUpdate;
      end;
    end;

    When AlwaysKeepFocus and AlwaysKeepSelection are both set to true, the feature of always selected element (when Count > 0) and deselecting of items is preserved.



Sign In or Register to comment.