delphi中的切换命令页

delphi中的切换命令页,delphi,command,delphi-7,Delphi,Command,Delphi 7,首先,我想为这个标题有多糟糕提前道歉,但我真的需要你的帮助 首先,让我解释一下我所说的切换命令页面: 我们猜我们有两个按钮。然后,我们在单击执行时命令按钮1: procedure TForm1.Button1Click(Sender: TObject); begin Showmessage('Hi'); end; end. 好的,现在当我只在按钮2上单击一次时,我希望按钮1在单击其他东西时执行(更改按钮1的命令),例如showmessage(“我的名字是怪物”) 我不知道该方法是如何调用的,

首先,我想为这个标题有多糟糕提前道歉,但我真的需要你的帮助

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

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

end.
好的,现在当我只在按钮2上单击一次时,我希望按钮1在单击其他东西时执行(更改按钮1的命令),例如
showmessage(“我的名字是怪物”)

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


我希望我已经帮了你足够多的忙,让你明白我的问题,谢谢你。最简单的方法是找一位战术家:

  • 在你的表格上打3个按钮
  • 在你的表格上放一个战术专家
  • 为希望按钮1执行的每个命令添加新操作
  • 如果要在设计时开始,请在动作中指定动作1 Button1的属性。这也可以在运行时完成
  • 按钮2:在其onClick事件中,将操作2分配给按钮1。
  • 按钮3:在其onClick事件中,将操作3分配给按钮1。

  • 表单的代码如下所示:

    //action.onExecute's:

    procedure TForm1.Action1Execute(Sender: TObject);
    begin
       DoStuff;
    end;
    
    procedure TForm1.Action2Execute(Sender: TObject);
    begin
      DoMoreStuff
    end;
    
    procedure TForm1.Action3Execute(Sender: TObject);
    begin
     DoOtherStuff;
    end;
    
    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;
    
    //按钮。onClick的:

    procedure TForm1.Action1Execute(Sender: TObject);
    begin
       DoStuff;
    end;
    
    procedure TForm1.Action2Execute(Sender: TObject);
    begin
      DoMoreStuff
    end;
    
    procedure TForm1.Action3Execute(Sender: TObject);
    begin
     DoOtherStuff;
    end;
    
    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;
    
    //您的“命令”:

    procedure TForm1.Action1Execute(Sender: TObject);
    begin
       DoStuff;
    end;
    
    procedure TForm1.Action2Execute(Sender: TObject);
    begin
      DoMoreStuff
    end;
    
    procedure TForm1.Action3Execute(Sender: TObject);
    begin
     DoOtherStuff;
    end;
    
    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;
    

    正如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.
    
    单元动态事件;
    接口
    使用
    窗口、消息、系统工具、变体、类、图形、控件、窗体、,
    对话框,stdctrl;
    类型
    TNotifyClass=类
    构造函数创建(const msg:String);超载;
    构造函数创建(EV:TNotifyEvent);超载;
    过程NotifyEvent(发送方:TObject);
    私有的
    FMessage:字符串;
    FNotifyEvent:TNotifyEvent;
    结束;
    TNotifyList=类
    构造函数创建(arr:字符串数组);
    毁灭者毁灭;推翻
    私有的
    FList:TList;
    FLastAccessedIndex:整数;
    函数GetEvent(索引:整数):TNotifyEvent;
    公众的
    属性事件[索引:整数]:TNotifyEvent read GetEvent;违约
    程序添加(NC:TNotifyClass);
    出版
    属性LastAccessedIndex:整数读取FLastAccessedIndex;
    结束;
    TForm6=类(TForm)
    按钮1:t按钮;
    按钮2:t按钮;
    程序按钮1点击(发送方:ToObject);
    过程表单创建(发送方:ToObject);
    销毁程序表(发送方:TObject);
    私有的
    {私营部门}
    FNotifyList:TNotifyList;
    关闭EV的程序(发送方:TObject);
    公众的
    {公共部门}
    结束;
    变量
    表6:TForm6;
    实施
    {$R*.dfm}
    {TNotifyList}
    程序TNotifyList.Add(NC:TNotifyClass);
    开始
    文件添加(NC);
    结束;
    构造函数TNotifyList.Create(arr:String数组);
    变量
    i:整数;
    开始
    FList:=TList.Create;
    FLastAccessedIndex:=-1;
    对于I:=低(arr)到高(arr)do
    FList.Add(TNotifyClass.Create(arr[i]);
    结束;
    析构函数TNotifyList.Destroy;
    变量
    i:整数;
    开始
    对于I:=0到Flist.Count-1 do
    TNotifyClass(FList[i])免费;
    免费飞行;
    继承;
    结束;
    函数TNotifyList.GetEvent(索引:整数):TNotifyEvent;
    开始
    如果(索引>=0)和(索引x0),则
    开始
    FLastAccessedIndex:=0;
    结束
    其他的
    开始
    FLastAccessedIndex:=-1;
    结束;
    结束;
    如果FLastAccessedIndex>-1,则结果:=TNotifyClass(FList[FLastAccessedIndex])。NotifyEvent;
    结束;
    步骤t用于M6.按钮1单击(发送者:对象);
    开始
    Button2.OnClick:=FNotifyList[FNotifyList.LastAccessedIndex+1];
    结束;
    {TNotifyClass}
    构造函数TNotifyClass.Create(const msg:String);
    开始
    FMessage:=msg;
    结束;
    构造函数TNotifyClass.Create(EV:TNotifyEvent);
    开始
    FNotifyEvent:=EV;
    结束;
    过程TNotifyClass.NotifyEvent(发送方:TObject);
    开始
    如果已分配(FNotifyEvent),则为FNotifyEvent(发送方)
    else Showmessage(FMessage);
    结束;
    过程TForm6.FormCreate(发送方:ToObject);
    开始
    ReportMemoryLeaksOnShutDown:=true;
    FNotifyList:=TNotifyList.Create(
    [“信息1”
    “信息2”
    “信息3”
    “信息4”
    “信息5”
    “信息6”
    “信息7”
    “信息8”
    “信息9”
    ,“信息10']
    );
    FNotifyList.Add(TNotifyClass.Create(FormCloseEV));
    结束;
    程序TForm6.FormCloseEV(发送方:ToObject);
    开始
    如果MessageDLG('Close',mtConfirmation,[mbyes,mbCancel],0)=Y,则关闭;
    结束;
    程序TForm6.FormDestroy(发送方:ToObject);
    开始
    FNotifyList.Free;
    结束;
    结束。
    
    您是否尝试过制作一些面板并对代码隐藏和显示它们?您可以在运行时分配一个事件处理程序。在“Button2Click”中,运行
    Button1.Onclick=Button1_2ndClick
    其中“Button1_2ndClick”是TNotifyEvent签名的一种方法……如果您是这样问的……问题是,每当我这样做时,它都会改变设置按钮的标题您可以通过操作更改标题,该操作具有将反映在按钮中的标题属性。如果希望标题永不更改,请将所有操作的标题设置为相同。或者尝试将按钮的标题更改为您喜欢的操作。OneExecute本身:Button1。操作:=Action2;Button1.说明:=“随你喜欢”。我的回答基本上与Bummi和Sertac Akyuz在评论中告诉你的相同-只是操作使之更容易-更少的代码,操作处理一切。@user2276109-如果你想编程,你应该习惯于编写代码:-)