我试图在TListView中添加一列。为此,我将新列添加到末尾,并通过将其索引设置为指定值来移动它。这有效,直到添加另一列。
我的操作步骤如下:将列添加到最后位置(Columns.Add),同时在最后位置添加子项(Subitems.Add)。然后,通过将其索引设置为正确的位置来移动该列。只要添加的是一个新列,就能正常工作。但是当添加第二列时,子项会出现问题。第一列的新子项会被移动到最后位置,例如:
我很乐意帮忙翻译!
例如,是否有一种命令或消息可以发送给ListView以刷新或保存其列->子项映射?这样,在添加第一个新列及其子项后,我可以使用该命令处理第二个新列,就像处理第一个新列一样。
还是这只是TListViews的列->子项处理或TListColumns的一个错误?
以下是一个VCL表单应用程序的示例代码(将Form1.OnCreate事件分配):
我的操作步骤如下:将列添加到最后位置(Columns.Add),同时在最后位置添加子项(Subitems.Add)。然后,通过将其索引设置为正确的位置来移动该列。只要添加的是一个新列,就能正常工作。但是当添加第二列时,子项会出现问题。第一列的新子项会被移动到最后位置,例如:
0 | 1 | new A | new B | 3
Caption | old sub 1 | old sub 3 | new Sub B | new sub A
我很乐意帮忙翻译!
例如,是否有一种命令或消息可以发送给ListView以刷新或保存其列->子项映射?这样,在添加第一个新列及其子项后,我可以使用该命令处理第二个新列,就像处理第一个新列一样。
还是这只是TListViews的列->子项处理或TListColumns的一个错误?
以下是一个VCL表单应用程序的示例代码(将Form1.OnCreate事件分配):
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
listview: TListView;
initButton: TButton;
addColumn: TButton;
editColumn: TEdit;
subItemCount: Integer;
procedure OnInitClick(Sender: TObject);
procedure OnAddClick(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
listview := TListView.Create(self);
with listview do
begin
Left := 8;
Top := 8;
Width := self.Width - 30;
Height := self.Height - 100;
Anchors := [akLeft, akTop, akRight, akBottom];
TabOrder := 0;
ViewStyle := vsReport;
Parent := self;
end;
initButton := TButton.Create(self);
with initButton do
begin
left := 8;
top := listview.Top + listview.Height + 20;
Width := 75;
Height := 25;
TabOrder := 1;
Caption := 'init';
OnClick := OnInitClick;
Parent := self;
end;
editColumn := TEdit.Create(self);
with editColumn do
begin
left := initButton.Left + initButton.Width + 30;
top := listview.Top + listview.Height + 20;
Width := 120;
Height := 25;
TabOrder := 2;
Parent := self;
Caption := '';
end;
addColumn := TButton.Create(self);
with addColumn do
begin
left := editColumn.Left + editColumn.Width + 10;
top := listview.Top + listview.Height + 20;
Width := 75;
Height := 25;
TabOrder := 1;
Enabled := true;
Caption := 'add';
OnClick := OnAddClick;
Parent := self;
end;
end;
procedure TForm1.OnInitClick(Sender: TObject);
var col: TListColumn;
i, j: integer;
item: TListItem;
begin
listview.Items.Clear;
listview.Columns.Clear;
// add items
for I := 0 to 2 do
begin
col := ListView.Columns.Add;
col.Caption := 'column ' + IntToStr(i);
col.Width := 80;
end;
// add columns
for I := 0 to 3 do
begin
item := ListView.Items.Add;
item.Caption := 'ItemCaption';
// add subitems for each column
for j := 0 to 1 do
begin
item.SubItems.Add('subitem ' + IntToStr(j+1));
end;
end;
subItemCount := 5;
end;
procedure TForm1.OnAddClick(Sender: TObject);
var number: integer;
col: TListColumn;
i: Integer;
ascii: char;
begin
listview.Columns.BeginUpdate;
number := StrToInt(editColumn.Text);
ascii := Chr(65 + number);
// create the new column
col := TListColumn(ListView.Columns.add());
col.Width := 80;
col.Caption := ascii;
// add the new subitems
for I := 0 to ListView.Items.Count-1 do
begin
ListView.Items[i].SubItems.Add('subitem ' + ascii);
end;
// move it to the designated position
col.Index := number;
listview.Columns.EndUpdate;
Inc(subItemCount);
end;
end.
谢谢!
编辑:Sertac Akyuz提出的建议修复方案很好,但我不能使用它,因为更改Delphi源代码对我的项目来说不是解决方案。已报告错误。
编辑:删除了无意中包含在第一个帖子中的第二个问题,并开了一个新问题(请参见链接的问题和问题修订)。