• c++ builder TListView控件按字符串排序(根据网上代码亲测ok)


     1 //---------------------------------------------------------------------------
     2 /*
     3 首先将一个列表框控件安放在Form上,并将其名称设为ListView1。
     4 然后在其中添加若干项目作为试验对象。方法为:用鼠标右键单击控件,
     5 在弹出的对话框中选择Columns Editor用来添加列和子列;
     6 再选Items Editor用来添加项目(包含子列和主列上的内容)。
     7 为了显示出子项目内容,需要在Object Inspector中修改ListView属性值,
     8 将ViewStyle设置为vsReport。另外,还必须将SortType设置为None,
     9 以使我们的排序程序起作用。这样,程序的外观已经符合需要,
    10 下面应该增加排序功能的代码了。为此需要添加类的方法到源代码中。
    11  */
    12 void __fastcall TFormVBS::listViewColumnClick(TObject *Sender, TListColumn *Column)
    13 {
    14     int i,m,n,j;
    15     static bool od=true;
    16 
    17     /* od代表用户点击的次数,奇数时为true,偶数时为false。
    18     注意此处od的存储类型设定为static,可以保证其数值的连续性。
    19     用n记录用户点击的列号,m记录列表框中的总项目数。*/
    20     od=!od;
    21 
    22     n=Column->Index;
    23     m=listView->Items->Count;
    24 
    25     /* 在列表框中临时添加一个项目作为排序中交换用的临时空间 */
    26     listView->Items->Add();
    27 
    28     /* 当用户点击第一列列标头时,
    29     排序按listView->Items->Item[i]->Caption进行,
    30     与其它列不同,所以要单独进行排序。 */
    31 
    32     if (n==0)
    33     {
    34         for(i=0;i < m-1;i++)
    35         {
    36             for(j=i+1;j< m;j++)
    37             {
    38                 if(od)
    39                 {
    40                     if(listView->Items->Item[i]->Caption >
    41                     listView->Items->Item[j]->Caption)
    42                     {
    43                         listView->Items->Item[m]= listView->Items->Item[i];
    44                         listView->Items->Item[i]= listView->Items->Item[j];
    45                         listView->Items->Item[j]= listView->Items->Item[m];
    46                     }
    47                 }
    48                 else
    49                 {
    50                     if(listView->Items->Item[i]->Caption < listView->Items->Item[j]->Caption)
    51                     {
    52                         listView->Items->Item[m]= listView->Items->Item[i];
    53                         listView->Items->Item[i]= listView->Items->Item[j];
    54                         listView->Items->Item[j]= listView->Items->Item[m];
    55                     }
    56                 }
    57             }
    58         }
    59     }
    60 
    61     listView->Items->Delete(m);
    62     return;
    63     /* 编译运行程序后,即会看到我们预期的结果。
    64     另外,本程序是按照字符串方式进行排序的,如果需要按照数字或其它方式排序,
    65     只需进行相应的类型转换即可。
    66     理解本程序后,读者即掌握了对ListView控件编程的一条基本思路,
    67     对今后使用BCB以及对Windows编程起到良好作用。 */
    68 }
    69 //---------------------------------------------------------------------------
  • 相关阅读:
    学习CLR Via C#的一些体会
    ScrollView动画滚动
    使用blend自定义symbol
    Silverlight中消除ToolTip的白色背景
    nil,NULL,NSNull的区别
    app store,Mac app store 下载加速的方法
    发布时NSLog不打印信息
    TestFlight的使用步骤
    “Could not change executable permissions on the application”的原因和解决方法
    iOS6地图“查看路线”、导航功能的实现
  • 原文地址:https://www.cnblogs.com/kernel0815/p/3281602.html
Copyright © 2020-2023  润新知