سؤال

I'm trying to name columns (in this grey row) in a string grid. I know that I should use something like this:

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.Cells[0,0] := 'Text 1';
  StringGrid1.Cells[1,0] := 'Text 2';
end;

The problem is that there is error:

'TForm1' does not contain a member named 'FormCreate'at line 81".

I'm a beginner. What is wrong with my program?

هل كانت مفيدة؟

المحلول

You need to declare the method in the type.

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  ...
  end;

And that line of code to the declaration of the type that you will find near the top of your unit. Then your program will compile. You also need to make sure that the event handler attaches the handler to the form's OnCreate event. Use the Object Inspector to check that.

But the easiest way to make this all happen is to get the IDE to write it all. So, you would:

  • Delete the code that you have shown in the question.
  • Click on the form in the designer.
  • Select the Events view in the Object Inspector.
  • Find the OnCreate event in the Object Inspector.
  • Double click in the handler column of the OnCreate event in the Object Inspector.
  • Now the code editor opens showing an empty event handler body for you to add code to, and all the other parts are joined up. Specifically the method is declared in the type, and the handler is connected to the event.

Now, that's how you do it normally, but it does pay to know the three things that need to be in place for an event to fire:

  1. The event handler is declared in the type of the class.
  2. The event handler is defined in the implementation of the class.
  3. The event handler is attached to the event in the Object Inspector. In fact, although you set it in the Object Inspector, the information actually lives in the .dfm file.

If you don't know all this already, then asking questions on Stack Overflow is really not the most effective way to get up to speed. A good book would certainly help. Even if it's for an older version of Delphi, the primary concepts have not changed for years. But if you don't have a book, then you should at least follow the tutorial.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top