gpt4 book ai didi

delphi - TListView 组件中的复选框不滚动

转载 作者:行者123 更新时间:2023-12-03 15:48:57 26 4
gpt4 key购买 nike

我创建了 ListView 并添加了复选框来检查每一行(如果需要)。一切都很好,直到 ListView 中有很多项目,因此需要对其进行排序。我在线检查,然后发现如果滚动滚动条,复选框不会与其他内容一起滚动。

我的代码:

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.

我需要在代码中添加什么以使复选框也能与其他内容一起滚动?

最佳答案

标准 ListView 有一个名为 checkboxes 的属性,可以将其设置为 true。
如果您使用它,那么您就不必费力制作自己的复选框。
我从未遇到过复选框在此控件中无法正确显示的问题。

此属性在 ViewStyle:= vsListViewStyle:= vsReport 中效果最佳

或者,如果您想要某些项目上的复选框而不是其他项目上的复选框然后制作一个复选框的图片(1 个未选中,1 个选中)并将它们都添加到 ImageList 中。
如果某个项目需要复选框,请将其 ImageIndex 属性设置为未选中项目的 ImageIndex 属性。如果需要勾选,则将其ImageIndex属性设置为勾选的Image。
这适用于 ViewStyle:= vsReportViewStyle:= vsList

您正在使用自定义 ListView(TsListView) 控件,请尝试使用标准 ListView 来代替,看看是否会出现奇怪的行为。

关于delphi - TListView 组件中的复选框不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5771601/

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