我是初学者,不好意思。我是想点击listview中的某一行后,使该行的值转到edit1.text中。谢谢!
源程序:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;type
TForm1 = class(TForm)
ListView1: TListView;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure ListView1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
const
Names: array[0..5, 0..1] of string = (
('Rubble', 'Barney'),
('Michael', 'Johnson'),
('Bunny', 'Bugs'),
('Silver', 'HiHo'),
('Simpson', 'Bart'),
('Squirrel', 'Rocky')
);var
I: Integer;NewColumn: TListColumn;
ListItem: TListItem;
ListView: TListView;
begin
ListView := TListView.Create(Self);
with ListView do
begin
Parent := Self;
// Align := alTop; ViewStyle := vsReport; NewColumn := Columns.Add;
NewColumn.Caption := 'Last';
NewColumn := Columns.Add;
NewColumn.Caption := 'First'; for I := Low(Names) to High(Names) do
begin
ListItem := Items.Add;
ListItem.Caption := Names[I][0];
ListItem.SubItems.Add(Names[I][1]);
end;
end;end;procedure TForm1.ListView1Click(Sender: TObject);
var
i:integer;
begin if ListView1.ItemIndex>=0 then
begin
i:=ListView1.ItemFocused.Index;
edit1.Text:=listview1.Items[i].Caption;
end;end;end.
源程序:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;type
TForm1 = class(TForm)
ListView1: TListView;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure ListView1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
const
Names: array[0..5, 0..1] of string = (
('Rubble', 'Barney'),
('Michael', 'Johnson'),
('Bunny', 'Bugs'),
('Silver', 'HiHo'),
('Simpson', 'Bart'),
('Squirrel', 'Rocky')
);var
I: Integer;NewColumn: TListColumn;
ListItem: TListItem;
ListView: TListView;
begin
ListView := TListView.Create(Self);
with ListView do
begin
Parent := Self;
// Align := alTop; ViewStyle := vsReport; NewColumn := Columns.Add;
NewColumn.Caption := 'Last';
NewColumn := Columns.Add;
NewColumn.Caption := 'First'; for I := Low(Names) to High(Names) do
begin
ListItem := Items.Add;
ListItem.Caption := Names[I][0];
ListItem.SubItems.Add(Names[I][1]);
end;
end;end;procedure TForm1.ListView1Click(Sender: TObject);
var
i:integer;
begin if ListView1.ItemIndex>=0 then
begin
i:=ListView1.ItemFocused.Index;
edit1.Text:=listview1.Items[i].Caption;
end;end;end.
with ListView do
begin
Parent := Self;
// Align := alTop;
OnClick := ListView1Click; // <- 加上这句
...procedure TForm1.ListView1Click(Sender: TObject);
var
i:integer;
begin if TListView(Sender).ItemIndex>=0 then // 把ListView1改为TListView(Sender)
begin
i:=TListView(Sender).ItemFocused.Index;
edit1.Text:=TListView(Sender).Items[i].Caption;
end;
end;