2013-04-12 77 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