How to create a form programmatically with a couple components on it in Delphi -


i'm working delphi 7 , i'm trying create form programmatically. here's form class stub:

unit clststudentinfoform;  interface      uses forms;      type         tstudentinfoform = class (tform)          end;  implementation   end. 

i have button on main form (that's regular form that's supposed create , show form above @ run-time) , when clicked creates , shows student form modal window. show form there's nothing on it. thing can click close button @ upper-right corner of window close it.

procedure tlibraryform.btnshowstudentifoformclick(sender: tobject); var     f : tstudentinfoform; begin     f := tstudentinfoform.createnew(self);     f.showmodal;     f.free;     f := nil; end; 

enter image description here

i have no idea how add components programmatically-created form (not @ run-time, source code). can me write code adds okay button student form sets caption , form's height , width (the code has written in student form file)?

any suggestions , examples highly appreciated. thank you.

by default (that is: default ide configuration settings), newly designed forms automatically created. main form shown, , secondary forms can shown with:

procedure tform1.button1click(sender: tobject); begin   form2.show;   form3.showmodal; end; 

it common practice disable auto creation option. go to: tools > (environment) options > (vcl) designer > module creation options, , disable/uncheck auto create forms & data modules option.

instead, create (already designed) form when needed:

procedure tform1.button1click(sender: tobject); var   form: tform2; begin   form := tform2.create(self);   form.show; end; 

this illustrates global variables secondary forms not needed, , common practice delete them possible prevent wrong usage:

type   tform2 = class(tform)   end;  //var //  form2: tform2;  << delete these global variable  implementation 

if not want set such secondary form form designer, need create controls in code @ runtime. follows:

unit unit2;  interface  uses   classes, forms, stdctrls;  type   tform2 = class(tform)   private     fbutton: tbutton;   public     constructor createnew(aowner: tcomponent; dummy: integer = 0); override;   end;  implementation  { tform2 }  constructor tform2.createnew(aowner: tcomponent; dummy: integer = 0); begin   inherited createnew(aowner);   fbutton := tbutton.create(self);   fbutton.setbounds(10, 10, 60, 24);   fbutton.caption := 'ok';   fbutton.parent := self; end;  end. 

as see, used createnew constructor. necessary t(custom)form derivatives:

use createnew instead of create create form without using associated .dfm file initialize it. use createnew if tcustomform descendant not tform object or descendant of tform.

for other container controls (such tpanel, tframe, etc.) can override default constructor create.

call form follows:

procedure tform1.button1click(sender: tobject); var   form: tform2; begin   form := tform2.create(nil);   try     form.showmodal;       form.free;   end; end; 

or:

procedure tform1.button1click(sender: tobject); begin   fform := tform2.createnew(application);   fform.show; end; 

in last case, form not freed hidden when closed, need store reference in private field (fform) , free later. or can automatically:

unit unit2;  interface  uses   classes, forms, stdctrls;  type   tform2 = class(tform)   private     fbutton: tbutton;     procedure formclose(sender: tobject; var action: tcloseaction);   public     constructor createnew(aowner: tcomponent; dummy: integer = 0); override;   end;  implementation  { tform2 }  constructor tform2.createnew(aowner: tcomponent; dummy: integer = 0); begin   inherited createnew(aowner);   onclose := formclose;   fbutton := tbutton.create(self);   fbutton.setbounds(10, 10, 60, 24);   fbutton.caption := 'ok';   fbutton.parent := self; end;  procedure tform2.formclose(sender: tobject; var action: tcloseaction); begin   action := cafree; end;  end. 

now, call without storing reference:

procedure tform1.button1click(sender: tobject); begin   tform2.createnew(self).show; end; 

whether pass, self, application or nil owner new form depends on when want automatically destroyed in case not free manually or via onclose event. using

  • self: destroy new form when calling form destroyed. particularly usefull when calling form isn't main form.
  • application: destroy new form when application ends. preferred choice.
  • nil: not destroy new form , results in memory leak @ application's finish. though, memory released when windows kills process.

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -