0

I want to create a label dynamically. I followed: how to dynamically create a component in delphi such as TLabel or TEdit ...etc

I have decleared by label like this:

 listofnames[i]:=TLabel.create(quiz2);
 listofnames[i].parent:=quiz2;
 listofnames[i].caption:=quiz.getsubjectname(i);
 listofnames[i].height:=25;
 listofnames[i].Width:=85;
 listofnames[i].left:=8;
 listofnames[i].top:=135+i*30;
 listofnames[i].OnClick:=labelclicked;

and in my form class at the top of the unit I have this:

 type
  Tlblarr=array of TLabel;
  TQuiz2 = class(TForm)
  //published section
    Q2LTitle: TLabel;
    Q2LIntro1: TLabel;
    Q2LMon: TLabel;
    Q2LTue: TLabel;
    Q2LWed: TLabel;
    Q2LFri: TLabel;
    Q2LThurs: TLabel;
    Q2LSun: TLabel;
    Q2LSat: TLabel;
    Q2ButStart: TButton;
    Q2LSubList: TLabel;
    Q2ButFin: TButton;
   //all above are other things on my form
    listofnames:Tlblarr;
  //array of dynamicly create labels
    procedure Q2ButStartClick(Sender: TObject);
    procedure labelclicked(Sender:TObject);
    procedure timeslotclicked(Sender:TObject);
    procedure Q2ButFinClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    clickedfirst:string;
    numberofsub:integer;
    numberofextra:integer;
   public
  end;

I am getting the following error:

Published field listofnames not a class or interface type

So I am presuming it is not meant to be published? However if I move it to the public or private section I get a different error:

Exception class EClassNotFound with message 'Class TLabel not found'

The example I was going off didn't mention it, so I presume it is something common sense I don't know yet. I have looked over the internet for the cause of the two errors and most people's problems are different than mine or the solution won't work - putting the label in the published spot seems to be the most common. Both errors occur in the same way: it compiles and at run immediately the exception draws up. I believe this is because the line Application.CreateForm(TQuiz2,Quiz2). This is in the Project1 section - a section I've never coded in or touched, it didn't even appear until I debugged the program.

Could someone indicate wherever the label array is definitely meant to go in published and where I can find more on how to debug this error. I have looked at http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Classes_EClassNotFound.html and http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/cm_cannot_publish_xml.html and I am still failing to understand.

Jken
  • 1
  • 1

1 Answers1

2

For runtime constructed field objects, they go in private or protected (or very rarely public) section. This depends on how you want them to be visible to other or descending forms (forms can be inherited like other classes). Put it under private unless you have a good reason to put it somewhere else.

The declarations between the classname and private are for the design time fields, so it is looking in your .dfm file for the properties of those objects.

As an aside, it is convention in Delphi to start a field variable with F, so FListOfNames: Tlblarr; FClickedOnFirst etc

Adam G
  • 1,283
  • 1
  • 6
  • 15