Wolfgang Chien's Homepage | Delphi學習筆記 - 問答篇 |
謝謝指導,但小弟是希望藉由讀進的字串來指定,如下:
onExit := BlackBox(S); // BlackBox : some function to convert string to TNotifyEvent
來將字串'FVALdate'轉成TNotifyEvent的FVALdate,不知有無這種可能, 謝謝 !
當然有可能, 我動手試了一下, 實驗的過程如下 --
![]() |
1. 用記事本在 C:\TEMP 目錄建立一個 what.txt, 內容只有一列: ABC |
![]() |
2. 開一個新的 Delphi 專案, form 上放 Button * 1, TEdit * 2, TMemo * 1 |
![]() |
3. 撰寫 Button1 的 OnClick 與其他程式, 寫好的程式如下:
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; Edit2: TEdit; Memo1: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } function GetProcStr: string; function GetProcAddr(const sProcName: string): TNotifyEvent; procedure IamABC(Sender: TObject); procedure IamCDE(Sender: TObject); public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); var sProcName: string; EventProcAddr: TNotifyEvent; begin sProcName := GetProcStr; // ShowMessage(sProcName); EventProcAddr := GetProcAddr(sProcName); Edit1.OnExit := EventProcAddr; end; function TForm1.GetProcStr: string; var f: TextFile; begin AssignFile(f, 'c:\temp\what.txt'); Reset(f); Readln(f, Result); CloseFile(f); end; function TForm1.GetProcAddr(const sProcName: string): TNotifyEvent; begin if sProcName = 'ABC' then Result := IamABC else if sProcName = 'CDE' then Result := IamCDE else Result := nil; end; procedure TForm1.IamABC(Sender: TObject); begin if Sender is TEdit then Memo1.Lines.Add(TEdit(Sender).Name + ' Lost Focus, Handle by ABC'); end; procedure TForm1.IamCDE(Sender: TObject); begin if Sender is TEdit then Memo1.Lines.Add(TEdit(Sender).Name + ' Lost Focus, Handle by CDE'); end; end. |
![]() |
4. 執行, 點一下 Button1, 然後試著從 Edit1 轉移焦點出來, 執行過程中, 再嘗試將 what.txt 的內容改成 CDE, 再點一次 Button1, Memo1 的內容會告訴你實驗的結果. |
首頁 | 學習筆記 | 主題公園 | 軟體下載 | 關於本站 | 討論信群 | 相約下次 |