Board logo

標題: [Delphi] ListView 的應用 [打印本頁]

作者: admin    時間: 2012-11-18 18:42     標題: [Delphi] ListView 的應用

要把 ListView 的 ViewStyle 設定為 vsReport, ListView 才會顯示為我們常用的Grid

unit listview2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    editItem: TEdit;
    edit1col: TEdit;
    edit2col: TEdit;
    edit3col: TEdit;
    ListView1: TListView;
    butAdd: TButton;
    butRead: TButton;
    butModify: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    butClear: TButton;

    procedure FormCreate(Sender: TObject);
    procedure butAddClick(Sender: TObject);
    procedure butReadClick(Sender: TObject);
    procedure butModifyClick(Sender: TObject);
    procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure butClearClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.butAddClick(Sender: TObject);
var
   RowItem:TListItem;
begin
  Rowitem := ListView1.Items.Add;
  Rowitem.Caption := edit1col.Text;  // first column
  Rowitem.SubItems.Add(edit2col.Text);  // second column
  Rowitem.SubItems.Add(edit3col.Text);  // third column
end;

procedure TForm1.butReadClick(Sender: TObject);
var
   Item:TListItem;
begin
  Item:= ListView1.Items.Item[StrtoInt(editItem.Text)];
  edit1col.Text := Item.Caption;
  edit2col.Text := Item.SubItems[0];
  edit3col.Text:= Item.SubItems[1];
end;

procedure TForm1.butClearClick(Sender: TObject);
begin
  ListView1.Items.Clear;
end;

procedure TForm1.butModifyClick(Sender: TObject);
var
  Item:TListItem;
begin
  Item:= ListView1.Items.Item[StrtoInt(editItem.Text)];
  Item.Caption:= edit1col.Text;
  Item.SubItems[0] := edit2col.Text;
  Item.SubItems[1] := edit3col.Text;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   //ListView1.Items.Clear;
end;

procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem;
  Selected: Boolean);
var
  listviewcurrentindex: integer;
begin
if Selected then begin        // without selected check, click on blank row will cause error
   listviewcurrentindex := Listview1.Selected.Index;
   editItem.Text :=  InttoStr(listviewcurrentindex);
   edit1col.Text := Listview1.Selected.Caption;
   edit2col.Text := Listview1.Selected.SubItems.Strings[0];
   edit3col.Text:= Listview1.Selected.SubItems.Strings[1];
end;
end;

end.




Remark:

//update the listview
ListView1.Show;





歡迎光臨 How2Do (http://forum.how2do.com.hk/) Powered by Discuz! 7.2