線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1330
推到 Plurk!
推到 Facebook!

如何將本段程式改成callback回傳

答題得分者是:careychen
sunhihi
一般會員


發表:4
回覆:7
積分:2
註冊:2008-12-17

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-12-18 09:17:12 IP:61.219.xxx.xxx 訂閱
將Unit2的資訊使用callback回傳給unit Unit1

請問該怎麼改 特別標示部份

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DBCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
procedure CallBack(Sender: TObject);
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
list: TList;
begin
//form2.show;
ShowForm(list, CallBack);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
freeform;
end;
procedure TForm1.CallBack(Sender: TObject);
begin
Label1.Caption := Caption;
end;

end.

-------------------------------------------------------------------------------------------------

unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, Db, DBTables, DBClient, MConnect, SConnect;
type
TForm2 = class(TForm)
ListView1: TListView;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
Query1: TQuery;
Database1: TDatabase;
SocketConnection1: TSocketConnection;
procedure FormCreate(Sender: TObject);
procedure ListView1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
FCallBack: TNotifyEvent;
procedure myFunc(Caption: string);
{ Private declarations }
public
{ Public declarations }
end;
procedure ShowForm(list: TList; CallBack: TNotifyEvent);
procedure FreeForm;

var
Form2: TForm2;
implementation
uses Unit1;
{$R *.DFM}
procedure TForm2.FormCreate(Sender: TObject);
var
I: Integer;
begin
ListView1.ViewStyle := vsReport;
for I := 0 to Query1.FieldCount-1 do
begin
with ListView1.Columns.Add do
begin
Caption := Query1.Fields.Fields[I].DisplayLabel;
Width := Query1.Fields.Fields[I].DisplayWidth*10;
end;
end;
Query1.First;
while (not Query1.Eof) do
begin
with ListView1.Items.Add do
begin
Caption := Query1.Fields.Fields[0].AsString;
for I := 1 to Query1.FieldCount-1 do
SubItems.Add(Query1.Fields.Fields[I].AsString);
end;
Query1.Next;
end;
Edit2.Text := DateTimeToStr(Now);
end;
procedure TForm2.ListView1Click(Sender: TObject);
begin
myFunc(ListView1.Selected.Caption);
ComboBox1.text := Caption;
end;

procedure TForm2.myFunc(Caption: string);
begin

form1.Label1.Caption := Caption;
利用callback將這段程式執行碼
轉到form1執行
end;

procedure TForm2.Button2Click(Sender: TObject);
var
i: Integer;
begin
Query1.close;
Query1.SQL.clear;
Query1.SQL.Add('Insert into KIN');
Query1.SQL.Add('values(:Title, :Sub_title, :Description, :Creator, :DateTime, :Pic)');
query1.parambyname('Title').asstring := ComboBox1.Text;
query1.parambyname('Sub_title').asstring := ComboBox2.Text;
query1.parambyname('Description').asstring := Memo1.Lines.GetText;
query1.parambyname('Creator').asstring := Edit1.Text;
query1.parambyname('DateTime').asstring := edit2.Text;
query1.parambyname('Pic').asstring := Edit1.Text;
query1.ExecSQL;
//-------------------------------------------------------------------------------------
i := ListView1.Items.Count;
ListView1.Items.Add.Caption := ComboBox1.text;
ListView1.Items[i].SubItems.Add(ComboBox2.text);
ListView1.Items[i].SubItems.Add(memo1.lines.GetText);
ListView1.Items[i].SubItems.Add(edit1.Text);
ListView1.Items[i].SubItems.Add(edit2.Text);
end;
procedure ShowForm(list: TList; CallBack: TNotifyEvent);
begin
if (not Assigned(Form2)) then
Form2 := TForm2.Create(Application);
Form2.FCallBack := CallBack;
Form2.Show;
end;
procedure FreeForm;
begin
if Assigned(Form2) then FreeAndNil(Form2);
end;
procedure TForm2.Button4Click(Sender: TObject);
begin
FreeForm;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
Query1.close;
Query1.SQL.clear;
Query1.SQL.Add('select * From KIN where Creator = '' Edit1.text ''');
Query1.open;
//-----------------------------------------------------------------------
end;
procedure TForm2.Button3Click(Sender: TObject);
var
i: Integer;
begin
Query1.close;
Query1.SQL.clear;
Query1.SQL.Add('delete from KIN');
Query1.SQL.Add('where Creator = :Creator');
Query1.parambyname('Creator').asstring := Edit1.text;
Query1.ExecSQL;
for i:=ListView1.Items.Count-1 downto 0 Do
if ListView1.Items[i].Caption = Edit1.Text then
begin
ListView1.Items.Item[i].Delete();
end;
end;
end.
編輯記錄
sunhihi 重新編輯於 2008-12-18 10:31:12, 註解 無‧
sunhihi 重新編輯於 2008-12-18 10:40:32, 註解 無‧
sunhihi 重新編輯於 2008-12-18 10:45:59, 註解 無‧
careychen
尊榮會員


