事件方法必須是類的方法

個人本意是想在一個通用unit單元中定義一個通用的查詢函數,在這個通用查詢函數中調用字段的ongettext方法來解析值爲代碼的字段(如:表的性別字段存儲的是代碼,'1':男,'2'女),在DBGridEh中性別顯示的是名稱男或女,而表中字段存儲的是'1'或'2'
因爲想盡量作的通用,不但願用聯合查詢,要用Ongettext
難道要在每一個TADOQuery控件中寫Ongettext事件,但是全部的TADOQuery都放在了datamodule1中,查詢sql是在不一樣窗體中寫的
要怎麼作?

type
   TEventHandlers = class { 建一個虛擬類}
       procedure GetName(Sender: TField; var Text: String; DisplayText: Boolean);
   end;
 
...
var 
  EvHandler:TEventHandlers;
 
implementation
 
{方法實現}
procedure TEventHandlers.GetName(Sender: TField; var Text: String; DisplayText: Boolean); 
begin 
    with datamodule1.query1 do 
    begin 
        close; 
        sql.Clear; 
        sql.Add('select [name] from [sex] where [code]=:x'); 
        Parameters[0].Value:=Sender.Value; 
        open; 
        Text:=Fields[0].AsString; 
    end; 
end;
 
 
 
{使用}
datamodule1.query1.FieldByName('XB').OnGetText:= EvHandler.GetName; 
相關文章
相關標籤/搜索