如何做到點選TListView中的某一列再將其中一欄位值帶至function |
答題得分者是:pceyes
|
tree.tw
一般會員 ![]() ![]() 發表:6 回覆:7 積分:2 註冊:2008-04-11 發送簡訊給我 |
|
pedro
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:152 回覆:1187 積分:892 註冊:2002-06-12 發送簡訊給我 |
|
pceyes
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:70 回覆:657 積分:1140 註冊:2003-03-13 發送簡訊給我 |
[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,' 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 發送簡訊給我 |
謝謝,可以使用了,但再延伸一個問題
我想用上下鍵移動光條時也可以觸發與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,' 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 發送簡訊給我 |
[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 發送簡訊給我 |
可以了,謝謝!!
===================引 用 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] |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |