bestlong 怕失憶論壇

 

 

搜索
bestlong 怕失憶論壇 論壇 Delphi Listview
查看: 7107|回復: 4
go

Listview [複製鏈接]

Rank: 9Rank: 9Rank: 9

1#
發表於 2006-11-8 10:02 |只看該作者 |倒序瀏覽 |打印
  1. procedure TfrmMaterialDict.RefreshSubMaterialView(treeNode: TTreeNode);
  2. var
  3.   ItemType: integer;
  4.   objNode: TNodeObject;
  5.   objMaterial :TMaterialObj;
  6.   listItem: TListItem;
  7.   sql: string;
  8.   rs: _Recordset;
  9. begin
  10.   lvMaterial.ViewStyle := vsList;
  11.   lvMaterial.Items.Clear;
  12.   lvMaterial.ViewStyle := vsReport;
  13.   ClearTxt;
  14.   PageControl1.ActivePageIndex := 0;
  15.   if Assigned(treeNode.Data) then
  16.   begin
  17.     objNode := TNodeObject(treeNode.Data);
  18.     if objNode.IS_Depot = 1 then //Ϊ·ÖÀa
  19.     begin
  20.       ItemType := objNode.Sort;
  21.       sql := 'select ID,SORT,MCODE,MNAME, MODAL,UNIT,PRICE,KIND,MAXNUM,MINNUM,DEPOTID,SELLPRICE1,SELLPRICE2,STOCKS from T_MMS_MATERIAL_DICT where SORT = ' + inttostr(ItemType) + 'and Mcode <> 0 order by Mcode';
  22.       rs := ADOConn.Execute(sql);
  23.       while not rs.EOF do
  24.       begin
  25.         objMaterial       := TMaterialObj.Create;
  26.         objMaterial.ID    := rs.Fields['ID'].Value;
  27.         objMaterial.SORT  := rs.Fields['SORT'].Value;
  28.         objMaterial.MCODE := rs.Fields['MCODE'].Value;
  29.         objMaterial.KIND  := rs.Fields['KIND'].Value;
  30.         listItem          := lvMaterial.Items.Add;
  31.         listItem.Caption  := objMaterial.KIND;
  32.         objMaterial.MNAME := rs.Fields['MNAME'].Value;
  33.         listItem.SubItems.Add(objMaterial.MNAME);
  34.         if not VarIsNull(rs.Fields['MODAL'].Value) then
  35.           objMaterial.MODAL := rs.Fields['MODAL'].Value
  36.         else
  37.           objMaterial.MODAL := '';
  38.         listItem.SubItems.Add(objMaterial.MODAL);
  39.         if not VarIsNull(rs.Fields['UNIT'].Value) then
  40.           objMaterial.MUNIT := rs.Fields['UNIT'].Value
  41.         else
  42.           objMaterial.MUNIT :='';
  43.         listItem.SubItems.Add(objMaterial.MUNIT);

  44.         if not VarIsNull(rs.Fields['PRICE'].Value) then
  45.           objMaterial.PRICE := rs.Fields['PRICE'].Value
  46.         else
  47.           objMaterial.PRICE := 0;
  48.         listItem.SubItems.Add(format('%5.2f',[objMaterial.PRICE]));

  49.         if not VarIsNull(rs.Fields['SELLPRICE1'].Value) then
  50.           objMaterial.SELLPRICE1 := rs.Fields['SELLPRICE1'].Value
  51.         else
  52.           objMaterial.SELLPRICE1 := 0;
  53.         listItem.SubItems.Add(format('%5.2f',[objMaterial.SELLPRICE1]));

  54.         if not VarIsNull(rs.Fields['SELLPRICE2'].Value) then
  55.           objMaterial.SELLPRICE2 := rs.Fields['SELLPRICE2'].Value
  56.         else
  57.           objMaterial.SELLPRICE2 := 0;
  58.         listItem.SubItems.Add(format('%5.2f',[objMaterial.SELLPRICE2]));

  59.         if not VarIsNull(rs.Fields['STOCKS'].Value) then
  60.           objMaterial.STOCKS := rs.Fields['STOCKS'].Value
  61.         else
  62.           objMaterial.STOCKS := 0;
  63.         listItem.SubItems.Add(format('%5.2f',[objMaterial.STOCKS]));

  64.         if not VarIsNull(rs.Fields['MAXNUM'].Value) then
  65.           objMaterial.MAXNUM := rs.Fields['MAXNUM'].Value
  66.         else
  67.           objMaterial.MAXNUM := 0;
  68.         listItem.SubItems.Add(format('%5.2f',[objMaterial.MAXNUM]));

  69.         if not VarIsNull(rs.Fields['MINNUM'].Value) then
  70.           objMaterial.MINNUM := rs.Fields['MINNUM'].Value
  71.         else
  72.           objMaterial.MINNUM := 0;
  73.         listItem.SubItems.Add(format('%5.2f',[objMaterial.MINNUM]));

  74.         listItem.Data := Pointer(objMaterial);
  75.         rs.MoveNext;
  76.         lvMaterial.ItemIndex :=0;
  77.         lvMaterialClick(Self);
  78.       end;
  79.       rs.Close;
  80.     end;
  81.   end;
  82. end;
