文章目录
- 控件设置
- 增加数据
- 删除选中的行
- 按标题删除
- 读取某行某列内容
- 修改某行某列内容
- 清空内容
- 设置焦点
- 上下移动条目
- 读出选中行信息
- 点击正反排序
- 每行item显示不一样的颜色
控件设置
- ViewStyle 设为vsReport
- ReadOnly 设为True
- GridLines 设为True
- RowSelect 设为True
增加数据
vari : integer;
begin//增加i := ListView1.Items.Count;with ListView1.Items.Add dobeginCaption:= IntToStr(i);SubItems.Add('第 '+IntToStr(i)+' 行');SubItems.Add('第三列内容');end;
end;
删除选中的行
vari : integer;
beginfor i:=ListView1.Items.Count-1 downto 0 do //直接用to会报错if ListView1.Items[i].Selected then //i=ListView1.Selected.indexbeginListView1.Items.Delete(i); //删除当前选中行end;
end;
按标题删除
vari : integer;
begin//按标题删除for i:=ListView1.Items.Count-1 downto 0 Doif ListView1.Items[i].Caption = Edit1.Text thenbeginListView1.Items.Item[i].Delete(); //删除当前选中行end;
end;
读取某行某列内容
beginEdit1.Text := listview1.Items[0].Caption; //读第i行第1列Edit2.Text := listview1.Items[0].SubItems.strings[0]; //读第i行第2列Edit3.Text := listview1.Items[0].SubItems.strings[1]; //读第i行第3列
end;
修改某行某列内容
begin//必须原先存在数据,才能去修改,可以用Add('')新建数据初始化listview1.Items[0].Caption:= Edit1.Text;listview1.Items[0].SubItems.strings[0]:= Edit2.Text;listview1.Items[0].SubItems.strings[1]:= Edit3.Text;
end;
清空内容
beginListView1.Clear;
end;
设置焦点
//类似手动选中某一行
beginListView1.SetFocus;ListView1.ItemIndex:=3;
end;
上下移动条目
procedure ListViewItemMoveUpDown(lv: TListView; Item: TListItem; MoveUp, SetFocus: Boolean);
varDestItem: TListItem;
beginif (Item = nil) or ((Item.Index - 1 < 0) and MoveUp) or ((Item.Index + 1 >= lv.Items.Count) and (not MoveUp)) thenExit;lv.Items.BeginUpdate;tryif MoveUp thenDestItem := lv.Items.Insert(Item.Index - 1)elseDestItem := lv.Items.Insert(Item.Index + 2);DestItem.Assign(Item);lv.Selected := DestItem;Item.Free;finallylv.Items.EndUpdate;end;if SetFocus thenlv.SetFocus;DestItem.MakeVisible(False);
end;procedure TForm3.Button8Click(Sender: TObject);
beginListViewItemMoveUpDown(ListView1, ListView1.Selected, True, True);//上移
end;procedure TForm3.Button9Click(Sender: TObject);
beginListViewItemMoveUpDown(ListView1, ListView1.Selected, False, True);//下移
end;
读出选中行信息
vari: integer;
beginif ListView1.Selected <> nil thenbeginmemo1.Lines.Add('Caption:' + ListView1.Selected.Caption);for I := 0 to ListView1.Selected.SubItems.Count-1 dobeginmemo1.Lines.Add('SubItems:' + ListView1.Selected.SubItems[i]);end;end;
end;
点击正反排序
m_bSort: boolean = false; // 控制正反排序的变量function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
vartxt1, txt2: string;Num1, Num2: Integer;
beginif ParamSort <> 0 then //点击subItemsbegintrytxt1 := Item1.SubItems.strings[ParamSort - 1];txt2 := Item2.SubItems.strings[ParamSort - 1];if m_bSort thenbeginResult := CompareText(txt1, txt2);end else beginResult := -CompareText(txt1, txt2);end;exceptend;endelse //点击captionbegintxt1:= Item1.Caption;txt2:= Item2.Caption;// 将字符串转换为数字Num1 := StrToIntDef(txt1, MaxInt);Num2 := StrToIntDef(txt2, MaxInt);// 进行数字比较Result := Num1 - Num2;//如果都不是数字安装默认排序法排序if (Num1 = MaxInt) and (Num2 = MaxInt) thenResult := AnsiCompareText(txt1, txt2);if m_bSort thenbeginResult:= -Result;endend;
end;procedure TForm3.ListView1ColumnClick(Sender: TObject; Column: TListColumn);
beginListView1.CustomSort(@CustomSortProc, Column.Index);m_bSort := not m_bSort;
end;
每行item显示不一样的颜色
添加AdvancedCustomDrawItem事件
procedure TForm3.ListView1AdvancedCustomDrawItem(Sender: TCustomListView; Item: TListItem;State: TCustomDrawState; Stage: TCustomDrawStage; var DefaultDraw: Boolean);
beginif Odd(Item.Caption.ToInteger) thenbeginSender.Canvas.Font.Color := clWindowText;Sender.Canvas.Font.Name := '宋体';Sender.Canvas.Font.Size := 12;Sender.Canvas.Brush.Color := $d0d0f2;endelsebeginSender.Canvas.Font.Color := clWindowText;Sender.Canvas.Font.Name := '宋体';Sender.Canvas.Font.Size := 12;Sender.Canvas.Brush.Color := $e6c8c8;end
end;
最后上个Demo合照,点击下载Demo源码