Setting Main Form and Calling Another Form

Hi All:

Again, I am new to Delphi.  I am using XE2 Pro.

I am attempting my first app.  I want it to have multiple forms, with the Main form calling other forms.

How do I set which form is the form that opens at the start of application?

How do I get frmMain to call frmSecondary?

Thanks in advance.
0
Thomas
2/15/2013 4:32:55 PM
embarcadero.delphi.firemonkey 4901 articles. 4 followers. Follow

14 Replies
6325 Views

Similar Articles

[PageSpeed] 51
Get it on Google Play
Get it on Apple App Store

> {quote:title=Thomas Clark wrote:}{quote}
> Hi All:
> 
> Again, I am new to Delphi.  I am using XE2 Pro.
> 
> I am attempting my first app.  I want it to have multiple forms, with the Main form calling other forms.
> 
> How do I set which form is the form that opens at the start of application?

In Delphi go to Project/View Source. This opens the main project file, whih will look like:
{code}
begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.
{code}

The first form created there will be the main form.
> 
> How do I get frmMain to call frmSecondary?
> 
To use a form, first add the unit to one of the uses clauses:

{code}
implementation
uses uForm2;
{code}

If you need to reference the unit from the interface section, use the interface sections uses clause, otherwise use that of the implementation section (this makes compilation more efficient and reduces circular unit call issues).

Now you need to create and show the form:
{code}
procedure TForm1.Button1Click(Sender: TObject);
var F2: TForm2;
begin
  F2 := TForm2.Create(Application);
  F2.ShowModal;
  F2.Free;
end;

If you want to show the form non-modal you'll need a suitable field in your form object and you'll probably want to create/destroy it in FormCreate/FormDestroy respectively.


Note also that the project file (discussed above) by default creates an instance of every form in your project which you can use, but it's bad practice.

--
MonkeyStyler FireMonkey style designer http://bit.ly/PzxKyI
Embarcadero MVP
0
Mike
2/15/2013 5:55:54 PM
Le 15/02/13 17:55, Mike Sutton a écrit :

Just one thing...

If you're going to create the form like this:

 > {code}
 > procedure TForm1.Button1Click(Sender: TObject);
 > var F2: TForm2;
 > begin
 >    F2 := TForm2.Create(Application);
 >    F2.ShowModal;
 >    F2.Free;
 > end;
 > {code}

.... don't create it on startup as well:

> {code}
> begin
>    Application.Initialize;
>    Application.CreateForm(TForm1, Form1);

// remove this next line
>    Application.CreateForm(TForm2, Form2);
>    Application.Run;
> end.
> {code}

Joanna

-- 
Joanna Carter [Team OOAD]
0
Joanna
2/15/2013 6:04:44 PM
Mike & Joanna:

It works!  Glorious!  Thank you so much.

I included Joanna's suggestion to not create what I will call 'subform applications' until I click on the button that calls the form.

From my Foxpro days I would use an 'h' for 'handle' (essentially an alias for a component/object to my mind).  'frm' for form object.  'btn' for a button object.  So a handle for a form object would be 'hfrm'.

I am using a main form to call frmDev (my development form) and frmEnv (my environment form).

Here is the code for my main form:

{code}
unit uMain;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs;

type
  TfrmMain = class(TForm)
    btnDev: TButton;
    btnEnv: TButton;
    procedure btnEnvClick(Sender: TObject);
    procedure btnDevClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation
  uses
    uEnv, uDev;

{$R *.fmx}

procedure TfrmMain.btnDevClick(Sender: TObject);
var
  hfrmDev : Tform;
begin
  hfrmDev := TfrmDev.Create(Application);
  hfrmDev.ShowModal;
  hfrmDev.Free;

end;

procedure TfrmMain.btnEnvClick(Sender: TObject);
var
  hfrmEnv : Tform;
begin
  hfrmEnv := TfrmEnv.Create(Application);
  hfrmEnv.ShowModal;
  hfrmEnv.Free;

end;

end.
{code}

Here is my Project code:

{code}
begin
  Application.Initialize;
  Application.CreateForm(TfrmMain, frmMain);
  //  Application.CreateForm(TfrmDev, frmDev);
  //  Application.CreateForm(TfrmEnv, frmEnv);
  Application.Run;
end.
{code}

