Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Several windows


  • Please log in to reply
13 replies to this topic

#1 BramS

BramS

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 11 February 2011 - 07:24 AM

Hi,

I've got an other question (again).

How can make several windows (Forms)?
I want to make a separated menu, like my first form is my menu then I press ok
and the second form is my actual program. And maybe let the menu window close.

Is this possible?

Edited by BramS, 11 February 2011 - 09:20 AM.

  • 0

#2 Zorfox

Zorfox

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 11 February 2011 - 04:26 PM

I want to make a separated menu, like my first form is my menu then I press ok
and the second form is my actual program. And maybe let the menu window close.

What do you mean by a separated menu that is also a form?

It sounds like your asking if it is possible to show a preliminary window before your application. When the user clicks a menu or button show the "main" form which is your program. That is a splash screen that waits for user input. I won't bother to explain how to make a splash screen. There are thousands of examples out there already.

If I misunderstood PLEASE ask again.
  • 0

#3 BramS

BramS

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 12 February 2011 - 02:13 AM

What I mean works like an instalation wizard.

e.g: You have to give your name at the first page, you press next and you have to choose a mode or something like that, and the next page is the actual program and the user input will be used in it.
I don't know if this works like a splash window.

Or do I have to work with page controls or tabs?
  • 0

#4 Zorfox

Zorfox

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 12 February 2011 - 06:34 AM

Thanks for the clarification! There are many ways to do this. Page controls happen to be one of them. It is up to you how you want the application to perform rather than have the development platform dictate how it "has to be done". That's one reason Delphi is such a nice platform.

If you choose to uses multiple forms then I would edit the project source. This is how splash screens are used as well by the way. From the menu choose Projects then view source. The project source will now appear in the editor. To hide the main form but show another add Application.ShowMainForm := False; to the project source. I.E.

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.ShowMainForm := False;
  Form2.Show;
  Application.Run;
end.

You can easily add as many forms as you like hiding and showing them in the project source. In the above example, just add the Form1.Show statement to the menu, button or whatever you like when you want the main form (form1 here) to be seen. You can start on form 20 if you like hiding and showing them until you get to your main form. The sky is the limit! Below is an example.

Start a new project with two forms. Add a button and memo on form1. Add a button and two checkboxes on form 2.

Add this to your project source...

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.ShowMainForm := False; //This was added
  Form2.Show; //This was added
  Application.Run;
end.

Add application.Terminate to the Onclick event of Form1's button

procedure TForm1.Button1Click(Sender: TObject);
begin
  Application.Terminate;
end;

Add this code to the OnClick event of Form2's button...

procedure TForm2.Button1Click(Sender: TObject);
begin
  if (CheckBox1.Checked = False) and (CheckBox2.Checked = False) then begin
    ShowMessage('You must select a mode first!');
    Exit;
    end else
  if (CheckBox1.Checked = True) and (CheckBox2.Checked = True) then begin
    ShowMessage('Only one mode at a time please!');
    Exit;
    end else

  if CheckBox1.Checked = True then
    Form1.Memo1.Lines.Add(Edit1.Text + ' is logged in. Mode one is being used');
  if CheckBox2.Checked = True then
    Form1.Memo1.Lines.Add(Edit1.Text + ' is logged in. Mode two is being used');
  Form1.Show;
  Form2.Hide;
end;

Now you have a preliminary form that controls what happens to the main form. Those values can be from an ini file or the registry if you need to remember them for the next time.

Edited by Zorfox, 12 February 2011 - 07:01 AM.
Added example

  • 0

#5 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 12 February 2011 - 06:52 AM

What I mean works like an instalation wizard.

e.g: You have to give your name at the first page, you press next and you have to choose a mode or something like that, and the next page is the actual program and the user input will be used in it.
I don't know if this works like a splash window.

Or do I have to work with page controls or tabs?

I've seen this implemented with tab controls where only one tab is visible at a time.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#6 BramS

BramS

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 12 February 2011 - 07:18 AM

@Zorfox: Thank you, I'll try this.
  • 0

#7 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 12 February 2011 - 07:51 AM

