You do not need to save the index position, you can always request the control of the list of the initial position of the column yourself:
procedure TForm1.Button1Click(Sender: TObject); var ColumnOrder: array of Integer; begin SetLength(ColumnOrder, ListView1.Columns.Count); ListView_GetColumnOrderArray(ListView1.Handle, ListView1.Columns.Count, PInteger(ColumnOrder));
For example, in the question, the ColumnOrder
array will contain (0, 1, 3, 2). If we want to update the subtype of a newly inserted column (third column on the left), then its initial position is β3β. Code example:
var ColumnOrder: array of Integer; SubIndex: Integer; begin SetLength(ColumnOrder, ListView1.Columns.Count); ListView_GetColumnOrderArray(ListView1.Handle, ListView1.Columns.Count, PInteger(ColumnOrder)); SubIndex := ColumnOrder[2]; // We want to update 3rd column from left // (visually -> SubItems[1]) // Test if the index is not 0, otherwise it holds an *item*, // not a subitem (the first column can change position too). if SubIndex > 0 then begin Dec(SubIndex); // VCL subitems are 0 based ListView1.Items[1].SubItems[SubIndex] := 'updated!'; end;
Please note: if you add columns, and not just reorder existing ones, this will only work if you have a bug fix in another question (again, if you do not, providing both reordering columns and adding column functions is generally not possible at all).
As for the default behavior, as it should be, suppose you have a list view in which you display information about a file with column names, sizes, date. As a developer, you should not worry about where the user could put the βsizeβ column, just put this information in βSubItems [0]β. Moreover, what if the user drags the "name" column, will it go down from element to sub-element.
I think it is logical to expect the elements / sub-elements to follow their respective columns.