Edited by: Thomas Clark on Feb 15, 2013 11:48 AM
0
Thomas
2/15/2013 7:48:38 PM
I guess I must have forget to mark this as a question because I couldn't give Mike & Joanna credit for solving my issue?  Well they most definitely hit the nail on the head!
0
Thomas
2/15/2013 7:53:16 PM
Le 15/02/13 19:48, Thomas Clark a écrit :

> It works!  Glorious!  Thank you so much.

No problem.

> {code}
> procedure TfrmMain.btnDevClick(Sender: TObject);
> var
>    hfrmDev : Tform;
> begin
>    hfrmDev := TfrmDev.Create(Application);
>    hfrmDev.ShowModal;
>    hfrmDev.Free;
>
> end;
> {code}

A couple of suggestions.

1. If you intend to show forms modally then you can do the following...

{code}
procedure TfrmMain.btnDevClick(Sender: TObject);
begin
   with TfrmDev.Create(nil) do
   try
     ShowModal;
   finally
     Free;
   end;
end;
{code}

2. Don't use prefixes, they really aren't necessary, are very 
old-fashioned and make code harder to read :-)

Wht is more, Delphi forms are not held by handles, they contain a 
handle, so definitely don't use the h prefix.

Joanna

-- 
Joanna Carter [Team OOAD]
0
Joanna
2/15/2013 8:22:11 PM
> {quote:title=Joanna Carter (Team OOAD) wrote:}{quote}
> 2. Don't use prefixes, they really aren't necessary, are very 
> old-fashioned and make code harder to read :-)

<rant>
I totally agree on that one. Hungarian notation was invented at microsoft to provide some type safety in weakly typed languages. It has no purpose in a strongly typed language like Delphi which will pick up any mis-assignments at compile time. 

It's sole purpose nowadays is to make code harder to read.
</rant>

--
MonkeyStyler FireMonkey style designer http://bit.ly/PzxKyI
Embarcadero MVP
0
Mike
2/15/2013 8:49:35 PM
Regarding prefixes: 

I am sure you guys are right if you are a prof. programmer and-or really fluent in the language, but I need the prefixes to orient myself.  Otherwise, I find myself getting lost.
0
Thomas
2/15/2013 10:56:50 PM
Le 15/02/13 22:56, Thomas Clark a écrit :

> Regarding prefixes:
>
> I am sure you guys are right if you are a prof. programmer and-or
> really fluent in the language, but I need the prefixes to orient
> myself.  Otherwise, I find myself getting lost.

I can understand what you mean but, nonetheless, you would be well 
advised to gently change your ways :-)

Personally, I would use prefixes like btn... edit... combo... but you 
really will benefit from dropping the type prefixes, especially the h 
for any type, especially forms, since that is completely misleading and 
could be even more confusing if you ever had to resort to accessing real 
Windows handles - something that is unusual in everyday Delphi programming.

Delphi is an abstraction layer on top of the Windows APIs and leaving 
the old ways encouraged all those years ago, when you had to get a lot 
closer to the metal, will help you - really :-)

Joanna

-- 
Joanna Carter [Team OOAD]
0
Joanna
2/15/2013 11:38:03 PM
Joanna Carter (Team OOAD) <"Joanna Carter [Team OOAD]" <" "> wrote:
> Le 15/02/13 22:56, Thomas Clark a écrit :
> 
>> Regarding prefixes:
>> 
>> I am sure you guys are right if you are a prof. programmer and-or
>> really fluent in the language, but I need the prefixes to orient
>> myself.  Otherwise, I find myself getting lost.
> 
> I can understand what you mean but, nonetheless, you would be well 
> advised to gently change your ways :-)
> 

I will just stick in my oar to defend the use of Hungarian. Way back when I
was a young programmer, we had a bug that cost us days of time. When we
worked out what it was, we resolved to do whatever it took to ensure that
it could never happen again. And we applied the principle as other bugs
were found. IIRC the bool "writing solid code" came out about a year later,
and was devoured by us. Hungarian is a tool for exactly that purpose -
eliminating bugs before they happen. Now, sure, typed languages help a lot,
but having the type as a prefix has helped me stop errors that I might not
have noticed when I have had to rename a variable and then check every
reference to correct it. Hungarian only makes it hard to read until you are
familiar, and then it just helps. The cost of me using it is zero, the
benefit is high.

