假设我创建了一个新的FMX项目,上面仅包含一个TButton和一个TProgressBar。
现在,我使用[Shift] + [F11]添加了“ Countertest.pas”文件。 (请参见下面的代码)
现在,我已经在“ Countertest.pas”文件中的一个过程中触发了一个在“ Unit1.pas”(主应用程序)中的过程,以更改TProgressBar的值。
在“ Unit1.pas”内部,我将其编写为从“ Countertest.pas”文件内部进行调用:
procedure TForm1.SomethingChanged(newPercentage:Integer);
begin
ProgressBar1.Value:=newPercentage;
showmessage('Congratulations, you have just reached '+IntToStr(newPercentage)+' Percent ;)');
end;
为了简化我的问题,这是我删除的“ Countertest.pas”文件:
unit Countertest;
interface
uses FMX.Memo; // Will be used later
type
TCountertest = Class
private
zahl: Integer;
published
constructor Create();
destructor Destroy();
procedure Counter();
property Percentage: Integer read zahl;
end;
implementation
constructor TCountertest.Create();
begin
end;
destructor TCountertest.Destroy();
begin
end;
procedure TCountertest.Counter();
begin
for i := 0 to 1337 do
Percentage:=0;
begin
zahl:=i;
if Percentage<>round(i / 100) then
begin
// Here I want to call a Procedure inside 'Unit1.pas' to change a Value of the TProgressBar (and some other components like TLabel.Text)
end;
Percentage:=round(i / 100);
end;
end;
end.
据我所知,有可能使用类似
procedure(Sender: TObject) of object;
之类的东西,这似乎是要使用的东西,但是我对如何使用它一无所知。我的意图是编写类似于TEdit控件中使用的OnChange事件的内容。
当然,我可以将“ Unit1.pas”添加到“ Countertest.pas”的“使用”部分,然后直接调用该过程,但是由于必须处理多个TCountertest实例,因此我希望它更像这样:
procedure InstanceOfTCountertest.SomethingChanged(newPercentage:Integer);
begin
ProgressBar1.Value:=newPercentage;
showmessage('Congratulations, you have just reached a new Percent ;)');
end;
在最终的应用程序中,有多个TCountertest实例,所以我也有多个进度栏(以及其他GUI组件,如TLabel)。
也许还有其他方法可以执行此操作,所以可以随意提出任何建议,以适合于显示这些实例的进度的目的。
最佳答案
通常,组件(例如TButton
)将事件公开为属性(例如:TButton.OnCLick
),由父组件或同级组件(在这种情况下为父TForm
)来设置事件处理程序。假设我们要更改Button1.OnClick
事件处理程序:
// Setting the handler:
procedure TForm1.Create(AOwner: TComponent);
begin
Button1.OnClick := Self.MyCustomClickHandler;
end;
// And the handler implementation:
procedure TForm1.MyCustomClickHandler(Sender: TObject);
begin
// ...
end;
因此,我想您想像
TCountertest
那样为您的TCountertest.OnCount
提供一个事件,以便其他组件/表单可以设置处理程序并根据计数器进度的变化采取行动。让我们描述一下如何使用Delphi方式(未经测试的代码):首先,您的组件应实现并公开事件:
unit Countertest;
interface
type
// Custom type for your event handler
TCountEvent = procedure(Sender: TObject; Percentage: Integer) of object;
TCountertest = Class
private
FPercentage: Integer;
// Variable that holds the event handler set by the user
FOnCount: TCountEvent;
// Internal event trigger
procedure DoCount();
public
constructor Create();
destructor Destroy();
procedure Counter();
published
property Percentage: Integer read FPercentage;
// This is the property for your event handler assignment
property OnCount: TCountEvent read FOnCount write FOnCount;
end;
implementation
constructor TCountertest.Create();
begin
// Initialize event handler to nil
FOnCount := nil;
end;
destructor TCountertest.Destroy();
begin
end;
// Event trigger
procedure TCounterTest.DoCount();
begin
// Check that the user assigned an event handler
if Assigned(FOnCount) then
FOnCount(Self, FPercentage);
end;
procedure TCountertest.Counter();
begin
// Your code (update FPercentage in your code)...
// When you need to trigger the event:
DoCount();
// Rest of your code...
end;
end.
现在,我们准备创建事件处理程序并将其设置为您的
TCountertest
实例:unit Unit1;
// ...
type
TForm1 = class
private
// ...
// Declare the handler
procedure CounterCount(Sender: TObject; Percentage: Integer);
public
// ...
end;
implementation
// ...
// Implement the handler
procedure TForm1.CounterCount(Sender: TObject; Percentage: Integer);
begin
ProgressBar1.Value := Percentage;
end;
// Set the event handlers for your counters
procedure TForm1.Create(AOwner: TComponent);
var
Counter1, Counter2: TCountertest;
begin
// Create the counters
Counter1 := TCountertest.Create();
Counter2 := TCountertest.Create();
// Set the event handlers
Counter1.OnCount := Self.CounterCount;
Counter2.OnCount := Self.CounterCount;
end;
end.
希望对您有所帮助。随意问是否。