?? unsort.pas
字號(hào):
{**********************************************************************
LISTVIEW排序相關(guān)方法
代碼名稱: LISTVIEW排序
編譯工具: Delphi 6.0
作者: 鄧普德
版權(quán): 成都四方信息技術(shù)有限公司
定義時(shí)間: 2006-08-02
修改時(shí)間: 2006-08-06
**********************************************************************}
unit unSort;
interface
uses
comctrls, Sysutils,ExtCtrls;
type
TSortStyle = (ssAlpha,ssNumeric,ssDateTime);
{
ssAlpha: 按照字符進(jìn)行排序
ssNumeric: 按照數(shù)字進(jìn)行排序
ssDateTime: 按照時(shí)間進(jìn)行排序
}
PSortInfo = ^TSortInfo;
TSortInfo = record
Col : Integer;
Style : TSortStyle;
Asc : Boolean;
end;
procedure SortListView(ListView:TListView; ColumnIndex:Integer;
Style: TSortStyle; Ascending: Boolean);
//設(shè)置ListView中的CheckBox()選擇框的通用方法
procedure ExecSetLVItems(
var vListView: TListView;
rgSelect: TRadioGroup;
strSource:String;
cmpType:Integer);
//根據(jù)條件設(shè)置ListView中的CheckBox()選擇框的通用方法
procedure SetLVItemsByCondition(
var vListView:TListView;
strTarget,strSource:String;
cmpType:Integer;indexListView:Integer);
implementation
function ListViewCompare(I1, I2: TListItem; Data: Integer): Integer; stdcall;
var
V1, V2: string;
function Sign(Val: Extended): Integer;//判斷是大于/小于/等于0
begin
if Val < 0 then
Result := -1
else if Val > 0 then
Result := 1
else
Result := 0;
end;
function ExtractNum(const S: string): string;//獲取字符串中前面的數(shù)字部分
var
i, j: Integer;
begin
j := 0;
for i := 1 to Length(S) do
if S[i] in ['0'..'9'] then
Inc(j)
else
Break;
if j = 0 then
Result := '0'
else
Result := Copy(S,1,j);
end;
begin
with PSortInfo(Data)^ do
begin
if Col = 0 then
begin
V1 := I1.Caption;
V2 := I2.Caption;
end else
begin
V1 := I1.SubItems[Col-1];
V2 := I2.SubItems[Col-1];
end;
case Style of
ssAlpha : Result := AnsiCompareText(V1,V2);
ssNumeric : Result := Sign(StrToFloat(ExtractNum(V1))-StrToFloat(ExtractNum(V2)));
ssDateTime : Result := Sign(StrToDateTime(V1) - StrToDateTime(V2));
else
Result := 0;
end;
if not Asc then
Result := -Result;
end;
end;
procedure SortListView(ListView:TListView; ColumnIndex:Integer;
Style: TSortStyle; Ascending: Boolean);
{排序ListView,ColumnIndex:排序列索引號(hào),
Style 排序方式:按字符,按數(shù)值,按日期(日期格式為
SysUtils.ShortDataTimeFmt,缺省為YY-MM-DD);
Ascending:=True按升序,否則按降序}
var
FSortInfo:TSortInfo;
begin
FSortInfo.Col := ColumnIndex;
FSortInfo.Style := Style;
FSortInfo.Asc := Ascending;
try
ListView.CustomSort(@ListViewCompare,LongInt(@FSortInfo));
except
end;
end;
//根據(jù)條件設(shè)置ListView中的CheckBox()選擇框的通用方法
procedure SetLVItemsByCondition(
var vListView:TListView;
strTarget,strSource:String;
cmpType:Integer;indexListView:Integer);
var
ps:Integer;
begin
Ps := pos(strTarget,strSource);
if Ps =0 then
begin
vListView.Items[indexListView].Checked := false;
Exit;
end;
Case cmpType of
0:
begin
vListView.Items[indexListView].Checked := (ps = 1);
end;
1:
begin
vListView.Items[indexListView].Checked :=
(strTarget = Copy(strSource,
Length(strSource) - Length(strTarget) + 1,
Length(strTarget)));
end;
2:
begin
vListView.Items[indexListView].Checked := true;
end;
end;
end;
procedure ExecSetLVItems(
var vListView: TListView;
rgSelect: TRadioGroup;
strSource:String;
cmpType:Integer);
var
I:Integer;
begin
if rgSelect.ItemIndex = 0 then
begin
For I := 0 to vListView.Items.Count -1 do
begin
SetLVItemsByCondition(vListView,
strSource,vListView.Items[I].Caption,cmpType,I);
end;
end
else if rgSelect.ItemIndex = 1 then
begin
For I := 0 to vListView.Items.Count -1 do
begin
SetLVItemsByCondition(vListView,
strSource,vListView.Items[I].SubItems[0],cmpType,I);
end;
end
else if rgSelect.ItemIndex = 2 then
begin
For I := 0 to vListView.Items.Count -1 do
begin
SetLVItemsByCondition(vListView,
strSource,vListView.Items[I].SubItems[1],cmpType,I);
end;
end;
end;
end.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -