Recent

Author Topic: [SOLVED] How to write procedure to manipulate form components?  (Read 822 times)

Lamb3234

  • New Member
  • *
  • Posts: 16
[SOLVED] How to write procedure to manipulate form components?
« on: December 07, 2019, 02:52:51 pm »
I have some code to reset the controls in my dialog:

Code: Pascal  [Select][+][-]
  1. procedure TForm2.OKButtonClick(Sender: TObject);
  2.   Form2.Hide;
  3.   LabeledEdit1.Clear;
  4.   LabeledEdit2.Clear;
  5.   LabeledEdit3.Clear;
  6.   ComboBox1.ItemIndex := -1;
  7.   ActiveControl := LabeledEdit1;
  8. end;

But because this procedure has tonnes of other code, I thought that to improve readability, I'd write a custom procedure called ResetForm and call this procedure in the OKButtonClick procedure:

Code: Pascal  [Select][+][-]
  1. procedure ResetForm (Form: TForm);
  2. begin
  3.   Form2.Hide;
  4.   LabeledEdit1.Clear;
  5.   LabeledEdit2.Clear;
  6.   LabeledEdit3.Clear;
  7.   ComboBox1.ItemIndex := -1;
  8.   ActiveControl := LabeledEdit1;  
  9. end;      

I get error from this (Identifier not found) - I think I have the parameters wrong, but I can't find anything on Google. What should the parameters be?
« Last Edit: December 07, 2019, 05:26:35 pm by Lamb3234 »

bytebites

  • Hero Member
  • *****
  • Posts: 632
Re: How to write procedure to manipulate form components?
« Reply #1 on: December 07, 2019, 03:02:33 pm »
Start from this
Code: Pascal  [Select][+][-]
  1. procedure TForm2.ResetForm

Lamb3234

  • New Member
  • *
  • Posts: 16
Re: How to write procedure to manipulate form components?
« Reply #2 on: December 07, 2019, 03:10:11 pm »
Thanks bytebites

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to write procedure to manipulate form components?
« Reply #3 on: December 07, 2019, 03:22:22 pm »
Note that you could also do it like this:

Code: Pascal  [Select][+][-]
  1. procedure ResetForm (AForm: TForm2);
  2. begin
  3.   if Assigned(AForm) then
  4.     with AForm do begin
  5.       Hide;
  6.       LabeledEdit1.Clear;
  7.       LabeledEdit2.Clear;
  8.       LabeledEdit3.Clear;
  9.       ComboBox1.ItemIndex := -1;
  10.       ActiveControl := LabeledEdit1;  
  11.     end;
  12. end;

though there is no benefit in doing it this way rather than with a method of TForm2.

The problem with your initial code is that all the controls are "hidden" inside the class TForm2, so to access them you would need to either qualify them with the referred form object (as shown above) or convert the procedure to a method of the form class, as hinted by bytebites.

HTH
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018