發表:41
回覆:580
積分:959
註冊:2004-03-03

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-12-18 14:22:31 IP:60.248.xxx.xxx 訂閱
因為您使用的預設的 TNotifyEvent 的事件傳遞,而這個只傳送一個 TObject 參數而已,但因為您在 MyFunc 與 Callback 中傳遞的卻是 String ,所以會無法正常的傳送!!
另外請下次 PO 的時候,將程式碼放入【程式碼區塊】,這樣閱讀者比較好看唷!

請照下面紅色的部份做修正

===================引 用 sunhihi 文 章===================
unit Unit1;

(略)

procedure TForm1.CallBack(Sender: TObject);
begin
if Sender is TListItem then
Label1.Caption := TListItem(Sender).Caption;
end;


end.

-------------------------------------------------------------------------------------------------

unit Unit2;

(略)

private
FCallBack: TNotifyEvent;
procedure myFunc(Caption: string);
{ Private declarations }
public
{ Public declarations }
end;


(略)

implementation

(略)

procedure TForm2.ListView1Click(Sender: TObject);
begin
myFunc(ListView1.Selected.Caption);
if Assigned(FCallBack) then
FCallBack(Listview1.Selected);
ComboBox1.text := Caption;
end;


procedure TForm2.myFunc(Caption: string);
begin
form1.Label1.Caption := Caption;
利用callback將這段程式執行碼
轉到form1執行
end;



(略)

end.
------
價值的展現,來自於你用哪一個角度來看待它!!
編輯記錄
careychen 重新編輯於 2008-12-18 14:26:55, 註解 無‧
sunhihi
一般會員


發表:4
回覆:7
積分:2
註冊:2008-12-17

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

===================引 用 careychen 文 章===================

unit Unit1;

(略)

procedure TForm1.CallBack(Sender: TObject);
begin
if Sender is TListItem then
Label1.Caption := TListItem(Sender).Caption;
end;
出現錯誤無法執行= =
個人更改成


[code delphi]
procedure TForm1.CallBack(Sender: TObject);
var
list: TList;
begin
if Sender is TList then
list := TList(Sender);
Label1.Caption := list.ClassName;
end;

[/code]


end.

-------------------------------------------------------------------------------------------------

unit Unit2;

(略)

private
FCallBack: TNotifyEvent;
procedure myFunc(Caption: string);
{ Private declarations }
public
{ Public declarations }
end;


(略)

implementation

(略)

procedure TForm2.ListView1Click(Sender: TObject);
begin
myFunc(ListView1.Selected.Caption);
if Assigned(FCallBack) then
FCallBack(Listview1.Selected);
ComboBox1.text := Caption;
end;

ComboBox1.text := Caption;
這行沒有必要
Caption反而指向form2



(略)

end.
作此修改後我的程式依然有問題 = =
careychen
尊榮會員


發表:41
回覆:580
積分:959
註冊:2004-03-03

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-12-18 17:56:49 IP:60.248.xxx.xxx 訂閱
我覺得嘛~~,您會錯,應該是上面沒有  uses comctrls   這個吧? 加上去後,改回我原本寫的就 ok 了

===================引 用 sunhihi 文 章===================

===================引 用 careychen 文 章===================

unit Unit1;

(略)

procedure TForm1.CallBack(Sender: TObject);
begin
if Sender is TListItem then
Label1.Caption := TListItem(Sender).Caption;
end;
出現錯誤無法執行= =
個人更改成


[code delphi]
procedure TForm1.CallBack(Sender: TObject);
var
list: TList;
begin
if Sender is TList then
list := TList(Sender);
Label1.Caption := list.ClassName;
end;

[/code]


end.

-------------------------------------------------------------------------------------------------

unit Unit2;

(略)

private
FCallBack: TNotifyEvent;
procedure myFunc(Caption: string);
{ Private declarations }
public
{ Public declarations }
end;


(略)

implementation

(略)

procedure TForm2.ListView1Click(Sender: TObject);
begin
myFunc(ListView1.Selected.Caption);
if Assigned(FCallBack) then
FCallBack(Listview1.Selected);
ComboBox1.text := Caption;
end;

ComboBox1.text := Caption;
這行沒有必要
Caption反而指向form2



(略)

end.
作此修改後我的程式依然有問題 = =
------
價值的展現,來自於你用哪一個角度來看待它!!
sunhihi
一般會員


發表:4
回覆:7
積分:2
註冊:2008-12-17

發送簡訊給我
#5 引用回覆 回覆 發表時間:2008-12-18 18:04:42 IP:61.219.xxx.xxx 訂閱
可以了
感恩
系統時間:2024-04-29 10:26:22
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!