Von TCheckListBox zu TcxCheckListBox mit einer Ausnahme?
-
26-09-2019 - |
Frage
Ich benutze diesen Code mit TCheckListBox (lbServices) und es funktioniert gut. Aber mit TcxCheckListBox von DevExpress es erhöht Ausnahme.
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;
Wenn ich den Code lbServices.Count debuggen = 12. lbServices.Items.Objects [i] ist gleich Null für alle Positionen in der Liste. Was ist hier falsch?
Lösung
Verwenden Sie den folgenden Code statt:
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;
Andere Tipps
Es gibt keine Objekte Eigenschaft von TcxCheckListBox.Items
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow