一个非常常见的功能,点击列表头排序;
1. 先定义两个变量
int ColumnToSort;
bool desc;
2. 设置ListView的ColumnClick属性为true;
3. 处理OnColumnClick事件
void __fastcall TFMain::ListView1ColumnClick(TObject *Sender,
TListColumn *Column)
{
ColumnToSort=Column->Index;
if (Column->Tag==0)
{
Column->Tag =1;
}
else
Column->Tag =0;
desc = Column->Tag;
((TCustomListView*)Sender)->AlphaSort();
}
4. 处理OnCompare事件
void __fastcall TFMain::ListView1Compare(TObject *Sender, TListItem *Item1,
TListItem *Item2, int Data, int &Compare)
{
if (!desc)
{
if(ColumnToSort==0)
Compare=CompareText(Item1->Caption,Item2->Caption);
else
{
int ix=ColumnToSort-1;
Compare=CompareText(Item1->SubItems->Strings[ix],Item2->SubItems->Strings[ix]);
}
}
else
{
if(ColumnToSort==0)
Compare=-CompareText(Item1->Caption,Item2->Caption);
else
{
int ix=ColumnToSort-1;
Compare=-CompareText(Item1->SubItems->Strings[ix],Item2->SubItems->Strings[ix]);
}
}
}
效果很理想。
完全参照bcb帮助实现的。