Topic: How to clear a form

Hello friends,


I have an image that acts as a button, like so:


procedure frmMain_add_person_btn_OnClick (Sender: TObject);
begin
          frmAddPerson.ShowModal;
end;

I would like this button to open a clear form. When i use the tablegrid to edit a record and close the form, the form will open up again with all fields filled. The only solution is to my knowing to add a button in the form 'new record'.


I would like to open the form, if add_person_btn is clicked, with all components cleared. I know i can do it like this:


procedure frmMain_add_person_btn_OnClick (Sender: TObject);
begin
          frmAddPerson.ShowModal;
          frmAddPerson.Edit1.Clear;
          frmAddPerson.Edit2.Clear;
          etc...
end;

But i have a lot of components on the form, is there an easier way to clear them all in one time?

Thanks!

2 (edited by derek 2019-06-06 00:03:56)

Re: How to clear a form

Hi DBK,
If you want to clear all the fields, you can use 'componentcount' and loop through all the fields on your form, clearing each of them (use button4 on form1 in the attached example).
However, rather than use 'showmodal', you could just use a normal button with a 'new record' action associated with it - but hide the button.  Then when you click on your 'new record' image, the hidden button associated with the image is clicked by your script (see attached example);
procedure Form1_Image1_OnClick (Sender: TObject);
begin
  form1.button1.click;
end;
This then avoids all the issues with clearing fields, using modal forms etc etc.
Hope this helps,
Derek.

Post's attachments

Attachment icon dbkclearfields.zip 619.29 kb, 337 downloads since 2019-06-06 

Re: How to clear a form

frmAddPerson.NewRecord; 
Dmitry.

Re: How to clear a form

derek wrote:

Hi DBK,
If you want to clear all the fields, you can use 'componentcount' and loop through all the fields on your form, clearing each of them (use button4 on form1 in the attached example).
However, rather than use 'showmodal', you could just use a normal button with a 'new record' action associated with it - but hide the button.  Then when you click on your 'new record' image, the hidden button associated with the image is clicked by your script (see attached example);
procedure Form1_Image1_OnClick (Sender: TObject);
begin
  form1.button1.click;
end;
This then avoids all the issues with clearing fields, using modal forms etc etc.
Hope this helps,
Derek.

Hello Derek,

Thank you!! I used the hidden button, simple & clever solution as always! Why couldn't i think of this!

Re: How to clear a form

DriveSoft wrote:
frmAddPerson.NewRecord; 

Thank you Dmitry, tried it but i receive an error: 'undeclared identifier: NewRecord'

Re: How to clear a form

dbk wrote:
DriveSoft wrote:
frmAddPerson.NewRecord; 

Thank you Dmitry, tried it but i receive an error: 'undeclared identifier: NewRecord'

It's mean you use old version, download and install latest version

Dmitry.

Re: How to clear a form

ok thank you Dmitry will test it