Solved

Form Caption Thing

Posted on 2004-11-02
343 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
Question by:deathman5
    14 Comments
     
    LVL 17

    Accepted Solution

    by:
    Form3.Caption := 'Hi';
    should be
    self.Caption := 'Hi';
    or even
    Caption := 'Hi';

    Regards, Geo
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    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
    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
     

    Author Comment

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

    Expert Comment

    by:esoftbg
    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
    I will be back after 12 - 16 hours, sorry ....
    0
     

    Author Comment

    by:deathman5
    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
    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
    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
    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
    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
    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
    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
    I like Workshop_Alex's comment .... In this way your application will start faster, because it will create only two Forms at startup
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
    Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

    845 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now