-- 
Matthew Jones
0
Matthew
2/16/2013 2:17:08 PM
Le 16/02/13 14:17, Matthew Jones a écrit :

> Hungarian is a tool for exactly that purpose - eliminating bugs
> before they happen. Now, sure, typed languages help a lot, but having
> the type as a prefix has helped me stop errors that I might not have
> noticed when I have had to rename a variable and then check every
> reference to correct it. Hungarian only makes it hard to read until
> you are familiar, and then it just helps. The cost of me using it is
> zero, the benefit is high.

An interesting POV but have you seen this? 
http://en.wikipedia.org/wiki/Hungarian_notation#Notable_opinions

Nowadays we have Code Insight, code completion, refactoring, all built 
in to the IDE.

In Delphi, all you have to do is hover over a variable and you will get 
a popup balloon with the type information - it will even lead you where 
it was declared.

Before such modern IDEs I would have agreed with you but now?

Joanna

-- 
Joanna Carter [Team OOAD]
0
Joanna
2/16/2013 4:28:34 PM
Joanna and All:

Well you have been taking me by the hand here as I start my Delphi journey, but I just have to say, I find it helpful for me.  And the fact that I misuse 'h' for a handle that is not a handle only reinforces my opinion, because it clues my mentor into the fact that I am under a cloud of misperception. I would much appreciate to be straightened out on the issue.  The opportunity for which might never happen until my mentor spots it.

Next, this is not my first rodeo.  While I am a novice programmer and have never risen above being competent at FoxPro, I have done a bit of programming and before I used prefixes -- I didn't use them.  I started using them because I found them useful.  This was in FoxPro.  And for its shortcomings it was highly typed.  Yet, I still found it useful.

For example iPos tells me that this is a variable that I created and it takes an integer.  I don't have to hover or do anything.  I can see it on paper and know exactly that this is not a keyword of Delphi etc.  It's my creation.  Now if I see Pos, I don't know jack about that unless I go through the machination to see if it was declared etc.  

So while HN is harder for you to read, it is easier for me.  It's like saying that I should speak Britain English to my family her in the states.  Maybe Britain English is more proper, but I can't understand it as easy as U.S. English.  So it's a matter of personal preference.

The only reason I mention this is not to convince you that my way is best, I mention this to convince you that HN is best for me and I hope that you will accept that.  It's how my mind works.



> {quote:title=Joanna Carter (Team OOAD) wrote:}{quote}
> An interesting POV but have you seen this? 
> http://en.wikipedia.org/wiki/Hungarian_notation#Notable_opinions
> 
> Nowadays we have Code Insight, code completion, refactoring, all built 
> in to the IDE.
> 
> In Delphi, all you have to do is hover over a variable and you will get 
> a popup balloon with the type information - it will even lead you where 
> it was declared.
> 
> Before such modern IDEs I would have agreed with you but now?
> 
> Joanna
> 
> -- 
> Joanna Carter [Team OOAD]
0
Thomas
2/16/2013 5:25:57 PM
Le 16/02/13 17:25, Thomas Clark a écrit :

> For example iPos tells me that this is a variable that I created and
> it takes an integer.  I don't have to hover or do anything.  I can
> see it on paper and know exactly that this is not a keyword of Delphi
> etc.  It's my creation.  Now if I see Pos, I don't know jack about
> that unless I go through the machination to see if it was declared
> etc.

Of course the better option is to realise that, in Delphi as well as 
other modern languages, you can use longer variable names, which are 
much more understandable and much more self-documenting.

> The only reason I mention this is not to convince you that my way is
> best, I mention this to convince you that HN is best for me and I
> hope that you will accept that.  It's how my mind works.

And that's fine; it's just that some of us have many years of evolving 
programming experience and, together with modern IDEs, are very grateful 
that we no longer have to use HN and wonder why anybody would bother 
with it anymore :-)

Joanna

-- 
Joanna Carter [Team OOAD]
0
Joanna
2/16/2013 5:45:12 PM
> {quote:title=Thomas Clark wrote:}{quote}
> Next, this is not my first rodeo.  While I am a novice programmer and have never risen above being competent at FoxPro

