gpt4 book ai didi

delphi - 从 TCheckListBox 到 TcxCheckListBox 有异常吗?

转载 作者:行者123 更新时间:2023-12-01 17:57:23 26 4
gpt4 key购买 nike

我将此代码与 TCheckListbox (lbServices) 一起使用,并且工作正常。但使用 Devexpress 的 TcxCheckListBox 会引发异常。

procedure TMaintenanceForm.AfterConstruction;
var
i: Integer;
ActionObj: TAction;
begin
inherited;
for i := 0 to ServiceActionList.ActionCount-1 do
begin
ActionObj := ServiceActionList.Actions[i] as TAction;
lbServices.Items.AddObject(ActionObj.Caption, ActionObj);
end;
end;

procedure TMaintenanceForm.btnStopClick(Sender: TObject);
begin
fContinue := False;
end;

procedure TMaintenanceForm.cmdExecuteSelectedClick(Sender: TObject);
var
i: Integer;
begin
Screen.Cursor := crHourGlass;
try
for i := 0 to lbServices.Count -1 do
if lbServices.Selected[i] then
(lbServices.Items.Objects[i] as TAction).Execute; // Exception here!!!!
finally
Screen.Cursor := crDefault;
end;
end;

如果我调试代码 lbServices.Count = 12。对于列表中的所有项目,lbServices.Items.Objects[i] 均为 nil。这里出了什么问题?

最佳答案

请改用以下代码:

var
AItem: TcxCheckListBoxItem;
begin
AItem := cxCheckListBox1.Items.Add;
AItem.ItemObject := Action1;
AItem.Text := Action1.Caption;
end;

...

var
I: Integer;
begin
for I := 0 to cxCheckListBox1.Items.Count - 1 do
if cxCheckListBox1.Items[I].Checked then
(cxCheckListBox1.Items[I].ItemObject as TACtion).Execute;
end;

关于delphi - 从 TCheckListBox 到 TcxCheckListBox 有异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3423189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com