6

I'm trying to add a column between existing columns in a TListView. Therefor I add the new column at the end and move it by setting it`s index to the designated value. This works, until adding another new column.

What I did: Add the column at last position (Columns.Add) and add the subitem at the last position (Subitems.Add) too. Afterwards I move the column by setting it's index to the correct position. This works fine as long as it's just one column that gets added. When adding a second new column, the subitems get screwed up. The new subitem of the first column is moved to the last position, e.g. like this:

0        |  1          |  new A       |  new B      | 3
Caption  |  old sub 1  |  old sub 3   |  new Sub B  | new sub A

I would be very happy if someone could help!

For example, is there maybe a command or message I can send to the ListView so it refreshes or saves it's Column --> Subitem mapping that I could use after adding the first new column and it's subitems so I can handle the second new column the same way as the first.

Or is this just a bug of TListViews column-->subitem handling or TListColumns...?

example code for a vcl forms application (assign the Form1.OnCreate event):

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.

Thank you!


Edit: The suggested fix from Sertac Akyuz works fine, though I can't use it because changing the Delphi sourcecode is no solution for my project. Bug is reported.

Edit: Removed the second question that was unintended included in the first post and opened new question (See linked question and Question-revision).

Update: The reported bug is now closed as fixed as of Delphi XE2 Update 4.

Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
torno
  • 466
  • 12
  • 25
  • I guess there's a missing refresh/update somewhere. Not sure what it is though. That said, this sounds like another case where virtual mode list views would shine. – David Heffernan Nov 24 '11 at 14:27
  • But they are only available for .Net, aren't they? i got the same problem with equivalent C#.Net project and maybe can use it there. – torno Nov 24 '11 at 14:35
  • No. Windows list view supports virtual mode and Delphi wraps it up very nicely. If you are manipulating columns at runtime it's definitely the way to go. Everyone else here would point you at virtual tree view but I like the native control myself. – David Heffernan Nov 24 '11 at 14:43
  • ok, thanks. i`ll have a look at this but i don't think i can use it because the project is quite large and changing one of it`s core components might not be the best idea :) – torno Nov 24 '11 at 14:59
  • Had a look through the VCL code and it does seem that somewhere along the line of updating a column's index, the VCL's data gets out of sync with the windows listview data where the subitems are concerned. Playing around with the Begin/EndUpdate has an effect, unfortunately so far not the desired one. Your best bet may indeed be to put the ListView in virtual mode as @David suggested. That way your app is always asked for the data it needs to show in each cell and there is no hidden "copy" in the vcl or windows. – Marjan Venema Nov 24 '11 at 20:34
  • It sounds like you should stop using the List View and use a real grid. – Warren P Nov 25 '11 at 19:45
  • thanks for the suggestions, but using virtual mode or a grid is no option. – torno Nov 28 '11 at 16:26

1 Answers1

7

Call the UpdateItems method after you've arranged the columns. E.g.:

..
col.Index := number;
listview.UpdateItems(0, MAXINT);
..



Update:

In my tests, I still seem to need the above call in some occasion. But the real problem is that "there is a bug in the Delphi list view control".

Duplicating the problem with a simple project:

  • Place a TListView control on a VCL form, set its ViewStyle to 'vsReport' and set FullDrag to 'true'.
  • Put the below code to the OnCreate handler of the form:
    ListView1.Columns.Add.Caption := 'col 1';
    ListView1.Columns.Add.Caption := 'col 2';
    ListView1.Columns.Add.Caption := 'col 3';
    ListView1.AddItem('cell 1', nil);
    ListView1.Items[0].SubItems.Add('cell 2');
    ListView1.Items[0].SubItems.Add('cell 3');
    
  • Place a TButton on the form, and put the below code to its OnClick handler:
    ListView1.Columns.Add.Caption := 'col 4';
  • Run the project and drag the column header of 'col 3' to in-between 'col 1' and 'col 2'. The below picture is what you'll see at this moment (everything is fine):

    list view after column drag

  • Click the button to add a new column, now the list view becomes:

    list view after adding column

    Notice that 'cell 2' has reclaimed its original position.

Bug:

The columns of a TListView (TListColumn) holds its ordering information in its FOrderTag field. Whenever you change the order of a column (either by setting the Index property or by dragging the header), this FOrderTag gets updated accordingly.

Now, when you add a column to the TListColumns collection, the collection first adds the new TListColumn and then calls the UpdateCols method. The below is the code of the UpdateCols method of TListColumns in D2007 VCL:

procedure TListColumns.UpdateCols;
var
  I: Integer;
  LVColumn: TLVColumn;
begin
  if not Owner.HandleAllocated then Exit;
  BeginUpdate;
  try
    for I := Count - 1 downto 0 do
      ListView_DeleteColumn(Owner.Handle, I);

    for I := 0 to Count - 1 do
    begin
      with LVColumn do
      begin
        mask := LVCF_FMT or LVCF_WIDTH;
        fmt := LVCFMT_LEFT;
        cx := Items[I].FWidth;
      end;
      ListView_InsertColumn(Owner.Handle, I, LVColumn);
      Items[I].FOrderTag := I;
    end;
    Owner.UpdateColumns;
  finally
    EndUpdate;
  end;
end;


The above code removes all columns from the underlying API list-view control and then inserts them anew. Notice how the code assigns each inserted column's FOrderTag the index counter:

      Items[I].FOrderTag := I;

This is the order of the columns from left to right at that point in time. If the method is called whenever the columns are ordered any different than at creation time, then that ordering is lost. And since items do not change their positions accordingly, it all gets mixed up.

Fix:

The below modification on the method seemed to work for as little as I tested, you need to carry out more tests (evidently this fix does not cover all possible cases, see 'torno's comments below for details):

