如何在一个函数单元(MyFunUnit)中写一个所有引用Form的KeyDown事件?
在每个Form中的组件事件写:
OnKeyDown := ToLookup_set_Value;
在一个通用单元中写过程:
procedure ToLookup_set_Value(Sender: TObject; var Key: Word;
Shift: TShiftState) ;
var
i: integer;
APPID, DOCID, tmpStr: string;
begin
//可以更改Internal的值
if Key = 13 then
with (Sender as TDBLookupCombobox) do
begin
end;
end
else if (Key = VK_F9) (*and (shift = [ssctrl])*) then
begin
end
//关闭机车数据编辑以后,刷新该数据记
else if (Key = VK_F5) and (Sender is TDBLookupCombobox) then
with (Sender as TDBLookupCombobox).ListSource.DataSet do
try
Wait;
Close;
Open;
finally
wait(false);
end;
end;
问题点数:0、回复次数:6Top
1 楼Delphifan()回复于 2003-12-03 09:54:02 得分 0
提示的出错信息为——
【Incompatible types: 'method pointer and regular procedure'】Top
2 楼jacky_shen(jacky)回复于 2003-12-03 23:59:15 得分 0
if you want to reference a method of an instance object (see Classes and objects), you need to add the words of object to the procedural type name. For example
type
TMethod = procedure of object;
TNotifyEvent = procedure(Sender: TObject) of object;
These types represent method pointers. A method pointer is really a pair of pointers; the first stores the address of a method, and the second stores a reference to the object the method belongs to. Given the declarations
type
TNotifyEvent = procedure(Sender: TObject) of object;
TMainForm = class(TForm)
procedure ButtonClick(Sender: TObject);
...
end;
var
MainForm: TMainForm;
OnClick: TNotifyEvent
we could make the following assignment.
OnClick := MainForm.ButtonClick;
Two procedural types are compatible if they have
the same calling convention,
the same return value (or no return value), and
the same number of parameters, with identically typed parameters in corresponding positions. (Parameter names do not matter.)
Procedure pointer types are always incompatible with method pointer types. The value nil can be assigned to any procedural type.
Nested procedures and functions (routines declared within other routines) cannot be used as procedural values, nor can predefined procedures and functions. If you want to use a predefined routine like Length as a procedural value, write a wrapper for it:
function FLength(S: string): Integer;
begin
Result := Length(S);
end;
Procedural types in statements and expressionsTop
3 楼HGRhgr(HGRhgr)回复于 2003-12-08 10:27:42 得分 0
OnKeyDown := ToLookup_set_Value;
要使等式成立,必须使得等式左右类型相同,
由于OnKeyDown是个对象方法,所以ToLookup_set_Value也必须要定义成对象方法Top
4 楼qiume(好好回贴,天天胖胖)回复于 2003-12-08 14:22:20 得分 0
类型不匹配Top
5 楼acai(acai)回复于 2003-12-10 17:47:56 得分 0
可以做到的,把你的代码修改为:
//在每个Form中的组件事件写:
//OnKeyDown := ToLookup_set_Value;
var
M: TMethod;
begin
M.Code := @ToLookup_set_Value;
YourComponent.OnKeyDown = TKeyEvent(M);
end;
Top
6 楼delphi2java(delphi2java)回复于 2003-12-10 17:51:59 得分 0
用application中的消息来控制比较方便。Top




