2011-04-24 74 views
3

我創建了ListView並添加了複選框以檢查每一行(如果需要)。一切都很好,直到列表視圖中有很多項目才需要放大。我在線檢查,然後我看到,如果我滾動滾動條,複選框不會與其他內容滾動。複選框不在TListView組件中滾動

我的代碼:

unit ShareCommissionDialog; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, StdCtrls, sButton, ComCtrls, sListView, sCheckBox, DataManager; 

type 
    TdlgShareCommission = class(TForm) 
    lvEmployees: TsListView; 
    btnCancel: TsButton; 
    btnDone: TsButton; 
    procedure FormCreate(Sender: TObject); 
    procedure FormShow(Sender: TObject); 
    procedure FormClose(Sender: TObject; var Action: TCloseAction); 
    procedure btnDoneClick(Sender: TObject); 
    procedure btnCancelClick(Sender: TObject); 

    private 
    manager: TDataManager; 
    ListViewCheckBoxList: TList; 
    employeeIDs: TStringList; 

    procedure ListViewCheckBoxCreate(List: TsListView; employeeID: string; R: TRect); 
    procedure ListViewCheckBoxClick(Sender: TObject); 
    procedure ListViewCheckBoxSelected(item: Integer; List: TsListView); 
    procedure ListViewCheckBoxUnselected(item: Integer; List: TsListView); 

    procedure PopulateEmployees; 
    procedure ShowSelectedEmployees; 
    procedure MarkRows; 

    public 
    property DataManager: TDataManager read manager write manager; 
    property CommissionReceivers: TStringList read employeeIDs write employeeIDs; 

    end; 

var 
    dlgShareCommission: TdlgShareCommission; 

implementation 

{$R *.dfm} 

procedure TdlgShareCommission.btnCancelClick(Sender: TObject); 
begin 
    employeeIDs := employeeIDs; 
end; 

procedure TdlgShareCommission.btnDoneClick(Sender: TObject); 
var 
    i, j: Integer; 

begin 
    employeeIDs := TStringList.Create; 

    for i := 0 to ListViewCheckBoxList.Count - 1 do 
    begin 
    if TsCheckBox(ListViewCheckBoxList[i]).Checked then 
    begin 
     for j := 0 to manager.Data.Employees.Count - 1 do 
     begin 
     if TsCheckBox(ListViewCheckBoxList[i]).Tag = StrToInt(manager.Data.Employees[j].ID) then 
     begin 
      employeeIDs.Add(manager.Data.Employees[j].ID); 
      Break; 
     end; 
     end; 
    end; 
    end; 
end; 

procedure TdlgShareCommission.FormClose(Sender: TObject; 
    var Action: TCloseAction); 
begin 
    manager.GetEmployees(manager.Data.SelectedPOS.ID); 
end; 

procedure TdlgShareCommission.FormCreate(Sender: TObject); 
begin 
    ListViewCheckBoxList := TList.Create; 
end; 

procedure TdlgShareCommission.ListViewCheckBoxCreate(List: TsListView; employeeID: string; R: TRect); 
function NewViewCheckBox: TsCheckBox; 
    begin 
    Result := TsCheckBox.Create(Self); 
    Result.Tag := StrToInt(employeeID); 
    Result.Parent := List; 
    Result.BoundsRect := R; 
    Result.Checked := False; 

    Result.OnClick := ListViewCheckBoxClick; 
    end; 
begin 
    ListViewCheckBoxList.Add(NewViewCheckBox); 
end; 

procedure TdlgShareCommission.FormShow(Sender: TObject); 
begin 
    if employeeIDs <> nil then 
    begin 
    PopulateEmployees; 
    ShowSelectedEmployees; 
    MarkRows; 
    end 
    else 
    begin 
    PopulateEmployees; 
    MarkRows; 
    end; 

    //PopulateEmployees; 
    //MarkRows; 
end; 

procedure TdlgShareCommission.ListViewCheckBoxClick(Sender: TObject); 
var 
    i: Integer; 

