2013-04-12 81 views
0

首先,我想提前道歉,标题可能有多糟糕,但我真的需要你的帮助。在delphi中切换命令页面

首先,让我通过切换命令页向您解释我meam: 让我们猜测我们有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') 

我希望我帮助你不够了解我问,谢谢

+0

您是否尝试过做一些面板和隐藏和显示的代码呢? –

+3

您可以在运行时分配一个事件处理程序。在'Button2Click'中,运行Button1.Onclick = Button1_2ndClick',其中'Button1_2ndClick'是TNotifyEvent签名的方法。 ..如果这是你要求的.. –

回答

0

做,这是一个TActionList最简单的方法:

  1. 把你的形式
  2. 浦3个TButtons在你的表单上添加一个TActionList。
  3. 为您希望Button1执行的每个命令添加一个新操作。
  4. 如果您想在设计时开始,在Button1的Action 属性中指定Action1。这也可以在运行时完成。
  5. Button2的:在其onClick事件,分配动作2到按钮1
  6. 将Button3:在其onClick事件,分配ACTION3到按钮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; 

//Button.onClick's:

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; 
+0

问题是,只要我这样做,它会更改按钮的标题 – user2276109

+0

您可以通过操作更改标题,该操作具有将反映在按钮中的标题属性。如果您希望标题永远不会更改,请将所有操作的标题设置为相同。或者尝试将按钮的标题更改为您在action.onExecute中所喜欢的内容:Button1.Action:= Action2; Button1.Caption:='任何你喜欢的'。我的回答基本上与Bummi回答的和Sertac Akyuz在评论中告诉你的一样 - 只是这些行为使得更简单 - 更少的代码和行为处理所有事情。 – Vector

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. 
+0

@ user2276109 - 如果你想编程,你应该习惯编写代码:-) – Vector