delphi - 调用Button1在Unit2中单击Form1/Unit1

标签 delphi unit-testing delphi-xe

我已经下载了Delphi XE7,并且在访问另一个单元时遇到了一些问题...
我需要从另一个单元调用过程,所以我将给出一个非常基本的说明,简单的程序...
这是来自主Unit1的具有形式和button1的代码:

unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, StdCtrls, Unit2;
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('Hello');
end;

end.


这是来自Unit2的代码:

unit Unit2;
interface
implementation
uses Unit1;
end.


现在,如何在单击form1上的button1时使Unit1中的过程Button1Click像显示消息一样,让我们​​说HelloFromUnit2? Unit2是codeUnit,没有任何内容。

最佳答案

使用内置过程调用Click处理程序

保留表格1的原样:

unit Unit2;

interface

implementation

uses 
  Unit1;

procedure Click;
begin
  if Assigned(Form1) then
    Form1.Button1.Click;
end;


结束。

关于delphi - 调用Button1在Unit2中单击Form1/Unit1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27096457/

相关文章:

delphi - 将 Delphi 2007 升级到 XE6 - 类型库编辑器在哪里?

javascript - Chutzpah 异常且不刷新测试列表

XML 注释和 Delphi XMl 数据绑定(bind)

delphi - Delphi XE Update 1-哪个版本号?

delphi - 如何在 Delphi 中从泛型转换为变体

delphi - 将C结构转换为delphi

android - 如何使用 Delphi 为 Android 创建工具提示

delphi - tagBitmap@DELPHI ???什么样的?

java - 以方法引用作为参数的单元测试方法

android - 如何在单元测试中获取上下文以在内存数据库对象中创建 Room 数据库