複製代碼


from: http://topic.csdn.net/t/20040614/16/3090833.html

Rank: 9Rank: 9Rank: 9

2#
發表於 2006-11-8 10:22 |只看該作者
  1. //增加
  2. i := ListView1.Items.Count;
  3. with ListView1 do
  4.   begin
  5.   ListItem := Items.Add;
  6.   ListItem.Caption := IntToStr(i);
  7.   ListItem.SubItems.Add('第 ' + IntToStr(i) + ' 行');
  8.   ListItem.SubItems.Add('其他内容');
  9. end;

  10. //依標題刪除
  11. for i := ListView1.Items.Count-1 downto 0 do
  12.   if ListView1.Items[i].Caption = Edit1.Text then
  13.   begin
  14.     ListView1.Items.Item[i].Delete(); //删除當前選中行
  15.   end;

  16. //選中一行
  17. if ListView1.Selected <> nil then
  18.   Edit1.Text := ListView1.Selected.Caption;

  19. // listview1.Items[Listview1.Items.Count - 1].Selected := True;
  20. // listview1.Items[Listview1.Items.Count - 1].MakeVisible(True);
  21. procedure TForm1.Button2Click(Sender: TObject); // 選擇第一行
  22. begin
  23.   listview1.SetFocus;
  24.   listview1.Items[0].Selected := True;
  25. end;

  26. procedure TForm1.Button1Click(Sender: TObject); // 選擇最後一行
  27. begin
  28.   listview1.SetFocus;
  29.   listview1.Items[Listview1.Items.Count - 1].Selected := True;
  30. end;
複製代碼


//這是個通用的函數
  1. procedure ListViewItemMoveUpDown(lv : TListView; Item : TListItem; MoveUp, SetFocus : Boolean);
  2. var
  3.   DestItem : TListItem;
  4. begin
  5.   if (Item = nil) or
  6.   ((Item.Index - 1 < 0) and MoveUp) or
  7.   ((Item.Index + 1 >= lv.Items.Count) and (not MoveUp))
  8.   then Exit;
  9.   lv.Items.BeginUpdate;
  10.   try
  11.     if MoveUp then
  12.       DestItem := lv.Items.Insert(Item.Index - 1)
  13.     else
  14.       DestItem := lv.Items.Insert(Item.Index + 2);
  15.     DestItem.Assign(Item);
  16.     lv.Selected := DestItem;
  17.     Item.Free;
  18.   finally
  19.     lv.Items.EndUpdate;
  20.   end;
  21.   if SetFocus then lv.SetFocus;
  22.   DestItem.MakeVisible(False);
  23. end;

  24. //此為呼叫過程,可以任意指定要移動的Item,下面是當前(Selected)Item
  25. ListViewItemMoveUpDown(ListView1, ListView1.Selected, True, True);//上移
  26. ListViewItemMoveUpDown(ListView1, ListView1.Selected, False, True);//下移
複製代碼


TListView组件使用方法

引用CommCtrl单元
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   ListView_DeleteColumn(MyListView.Handle, i);//i是要删除的列的序号,從0開始
  4. end;

  5. //用LISTVIEW顯示表中的紀錄:
  6. procedure viewchange(listv:tlistview;table:tcustomadodataset;var i:integer);
  7. begin
  8.   tlistview(listv).Items.BeginUpdate; {listv:listview名}
  9.   try
  10.     tlistview(listv).Items.Clear;
  11.     with table do {table or query名}
  12.     begin
  13.       active:=true;
  14.       first;
  15.       while not eof do
  16.       begin
  17.         listitem:=tlistview(listv).Items.add;
  18.         listitem.Caption:=trim(table.fields[i].asstring);
  19.         // listitem.ImageIndex:=8;
  20.         next;
  21.       end;
  22.     end;
  23.   finally
  24.     tlistview(listv).Items.EndUpdate;
  25.   end;
  26. end;
複製代碼


ListView使用中的一些要点。以下以一个两列的ListView为例。
→增加一行:
  1. with ListView1 do
  2. begin
  3.   ListItem:=Items.Add;
  4.   ListItem.Caption:='第一列内容';
  5.   ListItem.SubItems.Add('第二列内容');
  6. end;
複製代碼


→清空ListView1:
ListView1.Items.Clear;

→得到当前被选中行的行的行号以及删除当前行:
  1. For i:=0 to ListView1.Items.Count-1 Do
  2. If ListView1.Items[i].Selected then //i=ListView1.Selected.index
  3. begin
  4.   ListView1.Items.Delete(i); //删除當前選中行
  5. end;
複製代碼

當然,ListView有OnSelectItem事件,可以判断選擇了哪行,用個全局變數把它赋值出来。

→讀某行某列的操作:
Edit1.Text := listview1.Items.Caption; //讀第i行第1列
Edit2.Text := listview1.Items.SubItems.strings[0]; //讀第i行第2列
Edit3.Text := listview1.Items.SubItems.strings[1]; //讀第i行第3列
依次類推,可以用循環讀出整列。

→將焦點上移一行:
  1. For i:=0 to ListView1.Items.Count-1 Do
  2.   If (ListView1.Items[i].Selected) and (i>0) then
  3.   begin
  4.     ListView1.SetFocus;
  5.     ListView1.Items.Item[i-1].Selected := True;
  6.   end;
複製代碼

不過在Delphi6中,ListView多了一个ItemIndex属性,所以只要
ListView1.SetFocus;
ListView1.ItemIndex:=3;
就能設定焦點了。

Delphi的listview能實現交替顏色嗎?
  1. procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
  2. var
  3.   i: integer;
  4. begin
  5.   i:= (Sender as TListView).Items.IndexOf(Item);
  6.   if odd(i) then sender.Canvas.Brush.Color:= E0F0D7
  7.   else sender.Canvas.Brush.Color:= F0EED7;
  8.   Sender.Canvas.FillRect(Item.DisplayRect(drIcon));
  9. end;
複製代碼

Rank: 9Rank: 9Rank: 9

3#
發表於 2006-11-8 10:29 |只看該作者

在 ListView 的 Item 中顯示 CheckBox 與 Button

在ListView的Item中显示CheckBox与Button,主要思路就是在绘制每一个Item时创建一个CheckBox与Button,并显示在Item对应Column的位置.主要注意于3点:

将要显示的CheckBox与Button(其实可以更多其它控件)定义在一个结构中,使用指针连接到Item.Data,以便与该Item对应
要检查Item.Data是否为nil,防止重复创建控件
通过Item.SubItem所对应的Column.Width得出CheckBox与Button应该放置的坐标
代码并不难懂,首先定义一个结构:

type PItemCtrl = ^TItemCtrl
  TtemCtrl = record
     CheckBox: TCheckBox;
     Button: TButton;
   end;
end;

procedure TfrmMain.ListView1CustomDrawSubItem(Sender: TCustomListView; Item: TListItem; SubItem: Integer; State: TCustomDrawState; var DefaultDraw: Boolean);
var
  Rect: TRect;
  P: PItemCtrl;
begin
  { 第2个子项目上显示CheckBox,第5个子项目上显示Button }
  if SubItem in [2, 5] then
  begin
    DefaultDraw:= False;        // 不显示默认的文本.
    Rect:= Item.DisplayRect(drBounds);  // 获取Item显示的区域.

    if Item.Data = nil then     // 如果为空则创建CheckBox及Button.
    begin
      new(P);   // 创建一个指针用于存储CheckBox及Button.

      { 创建并显示CheckBox }
      P.CheckBox:= TCheckBox.Create(ListView1);
      P.CheckBox.Parent:= ListView1;
      P.CheckBox.Caption:= '';
      P.CheckBox.Width:= 20;
      P.CheckBox.Height:= 20;
      P.CheckBox.Left:= Rect.Right - ListView1.Columns[3].Width
        - ListView1.Columns[4].Width - ListView1.Columns[5].Width
        - ((ListView1.Columns[2].Width + P.CheckBox.Width) div 2);
      P.CheckBox.Top:= Rect.Top;
      P.CheckBox.Visible:= True;
      { SubItems[2 -1].Caption为0和1,直接转换为Boolean型并给CheckBox赋值. }
      P.CheckBox.Checked:= StrToBool(Item.SubItems[SubItem -1]);

      { 创建并显示Button }
      P.Button:= TSpeedButton.Create(ListView1);
      P.Button.Parent:= ListView1;
      P.Button.Caption:= '...';
      P.Button.Width:= 20;
      P.Button.Height:= 20;
      P.Button.Left:= Rect.Right - ((ListView1.Columns[5].Width
        + P.Button.Width) div 2);
      P.Button.Top:= Rect.Top;
      P.Button.Visible:= True;

      Item.Data:= P;    // 将CheckBox及Button的结构指针保存于Item.Data属性.
    end;
  end;
