00 Votes

Delphi Error with Form.Hide and Form.Show: This Form of Method Call only allowed for Class Methods

Question by Guest | 2020-11-12 at 09:10

I am using 2 forms and I want one to show up while the current one hides.

I used the code:

Form1.Hide;
Form2.Show;

I then get this error: This form of method call only allowed for class methods. And highlights the Form1.Hide;

What should I do?

ReplyPositiveNegative
00 Votes

This error message typically occurs when you call some procedure not on an instance of a class but on a class itself.

Typically, when starting Delphi, a form is defined in the following way in Delphis default project:

...
type
  TForm1 = class(TForm)
  ...

var
  Form1: TForm1;

Here, TForm1 is the name of the class. And then there is a variable Form1 of the type TForm1.

Now, you can call procedures such as Hide and Show only with Form1 but not with TForm1 (Form1.Show is correct, TForm1.Show not). Form1 is an instance of the class TForm1, with which you can work. When calling procedures of TForm1 directly, you typically get the error you have mentioned.

It's the same as if you have something like var a: integer - here you can also only work with "a" and not with "integer".

Now, you said that the error occurs with Form1.Hide and Form2.Show (not TForm1.Hide and TForm2.Show). Probably you have renamed your forms that in your project "Form1" and "Form2" are the form's class names. Please have a look at your class definition to check that and how your instance is named.

Additionally, you can have a look at your project file (the one with the extension dpr). Here, your form is created, typically like:

Application.CreateForm(TForm1, Form1);

First, the class name, then the variable name / the later instance. Here you can check whats defined in your project there.
2020-11-13 at 03:26

ReplyPositive Negative
Reply

Related Topics

jQuery: Show and hide elements

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.