0

首先,我想提前为标题的糟糕程度道歉,但我真的需要你的帮助。

首先,让我解释一下我所说的切换命令页面的含义:假设我们有 2 个按钮。然后我们在单击时命令按钮 1 执行:

procedure TForm1.Button1Click(Sender: TObject);
begin
Showmessage('Hi');
end;

end.

好的,现在当我在 button2 上单击一次时,我希望 button1 在单击其他内容时执行(更改 button1 的命令),例如showmessage ('My name is Monster')

我不知道这个方法是怎么调用的,但是它在一些游戏制作应用程序中使用如下(不是这样,而是类似):

**When** button1 clicked then showmessage ('Hi');
**When** button2 clicked **Then** button1 switch page 2--> Showmessage('My name is')
**When** button3 clicked **Then** button1 switch page 1--> Showmessage('Hi')

我希望我已经帮助你理解我的要求,谢谢

4

2 回答 2

2

正如 Sertac Akyuz 所提到的,只需将另一个事件分配给您的按钮。

一个透支的例子可能是:

unit DynamicEvents;

interface

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


type
  TNotifyClass=Class
    Constructor Create(const msg:String);overload ;
    Constructor Create(EV:TNotifyEvent);overload;
    Procedure NotifyEvent(Sender:TObject);
  private
    FMessage:String;
    FNotifyEvent:TNotifyEvent;
  End;

  TNotifyList=Class
    Constructor Create(arr:Array of String);
    Destructor Destroy;override;
    private
    FList:TList;
    FLastAccessedIndex:Integer;
    function GetEvent(Index: Integer): TNotifyEvent;
    public
    Property Events[Index:Integer]:TNotifyEvent read GetEvent; default;
    Procedure Add(NC:TNotifyClass);
    published

    Property LastAccessedIndex:Integer read FLastAccessedIndex;
  End;

  TForm6 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
    FNotifyList:TNotifyList;
    procedure FormCloseEV(Sender: TObject);
  public
    { Public-Deklarationen }
  end;

var
  Form6: TForm6;

implementation

{$R *.dfm}

{ TNotifyList }

procedure TNotifyList.Add(NC: TNotifyClass);
begin
  Flist.Add(NC);
end;

constructor TNotifyList.Create(arr: array of String);
var
 i:Integer;
begin
   FList := TList.Create;
   FLastAccessedIndex := -1;
   for I := Low(arr) to High(arr) do
     FList.Add(TNotifyClass.Create(arr[i]));
end;

destructor TNotifyList.Destroy;
var
 i:Integer;
begin
   for I := 0 to Flist.Count -1  do
     TNotifyClass(FList[i]).Free;
   Flist.Free;
  inherited;
end;

function TNotifyList.GetEvent(Index: Integer): TNotifyEvent;
begin
   if (Index>=0) and (Index<Flist.Count) then
    begin
      FLastAccessedIndex := Index;
    end
   else
    begin
      if Flist.Count>0 then
        begin
         FLastAccessedIndex := 0;
        end
      else
        begin
         FLastAccessedIndex := -1;
        end;
    end;
    if FLastAccessedIndex >- 1 then Result := TNotifyClass(FList[FLastAccessedIndex]).NotifyEvent;
end;



procedure TForm6.Button1Click(Sender: TObject);
begin
   Button2.OnClick := FNotifyList[FNotifyList.LastAccessedIndex + 1];
end;

{ TNotifyClass }

constructor TNotifyClass.Create(const msg: String);
begin
   FMessage := msg;
end;
constructor TNotifyClass.Create(EV:TNotifyEvent);
begin
   FNotifyEvent := EV;
end;

procedure TNotifyClass.NotifyEvent(Sender: TObject);
begin
   if Assigned(FNotifyEvent) then FNotifyEvent(Sender)
   else Showmessage(FMessage);
end;

procedure TForm6.FormCreate(Sender: TObject);
begin
  ReportMemoryLeaksOnShutDown := true;
  FNotifyList:=TNotifyList.Create(
                ['Message 1'
                ,'Message 2'
                ,'Message 3'
                ,'Message 4'
                ,'Message 5'
                ,'Message 6'
                ,'Message 7'
                ,'Message 8'
                ,'Message 9'
                ,'Message 10']
                );
  FNotifyList.Add(TNotifyClass.Create(FormCloseEV));
end;

procedure TForm6.FormCloseEV(Sender: TObject);
begin
  if MessageDLG('Close',mtConfirmation,[mbyes,mbCancel],0)=idYes then Close;

end;


procedure TForm6.FormDestroy(Sender: TObject);
begin
  FNotifyList.Free;
end;

end.
于 2013-04-13T06:34:07.810 回答
0

最简单的方法是使用 TActionList:

  1. 在表单上放置 3 个 TButton
  2. 在您的表单上放置一个 TActionList。
  3. 为您希望 Button1 执行的每个命令添加一个新操作。
  4. 如果要在设计时开始,请在 Button1 的 Action 属性中指定 Action1。这也可以在运行时完成。
  5. Button2:在其 onClick 事件中,将action2 分配给按钮 1。
  6. Button3:在其 onClick 事件中,将action3 分配给按钮 1。

    等等。

您的表单代码将如下所示:

//action.onExecute 的:

procedure TForm1.Action1Execute(Sender: TObject);
begin
   DoStuff;
end;

procedure TForm1.Action2Execute(Sender: TObject);
begin
  DoMoreStuff
end;

procedure TForm1.Action3Execute(Sender: TObject);
begin
 DoOtherStuff;
end;

//Button.onClick 的:

procedure TForm1.Button2Click(Sender: TObject);
begin
 Button1.Action:=Action2;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Button1.Action:=Action3;
end;

//你的“命令”:

procedure TForm1.DoStuff;
begin
  showmessage('hi');
end;

procedure TForm1.DoMoreStuff;
begin
 showMessage('red');
 Self.Color:=clRed;
end;

procedure TForm1.DoOtherStuff;
begin
  showMessage('black');
  self.Color:=clBlack;
end;
于 2013-04-13T06:32:52.250 回答