begin 
    for i := 0 to ListViewCheckBoxList.Count - 1 do 
    begin 
    if (i+1) <= (ListViewCheckBoxList.Count - 1) then 
     if not TsCheckBox(ListViewCheckBoxList[i+1]).Enabled then 
     begin 
     TsCheckBox(ListViewCheckBoxList[i+1]).Checked := TsCheckBox(ListViewCheckBoxList[i]).Checked; 
     end; 
    end; 

    MarkRows; 
end; 

procedure TdlgShareCommission.MarkRows; 
var 
    j: Integer; 

begin 
    for j := 0 to lvEmployees.Items.Count - 1 do 
    begin 
    if not TsCheckBox(ListViewCheckBoxList[j]).Checked then 
    begin 
     ListViewCheckBoxSelected(j, lvEmployees); 
    end 
    else 
    begin 
     ListViewCheckBoxUnselected(j, lvEmployees); 
    end; 
    end; 
end; 

procedure TdlgShareCommission.ListViewCheckBoxSelected(item: Integer; List: TsListView); 
begin 
    with List do 
    begin 
    Items[item].Selected := False; 
    end; 
end; 

procedure TdlgShareCommission.ListViewCheckBoxUnselected(item: Integer; List: TsListView); 
begin 
    with List do 
    begin 
    Items[item].Selected := False; 
    end; 
end; 

procedure TdlgShareCommission.PopulateEmployees; 
var 
    i: Integer; 
    R: TRect; 
    employeeRow: TListItem; 

begin 
    with lvEmployees do 
    try 
    Items.BeginUpdate; 
    Items.Clear; 

    manager.GetEmployees(manager.Data.SelectedPOS.ID); 

    for i := 0 to manager.Data.Employees.Count - 1 do 
    begin 
     employeeRow := Items.Add; 

     R := Items[i].DisplayRect(drBounds); 
     R.Left := 5; 
     R.Top := R.Top + 1; 
     ListViewCheckBoxCreate(lvEmployees, manager.Data.Employees[i].ID, R); 
     Items[i].Data := TsCheckBox(ListViewCheckBoxList[i]); 

     employeeRow.SubItems.Add(manager.Data.Employees[i].FirstName + ' ' + manager.Data.Employees[i].LastName); 
    end; 
    finally 
    lvEmployees.Items.EndUpdate; 
    end; 
end; 

procedure TdlgShareCommission.ShowSelectedEmployees; 
var 
    i, j: Integer; 

begin 
    for j := 0 to lvEmployees.Items.Count - 1 do 
    begin 
    for i := 0 to employeeIDs.Count - 1 do 
    begin 
     if TsCheckBox(ListViewCheckBoxList[j]).Tag = StrToInt(employeeIDs[i]) then 
     begin 
     TsCheckBox(ListViewCheckBoxList[j]).Checked := True; 
     end; 
    end; 
    end; 
end; 
end. 

我需要什麼要補充的檢查框也將與其他內容滾動代碼?

+0

什麼是'TsListView'? – 2011-04-24 16:50:54

+0

它是由http://www.alphaskins.com/ – evilone 2011-04-24 17:10:25

+0

製作的自定義ListView組件。我建議您聯繫控制供應商以獲取支持。 – 2011-04-24 17:12:52

回答

6

標準 ListView有一個名爲checkboxes的屬性,可以設置爲true。
如果你使用它,那麼你不必自己製作自己的複選框。
我從來沒有在此控件中顯示不正確的複選框的任何問題。

這個屬性最適合在ViewStyle:= vsListViewStyle:= vsReport

或者,如果你想在某些項目,但不能在其他複選框 然後做一個複選框的圖片(1選中,1個選中),他們都添加到ImageList中。
如果項目需要複選框,請將其ImageIndex屬性設置爲未選中項目的屬性。如果需要檢查,請將其ImageIndex屬性設置爲選中的圖像。
這適用於ViewStyle:= vsReportViewStyle:= vsList

您正在使用自定義ListView TsListView控件,請嘗試使用標準ListView來查看是否顯示出奇怪的行爲。

+0

好吧,但如果我想添加一個Listitem而不是複選框的按鈕?這個TsListView是skinnable組件,像標準ListView一樣工作。 – evilone 2011-04-24 17:13:00

+2

@evilone,相同的處理方式,爲每個按鈕的狀態創建圖像,並根據需要更改「ImageIndex」。 – Johan 2011-04-24 17:47:04