• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

Form Caption Thing

Im really confused, why is only one command not being lanched by delphi?
Form3.Caption := 'Hi';
When I go to Form3 its always like the way I made the caption before running the code.
Why isnt lanching?

[Delphi Code]
unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, DateUtils;

type
  TForm3 = class(TForm)
    ListView1: TListView;
    Button1: TButton;
    Button2: TButton;
    ListBox1: TListBox;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Edit1: TEdit;
    Label5: TLabel;
    ComboDay: TComboBox;
    ComboMonth: TComboBox;
    ComboYear: TComboBox;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.FormCreate(Sender: TObject);
var
  x: Integer;
  Y: Word; // Integer;
  FormattedDate : string;
begin
Form3.Caption := 'Hi';
//days as number
for x := 1 to 30 do
  comboDay.Items.Add(IntToStr(x));
//month String
for x:= 1 to 12 do
  comboMonth.Items.Add(LongMonthNames[x]);
//year uses DateUtils
Y := YearOf(Today);
for x := (Y-100) to Y do
  comboYear.Items.Add(inttostr(x));

end;

end.
[/Delphi Code]

This is how its called
[Delphi Code]
        Form1.Hide;
        form3 := Tform3.Create(Self);
        form3.ShowModal;
[/Delphi Code]

just another quick question
what should I use?
form3 := Tform3.Create(Self);
or
form3 := Tform3.Create(Application);
whats the diff?
0
deathman5
Asked:
deathman5
  • 7
  • 3
  • 2
  • +2
2 Solutions
 
geobulCommented:
Form3.Caption := 'Hi';
should be
self.Caption := 'Hi';
or even
Caption := 'Hi';

Regards, Geo
0
 
esoftbgCommented:
Possible:
1).  Application.CreateForm(TForm3, Form3);
2).  Form3 := TForm3.Create(Application);
3).  Form3 := TForm3.Create(Self); // but not in          procedure TForm3.....
0
 
esoftbgCommented:
1). The owner of Form3 will be Application
2). The owner of Form3 will be Application
3). The owner of Form3 will be Self (depends from which instance is called)
4). Form3 := TForm3.Create(nil);
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
deathman5Author Commented:
mmm, can u please explain whats the difference if the owner was Self or Application of nil?
0
 
esoftbgCommented:
It is not important which will be the owner of the Form ....
What is important is:
1). When you create the Form dynamically, you must Destroy it when it not needed anymore, or before Application is closed
2). Application destroys autocreated Forms ....
0
 
esoftbgCommented:
I will be back after 12 - 16 hours, sorry ....
0
 
deathman5Author Commented:
okey, Im really new to this
so is there any tut about these things?
whats the diff between dynamically created forms and auto created forms?
and how to creat auto forms?
sorry...
0
 
paulb1989Commented:
If you go into the project options in the delphi project menu, you will be able to see a list of forms in your application, and create which are auto created. If the form is auto created then you do not need to call its create method, but just use it like your main form as it has already been created.

You will have to call its show method though, as not all auto created forms are shown when the application is run, only the first created.
0
 
esoftbgCommented:
opening .dpr file (the Project), you will se something like:

program Project;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2},
  Unit3 in 'Unit3.pas' {Form3},
  ........................................,
  UnitN in 'UnitN.pas' {FormN};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);  //  Here are all the autocreated Forms on the Project;
  Application.CreateForm(TForm2, Form2);  //  Here are all the autocreated Forms on the Project;.
  Application.CreateForm(TForm3, Form3);  //  Here are all the autocreated Forms on the Project;
  ...........................................................;  //  Here are all the autocreated Forms on the Project ....
  Application.CreateForm(TFormN, FormN);  //  Here are all the autocreated Forms on the Project;
  Application.Run;
end.

//........

As @paulb1989 has been write above, you may exclude some Forms from the list of the autocreated Forms. It is needed to be atleast the MainForm (Form1) to be autocreated ....

> whats the diff between dynamically created forms and auto created forms?
> and how to creat auto forms?

As you see autocreated Forms are created once before the Project is started: Application.Run; This way your Application will spend some time on starting depending on the count of the autocreated Forms and its content ....
Autocreated Form are in Memory during whole the time until the application be closed .... You may use for example:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.Show;  //  Form2.Visisble := True;
  // Form2.ShowModal;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Form2.Hide;  //  Form2.Visisble := False;
end;

//........

If you decide to use some Forms as dynamically created, you will exclude them from the list os autocreated as @paulb1989 has been describe above .... An example for dynamically created Form:

procedure TForm1.Button3Click(Sender: TObject);
begin
  if not Assigned(Form3) then
    Application.CreateForm(TForm3, Form3);
  Form3.Show;  //  Form3.Visisble := True;
  // Form3.ShowModal;
end;
0
 
Wim ten BrinkSelf-employed developerCommented:
Is the FormCreate code assigned to the OnCreate event of Form3? :-)
A collegue of mine once had a similar problem. He had created a FormCreate unit and wondered why it was never called. I told him to put a breakpoint in it and so he did. The code never stopped at the breakpoint. I then looked at the events tab in the Object Inspector and saw the event wasn't assigned to the form...

I still have a good laugh about that one... That collegue of mine was a good Delphi developer and yet he too makes silly mistakes like this...

And one thing... Don't EVER refer to the form variable of a form class from within the form class code itself. If you don't know what you're doing, such code could lead to bugs that you will never be able to solve. Try to avoid those global variables anyway. (I tend to just delete after I create a new form class.)
0
 
deathman5Author Commented:
mmm, I may sound stupid but anyway
so If Im creating a simple application like 4 forms only
is it better to put all form as auto created or dynamically?
or may be 2 auto and 2 dynamically?
I understood that the difference is the Autocreated Form are in Memory during whole the time until the application be closed.
0
 
esoftbgCommented:
The MainForm must to be autocreated, that's obviously ....
In my opinion the Datamodule too (if you have a DataModule in the Application at all) ....
About the others I really don't know what to say .... It depends on your decision, nothing is a must .... Just you have two ways of the Form creation and you may use only autocreated Forms or both, autocreated and dynamically created ....
0
 
Wim ten BrinkSelf-employed developerCommented:
In general, my projects will have one main form and one datamodule that are auto-created. All other forms and datamodules are created by me in runtime. And to prevent them from being auto-created I actually remove the global form variables in those units. So no "var Form2: TForm2;" in my code. I just use local variables in my code when I create other forms.
0
 
esoftbgCommented:
I like Workshop_Alex's comment .... In this way your application will start faster, because it will create only two Forms at startup
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now