end;
我是雪龍
http://blog.bestlong.idv.tw
http://www.bestlong.idv.tw

Rank: 9Rank: 9Rank: 9

4#
發表於 2006-11-8 11:48 |只看該作者
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   ListItem: TListItem;
  4.   NewColumn: TListColumn;
  5. begin
  6.   // Create a ListView item for each image in the ImageList
  7.   with ListView1 do
  8.   begin
  9.     CheckBoxes := True;
  10.     ViewStyle := vsReport;
  11.     RowSelect := True;
  12.     ListItem := Items.Add;
  13.     ListItem.Caption := '1';
  14.     ListItem.SubItems.Add('Item 1');
  15.     ListItem.SubItems.Add('Item 11');

  16.     ListItem := Items.Add;
  17.     ListItem.Caption := '2';
  18.     ListItem.SubItems.Add('Item 2');
  19.     ListItem.SubItems.Add('Item 22');

  20.     ListItem := Items.Add;
  21.     ListItem.Caption := '3';
  22.     ListItem.SubItems.Add('Item 3');
  23.     ListItem.SubItems.Add('Item 33');

  24.     // Create columns to show during viewing as vsReport
  25.     NewColumn := Columns.Add;
  26.     NewColumn.Width := 20;
  27.     NewColumn := Columns.Add;
  28.     NewColumn.Caption := 'Column 1';
  29.     NewColumn := Columns.Add;
  30.     NewColumn.Caption := 'Column 2';
  31.     NewColumn := Columns.Add;
  32.     NewColumn.Caption := 'Column 3';
  33.     NewColumn := Columns.Add;
  34.     NewColumn.Caption := 'Column 4';
  35.   end;
  36. end;
複製代碼

Rank: 9Rank: 9Rank: 9

5#
發表於 2006-11-8 13:58 |只看該作者
  1. procedure Tf_rs_bm.ListViewOrder(Fieldname:String);
  2. var
  3.   p: ^Integer;
  4.   ID: Integer;
  5.   ListItem: TListItem;
  6.   Node: TTreeNode;
  7.   Array_bmid: Array of string;
  8.   Array_bmidnumber: integer;
  9.   Light_all: Boolean;
  10.   i: integer;
  11.   SubNode: TTreeNode;
  12.   Subp: ^integer;
  13.   Tem_level: integer;
  14. begin
  15.   Listview2.items.Clear;
  16.   Light_all := False;
  17.   Array_bmidnumber := 0;
  18.   Node := TreeView1.Selected;
  19.   if Node = nil then Exit;
  20.   p := Node.Data;
  21.   if p=Nil then Exit;
  22.   ID := p^;
  23.   if id=0 then Light_all := true
  24.   else begin
  25.     Array_bmidnumber := 1;
  26.     SetLength(Array_bmid,1);
  27.     Array_bmid[0] := IntToStr(p^);
  28.     Tem_level := Node.Level;
  29.     SubNode := Node.GetNext;
  30.     While (SubNode<>nil) and (SubNode.level>Tem_level) do begin
  31.       Inc(Array_bmidnumber);
  32.       Subp := SubNode.Data;
  33.       SetLength(Array_bmid,Array_bmidnumber);
  34.       Array_bmid[Array_bmidnumber-1] := IntToStr(Subp^);
  35.       SubNode := SubNode.GetNext;
  36.     end;
  37.   end;
  38.   with TQuery.Create(Nil) do
  39.   begin
  40.     DatabaseName := DatabaseAlias;
  41.     SQL.Clear;
  42.     if Light_all then
  43.     begin
  44.       SQL.Add('select id, ryxx_del, ryxx_ryjd, ryxx_xm, ryxx_xb, ryxx_ssbm, ryxx_csny, ryxx_gj, ryxx_jg, ryxx_zzmm, ryxx_hyzk, ryxx_whcd, ryxx_byxx, ryxx_sxzy, ryxx_jsjsp, ryxx_wysp, ryxx_jszc from rs_ryxx');
  45.       SQL.Add(' where ryxx_del=' IntToStr(FLAG_NOTDELETE));
  46.     end
  47.     else begin
  48.       if Array_bmidnumber <= 0 then exit;
  49.       SQL.Add('select id, ryxx_del, ryxx_ryjd, ryxx_xm, ryxx_xb, ryxx_ssbm, ryxx_csny, ryxx_gj, ryxx_jg, ryxx_zzmm, ryxx_hyzk, ryxx_whcd, ryxx_byxx, ryxx_sxzy, ryxx_jsjsp, ryxx_wysp, ryxx_jszc from rs_ryxx');
  50.       SQL.Add(' where ryxx_del=' IntToStr(FLAG_NOTDELETE));
  51.       if Array_bmidnumber > 0 then
  52.       begin
  53.         Sql.Add(' and (ryxx_ssbm=' Array_bmid[0]);
  54.         if Array_bmidnumber>1 then
  55.         begin
  56.           for i := 1 to Array_bmidnumber-1 do begin
  57.             Sql.Add(' or ryxx_ssbm=' Array_bmid[i]);
  58.           end;
  59.         end;
  60.         Sql.Add(')');
  61.       end;
  62.     end;
  63.     if LightDesc then sql.Add(' order by ' Fieldname ' desc')
  64.     else sql.Add  (' order by ' Fieldname);
  65.     Open;
  66.     First;
  67.     ListView2.Items.Clear;
  68.     while not Eof do
  69.     begin
  70.       ListItem := ListView2.Items.Add();
  71.       ListItem.Caption := Trim(FieldByName('ryxx_xm').AsString);
  72.       New(p);
  73.       p^ := FieldByName('id').AsInteger;
  74.       ListItem.Data := p;
  75.       if FieldByName('ryxx_xb').AsString='男' then
  76.       begin
  77.         ListItem.ImageIndex := 0;
  78.         ListItem.SubItems.Add('男');
  79.       end
  80.       else
  81.       begin
  82.         if FieldByName('ryxx_xb').AsString='女' then
  83.         begin
  84.           ListItem.ImageIndex := 1;
  85.           ListItem.SubItems.Add('女');
  86.         end
  87.         else
  88.         begin
  89.           ListItem.ImageIndex := 0;
  90.           ListItem.SubItems.Add('');
  91.         end;
  92.       end;
  93.       case FieldByName('ryxx_ryjd').AsInteger of
  94.       0: ListItem.SubItems.Add('应聘人员');
  95.       1: ListItem.SubItems.Add('试用人员');
  96.       2: ListItem.SubItems.Add('正式员工');
  97.       3: ListItem.SubItems.Add('退休员工');
  98.       4: ListItem.SubItems.Add('辞退员工');
  99.       5: ListItem.SubItems.Add('离休员工');
  100.       else ListItem.SubItems.Add('');
  101.       end;
  102.       Listitem.subitems.add(Fieldbyname('ryxx_csny').Asstring);
  103.       ListItem.subitems.Add(fieldbyname('ryxx_gj').Asstring);
  104.       ListItem.Subitems.Add(fieldbyname('ryxx_jg').Asstring);
  105.       ListItem.Subitems.Add(fieldbyname('ryxx_zzmm').Asstring);
  106.       ListItem.Subitems.Add(fieldbyname('ryxx_hyzk').Asstring);
  107.       ListItem.Subitems.Add(fieldbyname('ryxx_whcd').Asstring);
  108.       Listitem.Subitems.Add(fieldbyname('ryxx_byxx').Asstring);
  109.       ListItem.Subitems.Add(fieldbyname('ryxx_sxzy').Asstring);
  110.       ListItem.Subitems.Add(fieldbyname('ryxx_jsjsp').Asstring);
  111.       ListItem.Subitems.Add(fieldbyname('ryxx_wysp').Asstring);
  112.       ListItem.Subitems.Add(fieldbyname('ryxx_jszc').Asstring);
  113.       Next;
  114.     end;
  115.     Free;
  116.   end;
  117. end;
複製代碼
‹ 上一主題|下一主題

Archiver|怕失憶論壇

GMT+8, 2024-5-17 13:05 , Processed in 0.014657 second(s), 10 queries .

Powered by Discuz! X1.5

© 2001-2010 Comsenz Inc.