Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7814853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:19:58+00:00 2026-06-02T05:19:58+00:00

I would like to program the following situation: I have 2 different ListViews in

  • 0

I would like to program the following situation:

I have 2 different ListViews in a form. I would like to attach specific items from ListView2 into a ListView1 item. After the “Parent” Item gets deleted, it should also delete all the attached items from ListView2.
I tried this so far:

type
 TITEMS = record
 A_Items : array of TListItem;
end;

A Button that adds an item to ListView1 (ParentItems)

var
 item : TListItem;
begin
 item := ListView1.Items.Add;
 item.Caption := 'ParentTestItem';
 item.SubItems.Add('TestSubItem');

A button that adds an item to ListView2 (ChildItems)

 var
  item : TlistItem;
  items : TITEMS;
 begin
  if ListView1.Selected = NIL then exit; // Make sure an item is selected.
  item := ListView2.Items.Add;
  item.Caption := 'ChildTestItem';
  item.SubItems.Add('TestSubItem');
  SetLength (items.item, Length(items.item) + 1); // wrong? 
  items.item[Length(items.item)-1] := item;
  ListView1.Selected.SubItems.Objects[0] := @items;

A button that removes a ParentItem (and it should delete ChildItems as well…)

  var
   items : TItems;
   i : Integer;
   item : TlistItem;
  begin
   if ListView1.Selected = NIL then exit; // Make sure an item is selected.
   items := TItems(ListView1.Selected.SubItems.Objects[0]); // Cast
   for i := 0 to Length (items.item) - 1 do begin
    item := items.item[i];
    item.Delete;
   end;
   ListView1.Selected.Free;

Any Idea how I could realize this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:20:00+00:00Added an answer on June 2, 2026 at 5:20 am

    You need to allocate the list of items dynamically on the heap, not locally on the stack, so it stays valid in memory while you are using it.

    I would suggest using a TList instead of an array, it is easier to allocate dynmically. I would also suggest using the TListItem.Data property instead of the TListItem.SubItems.Objects[] property (unless you are already using the Data property for something else).

    procedure TForm1.AddParentBtnClick(Sender: TObject);
    var 
      item : TListItem; 
    begin 
      item := ListView1.Items.Add; 
      item.Caption := 'ParentTestItem'; 
      item.SubItems.Add('TestSubItem'); 
    end;
    
    procedure TForm1.AddChildBtnClick(Sender: TObject);
    var 
      Selected, item : TListItem; 
      items : TList; 
    begin 
      Selected := ListView1.Selected;
      if Selected = nil then Exit; // Make sure an item is selected. 
    
      items := TList(Selected.Data);
      if items = nil then begin
        items := TList.Create;
        Selected.Data := items;
      end;
    
      item := ListView2.Items.Add; 
      try
        item.Caption := 'ChildTestItem'; 
        item.SubItems.Add('TestSubItem'); 
        items.Add(item); 
      except
        item.Delete;
        raise;
      end;
    end;
    
    procedure TForm1.DeleteParentBtnClick(Sender: TObject);
    var
      Selected : TListItem; 
    begin
      Selected := ListView1.Selected;
      if Selected <> nil then Selected.Delete;
    end;
    
    procedure TForm1.ListView1Deletion(Sender: TObject; Item: TListItem);
    var 
      items : TList; 
      i : Integer; 
    begin 
      items := TList(Item.Data); // Cast 
      if items <> nil then begin
        for i := 0 to items.Count - 1 do begin 
         TListItem(items[i]).Delete; 
        end; 
        items.Free;
        Item.Data := nil;
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to execute some program through ssh and redirect its input from
I would like to create a program in a linux/unix environment that runs from
I have a situation where I would like to have the main thread waiting,
Situation: I have a C# program which does the following: Generate many files (replacing
The following program works correctly. However, I would like to change it to not
I would like to try TWO different things (both have a strong possibility of
I would like to do the following: In a FORTRAN program call myPackageFunction(MPI_User_function) ...shall
I would like the following functionality: I have a datagrid, and when I go
I would like to do the following . I have declared a structure in
I would like to implement a simulation program, which requires the following structure: It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.