首先,我想提前道歉标题可能有多糟糕,但我真的需要你的帮助。
首先,让我通过switch命令页面解释一下我的看法: 我们猜测我们有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 :(得分: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.
答案 1 :(得分:0)
最简单的方法是使用TActionList:
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;