2010/03/16

[轉載]Delphi ListView基本用法大全

作者:不明,取自CSDN轉載
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//增加項或列(字段)
ListView1.Clear;
ListView1.Columns.Clear;
ListView1.Columns.Add;
ListView1.Columns.Add;
ListView1.Columns.Add;
ListView1.Columns.Items[0].Caption := 'id';
ListView1.Columns.Items[1].Caption := 'type';
ListView1.Columns.Items[2].Caption := 'title';
ListView1.Columns.Items[2].Width := 300;
Listview1.ViewStyle := vsreport;
Listview1.GridLines := true;
  //注:此處代碼也可以直接在可視化編輯器中完成,也可寫成以下這樣
 
begin
  with listview1 do
  begin
    Columns.Add;
    Columns.Add;
    Columns.Add;
    ViewStyle := vsreport;
    GridLines := true;
    columns.items[0].caption := ' 進程名';
    columns.items[1].caption := '進程ID';
    columns.items[2].caption := ' 進程文件路徑';
    Columns.Items[0].Width := 100;
    Columns.Items[1].Width := 100;
    Columns.Items[2].Width := 150;
  end
end;
 
//增加記錄
with listview1.items.add do
begin
  caption := '1212';
  subitems.add('hh1');
  subitems.add('hh2');
end;
 
//刪除
listview1.items.delete(0);
 
//從數據庫表裡讀取數據寫入Listview
var
  Titem: Tlistitem; //此處一定要預定義臨時記錄存儲變量.
begin
  ListView1.Items.Clear;
  with adoquery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select spmc,jg,sl from kcxs');
    Open;
    ListView1.Items.Clear;
    while not eof do
    begin
      Titem := ListView1.Items.add;
      Titem.Caption := FieldByName('spmc').Value;
      Titem.SubItems.Add(FieldByName('sl').Value);
      Titem.SubItems.Add(FieldByName('jg').Value);
      next;
    end;
 
    //刪除
    ListView1.DeleteSelected;
  end;
end;
 
//如何取得ListView中選中行的某一列的值
 
procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage(ListView1.Selected.SubItems.Strings[1]); //返回選中行第三列中的值
end;
 
showMessage(listView1.Selected.Caption); //返回選中行第一列的值.
 
//第1列的值: - - > > > 
  ListView1.Selected.Caption
//第i列的值(i > 1): - - > > > 
  ListView1.Selected.SubItems.Strings[i]
 
ListView1.Items.Item[1].SubItems.GetText); //取得listview某行某列的值
 
Edit2.Text := listview1.Items[i].SubItems.strings[0]; //讀第i行第2列
 
//返回選中行所有子列值.是以回車符分開的,你還要從中剝離出來你要的子列的值。
 
showMessage(ListView1.Selected.SubItems.GetText);
 
//ListView 簡單排序的實現
 
//ListView 排序
 
//怎樣實現單擊一下按升序,再單擊一下按降序。
 
function CustomSortProc(Item1, Item2: TListItem; ColumnIndex: integer): integer;
  stdcall;
begin
  if ColumnIndex = 0 then
    Result := CompareText(Item1.Caption, Item2.Caption)
  else
    Result := CompareText(Item1.SubItems[ColumnIndex - 1],
      Item2.SubItems[ColumnIndex - 1])
end;
 
procedure TFrmSrvrMain.ListView1ColumnClick(Sender: TObject;
  Column: TListColumn);
begin
  ListView1.CustomSort(@CustomSortProc, Column.Index);
end;
 
//===============================================================
 
//增加
i := ListView1.Items.Count;
with ListView1 do
begin
  ListItem := Items.Add;
  ListItem.Caption := IntToStr(i);
  ListItem.SubItems.Add('第 ' + IntToStr(i) + ' 行');
  ListItem.SubItems.Add(' 第三列內容');
end;
 
//按標題刪除
for i := ListView1.Items.Count - 1 downto 0 do
  if ListView1.Items[i].Caption = Edit1.Text then
  begin
    ListView1.Items.Item[i].Delete(); //刪除當前選中行
  end;
 
//選中一行
if ListView1.Selected <> nil then
  Edit1.Text := ListView1.Selected.Caption;
 
// listview1.Items[Listview1.Items.Count -1].Selected := True;
// listview1.Items[Listview1.Items.Count -1].MakeVisible(True);
 
