Reputation: 63
I have two components created dynamically, a button (btnEnter) and an edit (edtID)- where the user will enter their user ID. What I want is for the program to verify whether the user has entered a valid ID when they have clicked the button.
The code I have:
1)When the objects are created
with btnEnter do
{edit properties such as caption, etc}
OnClick := ValidateID;
2)The procedure is declared as follows:
procedure ValidateID (Sender : TObject);
What I would like to do is pass the text in the edit through the procedure as a parameter, so that the procedure will be able to manipulate the text and determine whether it is valid or not.
So what I tried, but didn't work was:
procedure ValidateID (Sender : TObject; sID : string);
with btnEnter do
OnClick := ValidateID(edtID.Text);
Would really appreciate if someone could help me with this. Thanks
Upvotes: 1
Views: 4629
Reputation: 5238
This code works.
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
button: TButton;
edit: TEdit;
procedure ValidateID(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
edit := TEdit.Create(self);
button := TButton.Create(self);
button.Parent := Form1;
edit.Parent := Form1;
edit.Left := 1;
edit.Top := 1;
button.Left := 1;
button.Top := 50;
button.OnClick := ValidateID;
end;
procedure TForm1.ValidateID(Sender: TObject);
begin
ShowMessage(edit.Text)
end;
end.
Upvotes: 1
Reputation: 21033
The TButton.OnClick
event is of type TNotifyEvent
which has a signature:
TNotifyEvent = procedure(Sender: TObject) of object;
Thus, you can not assign a procedure with a different signature to TButton.OnClick
.
You need to declare the ValidateID
procedure as a method of the form class and then, since the TEdit is on the same form, it is in the same scope as your validation method, and you can simply access EditID.Text in your ValidateID
method.
Upvotes: 2