I don't know FoxPro but I'm assuming it's quite a basic language. You would probably do well to bone up on the theory of things like object orientation (properties, inheritance etc), recursion, anonymous methods, generics etc. in order to understand some of the advanced concepts of a modern language. Also things like design patterns, agile development and unit testing may be things you haven't come across before.

I'm sure people can recommend texts on all the above if you're interested. For Delphi specific stuff, Marco Cantu has some good books available - http://www.marcocantu.com/handbooks/ as well as Chris Rolliston's definitive tome http://delphifoundations.com/

As a point of interest, since you're new to Delphi, can I ask what made you decide to start coding with it?

--
MonkeyStyler FireMonkey style designer http://bit.ly/PzxKyI
Embarcadero MVP
0
Mike
2/16/2013 10:43:12 PM
> {quote:title=Mike Sutton wrote:}{quote}
>As a point of interest, since you're new to Delphi, can I ask what made you decide to start coding with it?

Mike: I do not like chasing Microsoft who I consider to be the 'ever changing without innovating' that is 'change for the sake of selling product' company.  Microsoft left their FoxPro programming community high and dry just as the product was catching up in terms of designing a modern user interface.  (FoxPro came from a datacentric place, and I loved it for the ability to manipulate databases and the tables.)  Many feel and I believe it as well that FoxPro was getting too good for its own britches and p
osed a threat to Microsoft's own expensive SQL server solutions.  

The C variants are impenetrable to me.

I was looking for a solution that was not Microsoft, well-established, commercially supported, and that search led me to Pascal/Delphi.
0
Thomas
2/17/2013 4:41:42 PM
Reply:

Similar Artilces:

Setting the Size of a Modal Form called by a Main Form
Why do I get an error for clicking on btnMaxClick? {code} unit uEnv; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs; type TfrmEnv = class(TForm) img0101: TImageControl; img0201: TImageControl; img0301: TImageControl; img0401: TImageControl; img0501: TImageControl; img0601: TImageControl; img0701: TImageControl; img0102: TImageControl; img0202: TImageControl; img0302: TImageControl; img0402: TImageControl; img0502: TI...

How do I mark a form as being the main form? Main form drop down is blank!
So I found online that I can go to Project->Options The Under Forms, pick the main form. The problem is that the Main form drop down is blank, all the forms appear under 'Available forms' but just not in that drop down. any thoughts? Thank you! Matt You have to have the following code on your main form: void setAsMainForm() { TfrmMain::SetAsMainForm(); } on your main form. IW does this automatically when you create a new project. I usually just rename the created form to whatever I want the main form named. On 7/23/2012 6:54 AM, Matthew Czarnek wrote: >...

calling control in one form from another form
hi all, i have two forms in a C#.net windows application i have a list view control in form1. i want to fill this list view from form2 and display form1 from form2. any idea of doing this?thanks in advance use showdialog to get the input.Thank You--some funny homemade software,http://www.geocities.com/lhcsoft2004/my software grouphttp://groups.yahoo.com/group/LauHanChingSoftware/...

calling a web form from another web form
I would like to know what is the best way to open a web form from another web form. I would like to call it from a button control. Thanks Well, if I've grasped your question right, there are many methods to do that: 1)Response.Redirect > To navigate to another page from code and is equal to clicking a hyperlink. I suggest this as I think it's closer to your question. 2)Server.Transfer > This ends the current web form and begins a new web form. It's usable when you want to pass some variables i.e (Label1.Text) to the next immediate page. It's not safe though and not used to send p...

Setting focus when a form displays another form
I have a form that I am displaying modelessly and in the OnActivate event, I show another form modelessly. I want the second form to have focus. When the first form is visible, then this works as I want. If the first form is not visibile and then is shown modelessly, the second form when shown does not get focus. The first form does. How do I make the second form get focus in this case? ...

How to call One FORM from Another FORM IN VB.NET
I AM NEW TO VB.NET, I CREATE A REPORT USING REPEATER COMPONENT (FORM NAME IS REP_VIEW) IN ANOTHER FORM (REP_GET) I GET CODE,MONTH AND YEAR FROM THIS FORM (REP_GET) WHEN I RUN THE FORM REP_GET AND GET DETAILS OF CODE,MONTH,YEAR AND I CLICK THE VIEW BUTTON IN REP_GET FORM IMMEDAIATELY FORM_VIEW FORM HASTO BE OPEN AND REPORT  HAS TO BE VIEWED.  BASED ON CODE,MONTH,YEAR SQL QUERIES ARE IN CODE BEHIND OF FORM_VIEW , CONDITIONS ARE CODE,MONTH,YEAR,      WHICH IS GET FROM FORM_GET   PLEASE HELP ME   THANKS  ...

