-1

I'm writing a Delphi app, in which I have a procedure to clear the form and reset it to its default state.

procedure ResetForm ();
begin
   edtG1.Text := ''; // Error occurs on this line, no matter what the instruction is.
   edtG1.Enabled := True;
   btnG1Check.Enabled := True;
   .
   .
   .
   .
end;

I've tried everything - that first line of code in the procedure doesn't compile, no matter what. I can't see any problem with the declaration of the procedure either.

Can someone please assist? My thanks.

I am trying to clear all the fields in the form, enable what has been disabled, etc. To reset it to its default state as it starts at the beginning of the program. I tried different code instructions on that first line (yes, edtG1 exists), other controls do exactly the same and all other instructions. I have tried renaming the procedure too, but to no avail.

3
  • 2
    The compiler doesn't play tricks with you or is angry - it precisely tells what is wrong and where. Read the details of any error/warning/info message and learn to interpret them. Additionally: if you would have the instruction Exit; then it would not yield a syntax/compiler error.
    – AmigoJack
    Jan 26 at 10:16
  • 1
    Ignoring the content of error messages is a habit you must shake off Jan 26 at 17:49
  • You say doesn't compile and error happens here, but you never once say what that error is, despite the fact that you get a clear, descriptive error message. When you write an error, the very next thing to follow should be the exact, complete error message. It's on the screen right in front of you, but we can't see that screen from here. You're asking us for help to solve your problem, and it's in your best interest to make it as easy as possible for us to do so by giving us the details you have available.
    – Ken White
    Jan 27 at 5:02

1 Answer 1

5

You have written a standalone routine and are accessing some controls which are on the form. That is not possible.

I assume you meant to write this as a method for your form.

Go to the interface section of your form unit, write procedure ResetForm; into the public section of your form class (where the comment { Public declarations } is) and press Ctrl+Shift+C and then write your code into the method.

11
  • 3
    That is not possible. Well, not strictly true. It is possible - using the forms reference, e.g. Form1.edtG1.Text := '';. But absolutely better as you suggest. Jan 26 at 9:27
  • @TomBrunberg: But then you will (presumably) use the global Form1 variable which, obviously, points to a single instance of the form (the auto-created one). If you in the future will create several instances of the form class, this will cause a nasty bug. On the other hand, using Self instead of Form1, in a method of the form class, will always give you the current instance, as expected. Of course Tom knows this, but I'm writing this for the benefit to others reading this Q&A. Jan 26 at 9:44
  • 2
    It would have helped to give the actual compiler error in this question.
    – dummzeuch
    Jan 26 at 9:49
  • 1
    Whenever I see someone accessing these global form variables I get nausea attacks. Jan 26 at 13:33
  • 3
    These global form variables are the spawn of Satan and should be removed with extreme prejudice. Jan 26 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.