procedure TListColumns.UpdateCols;
var
  I: Integer;
  LVColumn: TLVColumn;
  ColumnOrder: array of Integer;
begin
  if not Owner.HandleAllocated then Exit;
  BeginUpdate;
  try
    SetLength(ColumnOrder, Count);
    for I := Count - 1 downto 0 do begin
      ColumnOrder[I] := Items[I].FOrderTag;
      ListView_DeleteColumn(Owner.Handle, I);
    end;

    for I := 0 to Count - 1 do
    begin
      with LVColumn do
      begin
        mask := LVCF_FMT or LVCF_WIDTH;
        fmt := LVCFMT_LEFT;
        cx := Items[I].FWidth;
      end;
      ListView_InsertColumn(Owner.Handle, I, LVColumn);
    end;
    ListView_SetColumnOrderArray(Owner.Handle, Count, PInteger(ColumnOrder));

    Owner.UpdateColumns;
  finally
    EndUpdate;
  end;
end;

If you are not using packages you can put a modified copy of 'comctrls.pas' to your project folder. Otherwise you might pursue run-time code patching, or file a bug report and wait for a fix.

Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
  • unfortunately, this does not solve the problem :( still the described behavior after adding the second new column can be reproduced with the code above after adding your line. – torno Nov 24 '11 at 16:50
  • did you try the first example? with inserting the column and subitems at the correct position? or the code snippet? unfortunately, i`ve to got now and can try it again on monday... thanks for your suggestion – torno Nov 24 '11 at 16:52
  • @torno - No, I just tested the snippet inserting only one column. Should have read the question throughly... – Sertac Akyuz Nov 24 '11 at 16:54
  • thank you, sertac! that`s a nice point. I`ll try to verify your fix and if sucessful, I`ll report the bug. – torno Nov 28 '11 at 13:35
  • sertac, this works fine for existing items. but if you add a new item and fill the subitems with "item.SubItems.Add('new ' + IntToStr(current_subitem_index))". you`ll see, that the order of the subitems for new items somehow still is messed up. e.g. just add a column to existing 3, move it to index 1, add a new item with subitems. the subitem[0] still has the wrong index "subitems.count-1" instead of 0. i think it may be, because the updatecols is just called after adding the column, not after overwriting it`s index. i`ll try, if inserting the column works. – torno Nov 28 '11 at 13:49
  • inserting doesn't even call the UpdateCols method... but the order for existing subitems is correct (so the bug you fixed, only appears when calling columns.add). after inserting a column at the correct position, adding a new item and it's subitems seems to be buggy though :/ – torno Nov 28 '11 at 14:08
  • @torno - I couldn't duplicate the problem with new item and subitems. That's most probably because I didn't quite understood how to reproduce. In any case the fix is more complex than I initially thought (you can see what I first proposed from the edit history). It's best that Emb would fix this themselves. Or maybe it's fixed already? What version of Delphi you're using? BTW, you don't need to find a resolution or the cause to submit a bug report, a test case to reproduce is quite enough.. – Sertac Akyuz Nov 28 '11 at 15:00
  • i`m using delphi xe and with delphi xe2 it also appeared, so it doesn't seem to be fixed. i`ll report it but unfortunately, i need a fix that is compatible to delphi 7 and higher... :/ thanks for your help. – torno Nov 28 '11 at 15:03
  • this screenshot maybe let's you understand my issue: http://imageshack.us/photo/my-images/46/newitem.png/ instead of subitem index 1, the subitems for new items have the subitem index 2 – torno Nov 28 '11 at 15:08
  • @torno - You're welcome, and thanks for the information about the version. I hope you figure out something soon, and please let us know when you do :) . > *The picture* - Isn't that how it should look like? – Sertac Akyuz Nov 28 '11 at 15:16
  • hmm.. no. if it would be like this, i would`ve to remember, that the subitems for column "C" have the subitem index 2 and not 1 (what i would assume because of the column index) when i want to update their content. – torno Nov 28 '11 at 15:28
  • @torno - I see your point, but this is how it behaves by default, without adding any column (so without revealing the bug in the VCL). Populate a listview, change a column's index, and add items-subitems, you'll see the order of the items-subitems will depend on the ordering of the columns. So I don't think what you want is the intended behavior. – Sertac Akyuz Nov 28 '11 at 15:50
  • you may be right. but it seems strange to me. it means, you have to save the column-->subitem indices for each item when the column order changed and you want to update the subitems later. anyway, thank you really much for your help. i`ll let this post unanswered for some time, maybe someone else can help. if not, you get the answered-tag :) – torno Nov 28 '11 at 16:09
  • @torno - Ok, you're welcome and good luck! One last comment about the edit: I don't see the inconsistency as in the first table. When I iterate subitems, for every row I have them like "subitems[0]|subitems[2]|subitems[1]", but I might have get confused some by now and I may be doing something wrong. :) – Sertac Akyuz Nov 28 '11 at 16:29
  • ok, you are right. my example and the subitem-texts were badly chosen :) i always "add" a subitem for a new column, so it`s at the end. sorry for confusing you. so i maybe don't have to save the indices for each item but for each column. that may be possible. thanks for indicating this. i`ll try to find a solution and if successful, i`ll close this post. thanks again :) – torno Nov 28 '11 at 16:37
  • @torno - **If** you make up your mind about that being the default behavior, I'd then suggest you to consider asking it in a new question. 1st for, then it becomes a different question then the originally asked one. 2nd for, it might get attention from the developers who lost interest in this one :) . – Sertac Akyuz Nov 28 '11 at 18:09
  • you are right, thanks. i`ll open a bug report at embarcadero and maybe will open another thread for the existing issue. thank you – torno Nov 30 '11 at 13:50