2010-08-06 25 views
1

J'utilise ce code avec TCheckListbox (lbServices) et cela fonctionne très bien. Mais avec TcxCheckListBox de Devexpress, il déclenche une exception.De TCheckListBox à TcxCheckListBox avec une exception?

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; 

Si je déboguer le code lbServices.Count = 12. lbServices.Items.Objects [i] est nulle pour tous les éléments de la liste. Quel est le problème ici?

Répondre

4

Utilisez le code suivant à la place:

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; 
+0

Merci, ça marche bien! –

0

Il n'y a pas objets propriété de TcxCheckListBox.Items

+0

Alors, comment dois-je ajouter des objets à ce composant puis ? –

+0

Vous avez raison Roland. Désolé j'ai oublié de dire que: Je vérifie cela avec la librairie ExpressEditors 4. – SimaWB