Jump to content

ClientEvents and runtime component creation - Help-Me!


herculanojs

Recommended Posts

I'm having trouble defining events in runtime. I saw in another post that the definition of the event must be made before the creation of the form, but the components are created dynamically, so you can not use the definition of code in the form creation.
 
The code below works perfectly put in the form of creation, with the component already created. However when I create the component and attribute the event, it does not work.
 
The event is for the UniEdit only accept numbers.
Is there any other way to do it?
All my form components are dynamically created.
 
Exemple 1: component existing === OK!!
 
procedure TMainForm.UniFormCreate(Sender: TObject);
begin
     UniEdit1.ClientEvents.ExtEvents.Values['keypress']:='function keypress(sender, e, eOpts) {var allowed = "0123456789"; var c = e.getCharCode(); if (allowed.indexOf(String.fromCharCode©)=== -1) {e.stopEvent(); } }';
end;

 

 

Exemple 2: component not existing === NO!!

 

procedure TMainForm.UniButton1Click(Sender: TObject);
var Edit:TuniEdit;
begin
     edit := Tuniedit.Create(self);
     edit.Left := 8;
     edit.Top := 8;
     edit.Parent := self;
     edit.Visible := True;
     edit.ClientEvents.ExtEvents.Values['keypress']:='function keypress(sender, e, eOpts) {var allowed = "0123456789"; var c = e.getCharCode(); if (allowed.indexOf(String.fromCharCode©)=== -1) {e.stopEvent(); } }';
end;
 
how to proceed ?

 

Project1.rar

Link to comment
Share on other sites

Hi,

 

You must assign the Name...:

procedure TMainForm.UniButton1Click(Sender: TObject);
var 
  Edit:TuniEdit;
begin
  edit := Tuniedit.Create(self);
  edit.Left := 8;
  edit.Top := 8;
  edit.Parent := self;
  edit.Visible := True;
-> edit.Name := 'myedit';
  edit.ClientEvents.ExtEvents.Values['keypress']:='function keypress(sender, e, eOpts) {var allowed = "0123456789"; var c = e.getCharCode(); if (allowed.indexOf(String.fromCharCode(c))=== -1) {e.stopEvent(); } }';
end;

Best regards.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...