How exactly can a selected field or item from the TControlList be deleted?

350 Views Asked by At

I'm new to using component TControlList. Can someone help with this.

How exactly can a selected field or item from the TControlList be deleted for example?

I'm filling the items to TControlList from SQLLite database like this:

procedure TForm1.ControlList1BeforeDrawItem(AIndex: Integer; ACanvas: TCanvas;
  ARect: TRect; AState: TOwnerDrawState);
begin
  dm.fdmedcin.RecNo := AIndex+1;
  lbl5.Caption := 'Spc:' +TStringField( dm.fdmedcin.FieldByName('Spc_doc') ).AsString ;

.........

procedure TDM.fdmedcinAfterOpen(DataSet: TDataSet);
begin
  form1.ControlList1.ItemCount := fdmedcin.RecordCount;

enter image description here

1

There are 1 best solutions below

1
On

You get the selected item with TControlList.ItemIndex.

One solution could be that you create a method that deletes the entry in your fdmedcin and re-set the TControlList.ItemCount again like in your example.

form1.ControlList1.ItemCount := fdmedcin.RecordCount;

I don't know your exact code but it could look like this:

procedure TForm1.Delete;
begin
  dm.fdmedcin.RecNo := ControlList1.ItemIndex + 1; // seems like you add one in your OnBeforeDrawItem too
  dm.fdmedcin.Delete;
  ControlList1.ItemCount := fdmedcin.RecordCount;
end;

In other words: you have to match the TControlList.ItemIndex with your DataSet/whatever index.

I made a simple example with a TList<T> while T is an integer:

type
  TForm2 = class(TForm)
    BFill: TButton;
    ControlList1: TControlList;
    Label1: TLabel;
    Panel1: TPanel;
    BDelete: TButton;
    MSQuery1: TMSQuery;
    procedure BFillClick(Sender: TObject);
    procedure ControlList1BeforeDrawItem(AIndex: Integer; ACanvas: TCanvas; ARect: TRect; AState: TOwnerDrawState);
    procedure FormCreate(Sender: TObject);
    procedure BDeleteClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FList: TList<integer>;
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.BDeleteClick(Sender: TObject);
begin
  FList.Delete(ControlList1.ItemIndex);
  ControlList1.ItemCount := FList.Count;
end;

procedure TForm2.BFillClick(Sender: TObject);
begin
  ControlList1.ItemCount := FList.Count;
end;

procedure TForm2.ControlList1BeforeDrawItem(AIndex: Integer; ACanvas: TCanvas; ARect: TRect; AState: TOwnerDrawState);
begin
  Label1.Caption := IntToStr(FList[AIndex]);
end;

procedure TForm2.FormCreate(Sender: TObject);
var
  i: integer;
begin
  FList := TList<integer>.Create;
  for i := 0 to 9 do
    FList.Add(i);
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  FList.Free;
end;