Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Form Caption Thing

Posted on 2004-11-02
14
Medium Priority
?
356 Views
Last Modified: 2010-04-05
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
Comment
Question by:deathman5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +2
14 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 80 total points
ID: 12473872
Form3.Caption := 'Hi';
should be
self.Caption := 'Hi';
or even
Caption := 'Hi';

Regards, Geo
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12474463
Possible:
1).  Application.CreateForm(TForm3, Form3);
2).  Form3 := TForm3.Create(Application);
3).  Form3 := TForm3.Create(Self); // but not in          procedure TForm3.....
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12474601
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:deathman5
ID: 12474798
mmm, can u please explain whats the difference if the owner was Self or Application of nil?
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12475134
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 12475146
I will be back after 12 - 16 hours, sorry ....
0
 

Author Comment

by:deathman5
ID: 12475401
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
 
LVL 5

Expert Comment

by:paulb1989
ID: 12478432
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
 
LVL 12

Assisted Solution

by:esoftbg
esoftbg earned 80 total points
ID: 12480423
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
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12481435
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
 

Author Comment

by:deathman5
ID: 12486134
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 12486963
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
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12488707
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 12491038
I like Workshop_Alex's comment .... In this way your application will start faster, because it will create only two Forms at startup
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question