Delphi ListView的用法

來源:互聯網
上載者:User

 

//增加
  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 < 0) and MoveUp) 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; 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.