2010-08-06 21 views
1

我使用TCheckListbox(lbServices)這個代碼,它工作正常。但是使用Devexpress的TcxCheckListBox會引發異常。從TCheckListBox到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]是對列表中的所有項爲零。這裏有什麼問題?

回答

4

使用下面的代碼來代替:

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

謝謝,它工作正常! – 2010-08-06 12:07:48

0

沒有對象TcxCheckListBox.Items

財產
+0

那麼應該怎麼對象添加到這個組件然後 ? – 2010-08-06 11:24:29

+0

你是對的羅蘭。對不起,我忘了說:我使用ExpressEditors Library 4進行了檢查。 – SimaWB 2010-08-06 11:31:35

相關問題