TListView: submenu order after adding a new column between existing ones at runtime

If you add a new column between existing columns at run time, the sub-element indexes are not like the intended ones.

eg. after adding a new column between the second and third columns, the columns / subitems look like this:

colums[0] | colums[1] | (new) columns[2] | columns[3] caption | subitems[0] | subitems[2] | subitems[1] 

but I would suggest:

 colums[0] | colums[1] | (new) columns[2] | columns[3] caption | subitems[0] | subitems[1] | subitem[2] 

I need to be able to dynamically update the contents of a subitem in some conditions. Therefore, I would like to rely on the assumption that the subitem for a column with Column.Index = X is in Item.SubItems [X-1].

Do you think this is the default and assigned behavior? If so, what do you suggest for updating sub-items according to the columns. It is possible to save the index of a subitem that belongs to recently added columns.

Note. The Columns.Tag property is already in use.

I use Delphi XE and XE2, but I need to be compatible with Delphi 7 and above.

+4
source share
1 answer

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.

+3
source

Source: https://habr.com/ru/post/1383889/


All Articles