procedure TForm1.Button2Click(Sender: TObject); // 選擇第一條
begin
  listview1.SetFocus;
  listview1.Items[0].Selected := True;
end;
 
procedure TForm1.Button1Click(Sender: TObject); // 選擇最後一條
begin
  listview1.SetFocus;
  listview1.Items[Listview1.Items.Count - 1].Selected := True;
end;
 
//這是個通用的過程
 
procedure ListViewItemMoveUpDown(lv: TListView; Item: TListItem; MoveUp,
  SetFocus: Boolean);
var
  DestItem: TListItem;
begin
  if (Item = nil) or
    ((Item.Index - 1 <> = lv.Items.Count) and (not MoveUp)) then
    Exit;
  lv.Items.BeginUpdate;
  try
    if MoveUp then
      DestItem := lv.Items.Insert(Item.Index - 1)
    else
      DestItem := lv.Items.Insert(Item.Index + 2);
    DestItem.Assign(Item);
    lv.Selected := DestItem;
    Item.Free;
  finally
    lv.Items.EndUpdate;
  end;
  if SetFocus then
    lv.SetFocus;
  DestItem.MakeVisible(False);
end;
 
//此為調用過程,可以任意指定要移動的Item,下面是當前(Selected)Item
ListViewItemMoveUpDown(ListView1, ListView1.Selected, True, True); //上移
ListViewItemMoveUpDown(ListView1, ListView1.Selected, False, True); //下移
 
//TListView組件使用方法
 
//引用CommCtrl單元
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  ListView_DeleteColumn(MyListView.Handle, i); //i是要刪除的列的序號,從0開始
 
end;
 
//用LISTVIEW顯示表中的信息:
 
procedure viewchange(listv: tlistview; table: tcustomadodataset; var i:
  integer);
begin
  tlistview(listv).Items.BeginUpdate; {listv:listview名}
  try
    tlistview(listv).Items.Clear;
    with table do {table or query名}
    begin
      active := true;
      first;
      while not eof do
      begin
        listitem := tlistview(listv).Items.add;
        listitem.Caption := trim(table.fields[i].asstring);
        // listitem.ImageIndex:=8;
        next;
      end;
    end;
  finally
    tlistview(listv).Items.EndUpdate;
  end;
end;
 
//ListView使用中的一些要點。以下以一個兩列的ListView為例。
//→ 增加一行:
with ListView1 do
begin
  ListItem := Items.Add;
  ListItem.Caption := ' 第一列內容';
  ListItem.SubItems.Add('第二列內容');
end;
//→清空ListView1:
ListView1.Items.Clear;
//→得到當前被選中行的行的行號以及刪除當前行:
for i := 0 to ListView1.Items.Count - 1 do
  if ListView1.Items[i].Selected then //i=ListView1.Selected.index
  begin
    ListView1.Items.Delete(i); //刪除當前選中行
  end;
//當然,ListView有 OnSelectItem事件,可以判斷選擇了哪行,用個全局變量把它賦值出來。
//→讀某行某列的操作:
Edit1.Text := listview1.Items[i].Caption; //讀第i行第1列
Edit2.Text := listview1.Items[i].SubItems.strings[0]; //讀第i行第2列
Edit3.Text := listview1.Items[i].SubItems.strings[1]; //讀第i行第3列
//以次類推,可以用循環讀出整列。
//→ 將焦點上移一行:
for i := 0 to ListView1.Items.Count - 1 do
  if (ListView1.Items[i].Selected) and (i > 0) then
  begin
    ListView1.SetFocus;
    ListView1.Items.Item[i - 1].Selected := True;
  end;
//不過在Delphi6 中,ListView多了一個ItemIndex屬性,所以只要
ListView1.SetFocus;
ListView1.ItemIndex := 3;
//就能設定焦點了。
 
//Delphi的listview能實現交替顏色麼?
 
procedure TForm1.ListView1CustomDrawItem(
  Sender: TCustomListView; Item: TListItem; State: TCustomDrawState;
  var DefaultDraw: Boolean);
var
  i: integer;
begin
  i := (Sender as TListView).Items.IndexOf(Item);
  if odd(i) then
    sender.Canvas.Brush.Color := $02E0F0D7
  else
    sender.Canvas.Brush.Color := $02F0EED7;
  Sender.Canvas.FillRect(Item.DisplayRect(drIcon));
end;

沒有留言:

張貼留言