delphi 如何从TControlList中删除选定的字段或项?

wpx232ag  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(207)

我是第一次使用TControlList组件。有人能帮助我吗?
例如,如何从TControlList中删除选定的字段或项?
我将SQLLite数据库中的项目填充到TControlList,如下所示:

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;

jm81lzqq

jm81lzqq1#

您可以使用TControlList.ItemIndex取得选取的项目。
一种解决方案是创建一个方法,删除fdmedcin中的条目,然后像示例中那样重新设置TControlList.ItemCount

form1.ControlList1.ItemCount := fdmedcin.RecordCount;

我不知道你的确切代码,但它可能看起来像这样:

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;

换句话说就是:必须将TControlList.ItemIndex与DataSet/whatever索引匹配。
我用TList<T>做了一个简单的例子,而Tinteger

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;

相关问题