The easiest way is to use a TPageControl. With it you can put controls in design time for each page. And adjust shown page index in Next or Previous buttons OnClick event handlers according to which page you are currently at and values supplied by user. It's the easiest method for the minimal code it requires in adjusting what to display. With TTabControl you still have to code the display of the page. With separate forms, like zorfox suggested, you would need to "synchronize" the basic looks of the forms (eg. button Next, Previous, and Cancel buttons position). And also there will be a slight flicker when the current form hiding and the next shown.

However if you familiar with embedding forms (or frames) into other form, then actually it's the easiest to manage when the project size is medium or big, or the flow is quite complex.

Anyway you need to define a class to track what information had been supplied by user, the current page, and what calculation to do when use click the current Next or Previous button.

Add application.Terminate to the Onclick event of Form1's button


While there is no harm in using Application.Terminate to close a Delphi application, but the best and most recommended way is to call the main form's Close method.

Edited by LuthfiHakim, 12 February 2011 - 08:40 AM.

  • 0

#8 BramS

BramS

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 12 February 2011 - 08:34 AM

I did this as first simple try

in Unit1:
procedure TForm1.Button1Click(Sender: TObject);
begin
  Form1.hide;
  Form2.show;
end;

in the project source:
program project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Forms, unitA, LResources, Unit2
  { you can add units after this };

{$IFDEF WINDOWS}{$R project1.rc}{$ENDIF}

begin
  {$I project1.lrs}
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Form2.hide;
  Application.Run;
end.


I also tried the example of zorfox
Both didn't work, maybe did I misunderstand Zorfox and placed the code on the wrong place?
  • 0

#9 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 12 February 2011 - 08:44 AM

What did not work?
...
begin
  {$I project1.lrs}
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Form2.hide; ***
  Application.Run;
end.

Actually the line I marked with *** is not required, you can set Form2's visible property into False in design time to avoid it automatically shows when the program starts.
  • 0

#10 BramS

BramS

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 12 February 2011 - 08:49 AM

@LuthfiHakim: The code of Unit1 didn't work, didn't recognize form2
I have actually no idea where I have to place the code of Zorfox (project source, unit1, unit2?)
  • 0

#11 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 13 February 2011 - 12:34 AM

That's because you did not add or create Form2. Through lazarus IDE, add new form (menu File - New Form. The new form will be automatically named Form2 (if your only form is currently named Form1). Then open Form1 unit, and add Form2 into its local uses list (uses list after implementation keyword). After that, your Form1 will recognize Form2.
  • 0

#12 Zorfox

Zorfox

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 13 February 2011 - 09:31 AM

@LuthfiHakim: The code of Unit1 didn't work, didn't recognize form2

As LuthfiHakim said this is most likely because you didn't add Unit2 to the uses clause in Unit1. I am not familiar with lazarus, LuthfiHakim would be the person to ask about that. Whenever I forget to add another form to the uses clause Delphi always asks if I want to then does it. In case there is some reason your IDE is not setup for that add the uses clause after the implementation i.e.

for Unit1, add -> uses Unit2 <- right after the word implementation and vise versus for Unit2.

For unit1,
implementation

uses Unit2; <---Add this

For Unit2,
implementation

uses Unit1; <---Add this


I have actually no idea where I have to place the code of Zorfox (project source, unit1, unit2?)

There were three places for the code. I did point that out. In the post just above each code block it states where to put the code. In the project source you only need to add 2 lines after Application.CreateForm(TForm2, Form2);
Application.ShowMainForm := False; //This is added
Form2.Show; //This is added


As LuthfiHakim pointed out Form2.Show is not neccesary if you set Form2's visible property to true at design time. This was merely an example to show the basic process.

The other two code sections are added to the Button.onclick events of the forms. While designing the application, double click the button you dropped on the form. The editor will appear and the cursor will be between the begin and end of this code,

procedure TForm1.Button1Click(Sender: TObject);
begin

end;

The code I posted, minus what is created by Delphi as above, goes between the begin and end. If you simply paste the code without double clicking the button to open the editor Delphi will complain 'Undeclared Identifier: 'Button1.Click'. The onclick event is added by Delphi to other files we aren't dealing with right now when you add the onclick event that way.

By the way, in your first example you would have had problems. IF form2 had appeared and Form1 disappeared you would not have been able to close the application without resetting it in Delphi. ALWAYS provide a way to close the application wherever you happen to be. Closing form2 would not have exited the application. Just food for thought.

@LuthfiHakim: Good advise, MainForm.Close. Thanks!
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download