谁能给个不错的LISTVIEW点击列头排序的源代码

zyandzf 2004-08-26 10:59:37
谁能给个不错的LISTVIEW点击列头排序的源代码
感激不尽
谢谢
zhouying1981@163.net
...全文
218 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
constantine 2004-08-26
  • 打赏
  • 举报
回复
---------------------------------------------------------------

你需要在class里设置2个变量,一个int变量表示当前排序的列号,一个bool
变量表示当前是升序还是降序排列。当用户点击的列号和保存的列号不同时,
为升序排列,同时保存排序的列号;当用户点击的列号和保存的列号相同时,
将bool变量取反,然后再排序。这儿我写个例子:

class TForm1 : public TForm
{
__published: // IDE-managed Components
TListView *ListView1;
void __fastcall ListView1ColumnClick(TObject *Sender,
TListColumn *Column);
void __fastcall ListView1Compare(TObject *Sender, TListItem *Item1,
TListItem *Item2, int Data, int &Compare);
private: // User declarations
int SortCol;
bool Ascend;
bool __fastcall CustomSort(PFNLVCOMPARE SortProc, int lParam);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};

//-------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
SortCol = -1; Ascend = true;
}
//-------------------------------------------------------------
void __fastcall TForm1::ListView1ColumnClick(TObject *Sender,
TListColumn *Column)
{
if (SortCol != Column->Index) {
SortCol = Column->Index;
Ascend = true;
}
else
Ascend = !Ascend;
ListView1->CustomSort(NULL, 0);
}
//-------------------------------------------------------------
void __fastcall TForm1::ListView1Compare(TObject *Sender,
TListItem *Item1, TListItem *Item2, int Data, int &Compare)
{
if (SortCol == -1)
Compare = 0; //不排序
else if(SortCol == 0)
Compare = CompareText(Item1->Caption,Item2->Caption);
else
Compare = CompareText(Item1->SubItems->Strings[SortCol
-1], Item2->SubItems->Strings[SortCol-1]);
if (!Ascend) Compare = -Compare; //降序排列
}
wu_07 2004-08-26
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/3300/3300860.xml?temp=.9795496

13,870

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