Forms or No Forms
Hope this isn't a repeat.  I looked and didn't find anything. Say for example you had a web page with around 20 fields retrive and update to a database with differing datatypes.  Some fields will have some special controls (dropdowns, radios, buttons, etc) that interact with other parts of the page.  What is the best and/or most efficient code to make the page. Would you use a Formview bound to a standard ADO.net SQL connection and edit the templates. Use a Formview bound to an Object Data Source and edit within the templates. Or use an Object Data Source and bind the...

How do I know the called form is returned to the calling form?
My FormA called FormB. When FormB is released, the calling form(FormA) can drive any event, or any kind of method to know that? You could do it by setting and checking a variable in the UserSession. FormB could set a value like UserSession.FormBKilled : Boolean. FormA could then check that value and act upon it. The other option would be to have form B call a method, or set a value, on FormA before it gets destroyed. procedure TFormB.ExitBtnClick(sender: TObject); var AForm : TIWForm; begin WebApplication.SetActiveForm(WebApplication.FindComponent('FormA') as TIWContai...

Can a login.aspx form be disabled if called from a form in another folder?
I have an ASP.NET 2.0 app that is secured using a login.aspx form.  If any user jumps to any aspx form in my app folder it triggers the login form.  Now, I am tring to tie in code written by another company that is currently in a different folder.  The two web apps need to call each other in many places.  If I add a login.aspx form to the other folder, it gets triggered every time the user switches between apps.  I want to make it look like one seamless app, to the user so is there a way to disable the login form if one app calls the other or some other way...

form within form
The form in the content page is simply accessing an ajax/google javascript function, but because the master page has the "form... runat=server" bit in it, when the form in the content page is submitted, it submits the main form, not the google map form!  I don't want any postback on the google content page javascript form, just a "<form name="addr" method="post" action="" onsubmit="javascript:..." so that it simply runs the javascript on the page without a post back... I investigated "attribute.add" but it seems I ...

Independent Forms of the Main Form
Hi... Im creating a form independent of the main form. But when that form call another modal form, i want to that modal only "locks" the indepent form, and not the entire application. I´ve tried to use PopupParent property, but didnt have effect or im using in the wrong way. To do that im doing this: // FORM 1: The main form type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; procedure TForm1.Button1Click(Sender: TObject); be...

Form as Child of another Form
Hi. Maybe my first mail describing the problem was too complex. A simple question then: Why do edit controls don't work correctly, if created in a child form of another form? --- snip --- void __fastcall TForm1::Button1Click(TObject *Sender) { TfrmTest* frmTest = new TfrmTest(Application); frmTest->Parent = this; frmTest->Show(); } --- snap --- If 'TfrmTest' contains edit-controls they no longer accept focus using the mouse. Any help would be appreciated. Thanks Eike Petersen > {quote:title=Eike Petersen wrote:}{quote} > ...

Assign TextBox Value From 1 Form to Another Form on another page
I have searched for days without finding the answer that Im looking for. I have created a marketing request form table called tblRequestForm with a few basic fields. [RequestFormID] , [FormTitle] , [Department] , [YourName]. I also have created another table called tblRequests that has these fields. [RequestID] , [RequestFormID] , [RequestTitle] , [RequestDescription]. I am trying to fill out the RequestForm on page1.aspx then on insert, postback the [RequestFormID] automatically into FormRequest on page2.  I am really new to this and help would be great.~Mickey...

How do you call a windows form from a web form?
hi,  i doing a vb project which include a web form and a windows form.   How do you call a windows form from a web form? can any kind souls plz tell mi haha....my email is EDITED. i appreciate the help   regards kit  EDITED By: longhorn2005 This gives you an idea using VS 2003-- most of it is still relevant:  http://msdn.microsoft.com/msdnmag/issues/02/01/UserCtrl/---------------------------------------MCP - Web Based Client Development .NET 2.0 I am attempting to do something similar, I have an existing windows form application that I was asked to i...