运行期获取控件全部事件

From  http://delphi.about.com/od/adptips2005/qt/listhandlers.htm

~~~~~~~~~~~~~~~~~~~~~~~~~
USES TYPINFO, .... !!!!

procedure TForm1.FormCreate(Sender: TObject) ;

var
Count, Loop: Integer;
List: PPropList;
method : TMethod;
begin
Count := GetPropList(TypeInfo(TForm1), tkMethods, nil) ;// 也可以写为 Self.ClassType
GetMem(List, Count * SizeOf(PPropInfo)) ;
try
GetPropList(TypeInfo(TForm1), tkMethods, List) ;
for Loop := 0 to Pred(Count) do
begin
Listbox1.Items.Add(List[Loop]^.Name) ;
method := GetMethodProp(Form1, List[Loop]^.Name) ;
if Assigned(method.Code) and Assigned(method.Data) then
begin
ListBox2.Items.Add(List[Loop]^.Name) ;
end;
end;
finally
FreeMem(List, Count * SizeOf(PPropInfo))
end;

end;


http://www.hur.cn/special/delphiProgram/06482.htm

list all properties, events of a component?

列出控件所有属性和事件?

uses

  TypInfo;

procedure ListComponentProperties(Component: TComponent; Strings: TStrings);

var

  Count, Size, I: Integer;

  List: PPropList;

  PropInfo: PPropInfo;

  PropOrEvent, PropValue: string;

begin

  Count := GetPropList(Component.ClassInfo, tkAny, nil);

  Size  := Count * SizeOf(Pointer);

  GetMem(List, Size);

  try

    Count := GetPropList(Component.ClassInfo, tkAny, List);

    for I := 0 to Count - 1 do

    begin

      PropInfo := List^[I];

      if PropInfo^.PropType^.Kind in tkMethods then

        PropOrEvent := 'Event'

      else

        PropOrEvent := 'Property';

      PropValue := VarToStr(GetPropValue(Component, PropInfo^.Name));

      Strings.Add(Format('[%s] %s: %s = %s', [PropOrEvent, PropInfo^.Name,

        PropInfo^.PropType^.Name, PropValue]));

    end;

  finally

    FreeMem(List);

  end;

end;

// Example: List all Properties/Events from Button1 in a TListBox

procedure TForm1.Button1Click(Sender: TObject);

begin

  ListComponentProperties(Button1, ListBox1.Items);

end;


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi是一种强大的编程语言,它允许我们在运行时动态地创建和修改控件,并对其绑定事件。使用delphi动态控件绑定事件可以实现用户交互和动态更新界面的功能。 首先,在Delphi中创建动态控件是通过调用控件类的构造函数完成的,例如使用TButton类创建一个按钮控件,可以使用类似以下代码: ```delphi var button: TButton; begin button := TButton.Create(Form1); // 创建按钮 button.Parent := Form1; // 设置按钮的父容器 button.Left := 100; // 设置按钮的位置 button.Top := 100; button.Caption := '按钮'; // 设置按钮的文本 end; ``` 接下来,我们可以为动态创建的控件绑定事件,让控件具有交互功能。绑定事件的方法是使用Delphi的`OnEvent`属性,并通过指定回调函数来处理事件。例如,我们可以为上面创建的按钮绑定一个点击事件,代码如下: ```delphi procedure TForm1.ButtonClick(Sender: TObject); begin ShowMessage('按钮被点击了!'); end; ``` 然后,在动态创建按钮的代码中添加绑定事件的部分: ```delphi button.OnClick := ButtonClick; // 绑定按钮点击事件 ``` 这样,当按钮被点击时,就会触发`ButtonClick`函数,并弹出一个消息框显示提示信息。 通过以上步骤,我们就实现了Delphi动态控件事件绑定。通过动态创建控件并为其绑定事件,我们可以实现根据用户操作实时更新界面的功能,提升了用户体验和交互性。 总结起来,Delphi动态控件绑定事件的步骤包括创建控件、设置控件属性和绑定事件函数。通过这些步骤,我们可以实现灵活、动态的界面设计和交互功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值