• Delphi中为事件赋值(把某个单元的过程赋值给事件)


    把普通方法赋值给事件或者类中的方法

    --开发环境:D7

    ------------Unit1--开始-------------

    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls,TypInfo;

    type
    TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    private
    { Private declarations }
    public
    procedure MyFormbuttonClick(Sender:TObject);
    { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation
    uses
    Unit2;
    {$R *.dfm}

    procedure TForm1.Button2Click(Sender: TObject);
    begin
    MybuttonClick(Sender);
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    var
    vMybuttonClick:TNotifyEvent;
    begin
    TMethod(vMybuttonClick).Code:=@MybuttonClick ; //Unit2中的MybuttonClick
    Button1.OnClick:=vMybuttonClick;
    //Button1.OnClick:=MyFormbuttonClick ; //MyFormbuttonClick可以直接复制
    end;

    procedure TForm1.MyFormbuttonClick(Sender: TObject);
    begin
    MessageBox(0,PChar('AAA'),PChar('提示'),MB_OK);
    end;

    end.

    ------------Unit1--结束--------------

    --------------Form1开始

    object Form1: TForm1
    Left = 745
    Top = 544
    Width = 203
    Height = 156
    Caption = 'Form1'
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    OldCreateOrder = False
    OnCreate = FormCreate
    PixelsPerInch = 96
    TextHeight = 13
    object Button1: TButton
    Left = 48
    Top = 32
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    end
    object Button2: TButton
    Left = 48
    Top = 80
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 1
    OnClick = Button2Click
    end
    end

    ----------------------Form1结束

    -------------------------------Unit2开始

    unit Unit2;

    interface

    uses
    Windows;
    procedure MybuttonClick(Sender:TObject);
    implementation


    procedure MybuttonClick(Sender:TObject);
    begin
    MessageBox(0,PChar('AAA'),PChar('提示'),MB_OK);

    end;
    end.

    -------------------------------Unit2结束

  • 相关阅读:
    Calendar日历类
    DateFormat类和SimpleDateFormat类
    Date时间类(java.util.Date)
    时间处理相关类
    不可变和可变字符序列使用陷阱
    String类
    搬圆桌问题
    重温经典之排序 java实现
    i++ 和 ++i
    Intellij Idea 使用技巧 updating
  • 原文地址:https://www.cnblogs.com/dmqhjp/p/14771824.html
Copyright © 2020-2023  润新知