delphi – TListView:在运行时在现有之间添加新列之后的子项顺
如果在运行时期间在现有列之间添加新列,则子项索引不会像我假设的那样.
例如在第二列和第三列之间添加新列后,列/子项如下所示: colums[0] | colums[1] | (new) columns[2] | columns[3] caption | subitems[0] | subitems[2] | subitems[1] 但我会假设: colums[0] | colums[1] | (new) columns[2] | columns[3] caption | subitems[0] | subitems[1] | subitem[2] 我需要能够在某些条件下动态更新子项的内容.这就是为什么我想依赖于这样的假设,即Column.Index = X的列的子项位于Item.SubItems [X-1]. 你认为这是默认和指定的行为吗?如果是这样,你会建议根据列更新子项.可能是保存属于最近添加的列的子项索引. 注意:Columns.Tag属性已在使用中. 我正在使用Delphi XE和XE2,但我需要与Delphi 7及更高版本兼容. 解决方法
您不需要保存索引位置,您可以随时询问列表视图控件本身列的原始位置:
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)); 对于问题中的示例,ColumnOrder数组将保持(0,1,3,2).如果我们想要更新新插入列的子项(左起第3列),则其原始位置为“3”.代码示例: var ColumnOrder: array of Integer; SubIndex: Integer; begin SetLength(ColumnOrder,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; 请注意,如果您要添加列而不仅仅是重新排序现有列,这只有在您修复了other question中的错误时才会起作用(如果不修复,则会再次执行,同时提供列重新排序和列添加无论如何都无法实现功能. 关于默认行为是否应该如此,假设您有一个列表视图,您正在显示具有列’name’,’size’,’date’的文件信息.作为开发人员,您不应该担心用户可能放置“大小”列的位置,只需将信息放入“SubItems [0]”即可.此外,如果用户拖动“名称”列,它将从项目降级为子项目. 我认为期望项目/子项目遵循各自的列是合乎逻辑的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |