KidList
PointList
NoteList
CommentList
CommentListKid
我该怎么办?
KidList
PointList
NoteList
CommentList
CommentListKid
我该怎么办?
您可以尝试以下技术。
首先,添加一个私人字段
private
SyncBoxes: TArray<TListBox>;
添加到表单,并在创建表单时对其进行初始化:
procedure TForm1.FormCreate(Sender: TObject);
begin
SyncBoxes := [ListBox1,ListBox2,ListBox3,ListBox4];
end;
然后定义以下插入器类:
type
TListBox = class(Vcl.StdCtrls.TListBox)
strict private
procedure Sync;
protected
procedure WMVScroll(var Message: TWMVScroll); message WM_VSCROLL;
procedure CNCommand(var Message: TWMCommand); message CN_COMMAND;
procedure WMMouseWheel(var Message: TWMMouseWheel); message WM_MOUSEWHEEL;
end;
实现为
procedure TListBox.CNCommand(var Message: TWMCommand);
begin
inherited;
if Message.NotifyCode = LBN_SELCHANGE then
Sync;
end;
procedure TListBox.Sync;
var
LB: TListBox;
begin
for LB in Form1.SyncBoxes do
if LB <> Self then
LB.TopIndex := Self.TopIndex;
end;
procedure TListBox.WMMouseWheel(var Message: TWMMouseWheel);
begin
inherited;
Sync;
end;
procedure TListBox.WMVScroll(var Message: TWMVScroll);
begin
inherited;
Sync;
end;
当然,在真实的应用中,您可以重构它。
结果可能足够好:
但是,列表框的滚动动画使同步稍微延迟了。