全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1879
推到 Plurk!
推到 Facebook!

如何做到點選TListView中的某一列再將其中一欄位值帶至function

答題得分者是:pceyes
tree.tw
一般會員


發表:6
回覆:7
積分:2
註冊:2008-04-11

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-07-17 10:36:09 IP:202.39.xxx.xxx 訂閱
各位先進:

板上找不到相關的案例,所以上來請教協助。

如何做到點選TListView中的某一列再將其中某一欄位帶至function中。

謝謝!!
pedro
尊榮會員


發表:152
回覆:1187
積分:892
註冊:2002-06-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-07-17 13:56:24 IP:60.248.xxx.xxx 未訂閱
試看看這小段
procedure TForm1.ListView1Click(Sender: TObject);
begin
myFunc(ListView1.Selected.Caption);
end;
procedure TForm1.myFunc(Caption: string);
begin
ShowMessage(Caption);
end;
pceyes
尊榮會員


發表:70
回覆:657
積分:1140
註冊:2003-03-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-07-17 18:19:33 IP:220.141.xxx.xxx 訂閱

[code delphi]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, StrUtils;

type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
procedure FormActivate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure ListView1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
{$R *.dfm}

procedure TForm1.FormActivate(Sender: TObject);
var
i : integer;
Mytitle : Array of string;
MyWidth : Array of integer;
ListItem: TListItem;
alarmfile_st ,temp_st: tstringlist;
str : string;
begin
// prepare text file
alarmfile_st := tstringlist.create;
temp_st := tstringlist.create;
alarmfile_st.LoadFromFile('alarm.txt');
// title name
SetLength(Mytitle, 4);
Mytitle[0]:= '提醒日';
Mytitle[1]:= '到期日';
Mytitle[2]:= '抬頭';
Mytitle[3]:= '內容';
// field width
SetLength(Mywidth, 4);
Mywidth[0]:= listview1.Font.Size*8;
Mywidth[1]:= listview1.Font.Size*7;
Mywidth[2]:= listview1.Font.Size*10;
Mywidth[3]:= listview1.Font.Size*20;
// set title
for i := 0 to 3 do begin
listview1.Columns.Add;
listview1.Columns[i].Caption := Mytitle[i];
listview1.Columns[i].Width := Mywidth[i];
end;
// set items
with listview1 do begin
for I := 0 to alarmfile_st.Count-1 do
begin
str := alarmfile_st[i];
temp_st.Text := stringreplace(Str,'',#13,[rfReplaceAll]);
ListItem := Items.Add;
ListItem.Caption := temp_st[0];
ListItem.SubItems.Add(temp_st[1]);
ListItem.SubItems.Add(temp_st[2]);
if pos('
',temp_st[3])<>-1 then begin
ListItem.SubItems.Add(stringreplace(temp_st[3],'
',#13 #10,[rfReplaceAll]));
end;
end;
end;
// setup listview
listview1.ViewStyle := vsReport ;
listview1.RowSelect := true;

end;

procedure TForm1.ListView1Click(Sender: TObject);
var aTime,Otime,aTitle,aData : string;
i : integer;
begin
i := listview1.Selected.Index;
aTime := listview1.Items[i].Caption;
Otime := listview1.Items[i].SubItems[0];
aTitle := listview1.Items[i].SubItems[1];
showmessage('YourFunction(' aTime ',' oTime ',' aTitle ')');
end;
end.

[/code]
------
努力會更接近成功
tree.tw
一般會員


發表:6
回覆:7
積分:2
註冊:2008-04-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-07-18 11:25:54 IP:202.39.xxx.xxx 訂閱
謝謝,可以使用了,但再延伸一個問題
我想用上下鍵移動光條時也可以觸發與click一樣的行為,但我用以下的方法執行是會有類似記憶體錯息
procedure TformMain.TListVewSelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);

錯誤訊息:
Access violation at address 01CE06E4 in module 'xxxx.dl'. Read of address 00000004.



===================引 用 pceyes 文 章===================

[code delphi]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, StrUtils;

type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
procedure FormActivate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure ListView1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
{$R *.dfm}

procedure TForm1.FormActivate(Sender: TObject);
var
i : integer;
Mytitle : Array of string;
MyWidth : Array of integer;
ListItem: TListItem;
alarmfile_st ,temp_st: tstringlist;
str : string;
begin
// prepare text file
alarmfile_st := tstringlist.create;
temp_st := tstringlist.create;
alarmfile_st.LoadFromFile('alarm.txt');
// title name
SetLength(Mytitle, 4);
Mytitle[0]:= '提醒日';
Mytitle[1]:= '到期日';
Mytitle[2]:= '抬頭';
Mytitle[3]:= '內容';
// field width
SetLength(Mywidth, 4);
Mywidth[0]:= listview1.Font.Size*8;
Mywidth[1]:= listview1.Font.Size*7;
Mywidth[2]:= listview1.Font.Size*10;
Mywidth[3]:= listview1.Font.Size*20;
// set title
for i := 0 to 3 do begin
listview1.Columns.Add;
listview1.Columns[i].Caption := Mytitle[i];
listview1.Columns[i].Width := Mywidth[i];
end;
// set items
with listview1 do begin
for I := 0 to alarmfile_st.Count-1 do
begin
str := alarmfile_st[i];
temp_st.Text := stringreplace(Str,'',#13,[rfReplaceAll]);
ListItem := Items.Add;
ListItem.Caption := temp_st[0];
ListItem.SubItems.Add(temp_st[1]);
ListItem.SubItems.Add(temp_st[2]);
if pos('
',temp_st[3])<>-1 then begin
ListItem.SubItems.Add(stringreplace(temp_st[3],'
',#13 #10,[rfReplaceAll]));
end;
end;
end;
// setup listview
listview1.ViewStyle := vsReport ;
listview1.RowSelect := true;

end;

procedure TForm1.ListView1Click(Sender: TObject);
var aTime,Otime,aTitle,aData : string;
i : integer;
begin
i := listview1.Selected.Index;
aTime := listview1.Items[i].Caption;
Otime := listview1.Items[i].SubItems[0];
aTitle := listview1.Items[i].SubItems[1];
showmessage('YourFunction(' aTime ',' oTime ',' aTitle ')');
end;
end.

[/code]
編輯記錄
tree.tw 重新編輯於 2008-07-18 11:26:38, 註解 無‧
pceyes
尊榮會員


發表:70
回覆:657
積分:1140
註冊:2003-03-13

發送簡訊給我
#5 引用回覆 回覆 發表時間:2008-07-18 14:54:14 IP:220.141.xxx.xxx 訂閱

[code delphi]
procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
var aTime,Otime,aTitle: string;
i : integer;
begin
i := item.Index;
aTime := listview1.Items[i].Caption;
Otime := listview1.Items[i].SubItems[0];
aTitle := listview1.Items[i].SubItems[1];
showmessage('YourFunction(' aTime ',' oTime ',' aTitle ')');
end;
[/code]
------
努力會更接近成功
tree.tw
一般會員


發表:6
回覆:7
積分:2
註冊:2008-04-11

發送簡訊給我
#6 引用回覆 回覆 發表時間:2008-07-18 15:58:36 IP:202.39.xxx.xxx 訂閱
可以了,謝謝!!
===================引 用 pceyes 文 章===================

[code delphi]
procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
var aTime,Otime,aTitle: string;
i : integer;
begin
i := item.Index;
aTime := listview1.Items[i].Caption;
Otime := listview1.Items[i].SubItems[0];
aTitle := listview1.Items[i].SubItems[1];
showmessage('YourFunction(' aTime ',' oTime ',' aTitle ')');
end;
[/code]
系統時間:2024-04-20 2:24:26
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!