Recent

Author Topic: Multiple forms  (Read 4603 times)

rickielynn

  • New Member
  • *
  • Posts: 18
Multiple forms
« on: November 06, 2017, 12:36:30 am »
I created a program with two forms; Form1 and Form2.  At startup, Form1 appears. A button on Form1 makes Form2 appear by calling Form2.Show.  Form1 is still visible in the background.  When I click back on Form1, Form2 disappears.  I want Form2 to remain visible; ie: be able to click back and forth between them.  I do not want Form2 to be modal.  How can I do this?

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: Multiple forms
« Reply #1 on: November 06, 2017, 12:44:59 am »
that is how show works. Either the form2 wend behind form1 when you clicked on form1 or you have extra code in form2 to hide it when it looses focus.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

soerensen3

  • Full Member
  • ***
  • Posts: 213
Re: Multiple forms
« Reply #2 on: November 06, 2017, 12:57:18 am »
You can make it stay on top though if you set FormStyle to fsStayOnTop.
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

rickielynn

  • New Member
  • *
  • Posts: 18
Re: Multiple forms
« Reply #3 on: November 06, 2017, 01:04:35 am »
There is no extra code to hide Form2.  When I click back on Form1, Form2 seems to get minimized.  I want it to remain as it was.  I tried making Form1 a MDIForm, and Form2 a MDIChild, but it didn't seem to help.

I want Form1 to be the main form with menu, etc, and then be able to display other forms on top of it.  I was able to do this in Visual Basic by making Form1 a MDIForm and the other forms MDIChilds.

Any suggestions?

rickielynn

  • New Member
  • *
  • Posts: 18
Re: Multiple forms
« Reply #4 on: November 06, 2017, 01:18:48 am »
Yes, soerensen3, making Form2 StayOnTop and Form1 Normal seems to work.  Also made Form2.Visible:=False upon Form2.create so that it does not appear until called upon by Form1.

Thanks!

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: Multiple forms
« Reply #5 on: November 06, 2017, 01:34:25 am »
1) make sure that form1 is not maximized after you click on it move it around do you see form2 somewhere behind it? if yes then changing focus brings a form to the top this is how things work.
2) mdi is not working for the win32 widget set for some reason the lcl team did not see any value to implement it.
3) if stay on top solves your problem then try setting the popupParent of form2 to form1 that should solve it too and I prefer it from stay on top forms that will remain on top of other applications as well.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

RAW

  • Hero Member
  • *****
  • Posts: 867
Re: Multiple forms
« Reply #6 on: November 06, 2017, 02:58:56 am »
Play around with this... (PopupParent, PopupMode as @taazz already mentioned...).
If you don't want that Form2 can be maximized or minimized then use BorderStyle... or play around with Form2.Constraints.MaxHeight and so on...
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.8 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1
// This is polarity (hell) and hell is reigned by lies, nothing but lies. Be careful what you believe ...
// There are 10 types of people in this world, those who understand mind control and ...

RAW

  • Hero Member
  • *****
  • Posts: 867
Re: Multiple forms
« Reply #7 on: November 06, 2017, 01:08:26 pm »
Code: Pascal  [Select][+][-]
  1. Procedure TForm1.btnShowClick(Sender: TObject);
  2.  Begin
  3.   If Not Assigned(Form2)
  4.   Then
  5.    Begin
  6.     Form2                      := TForm.Create(Self);
  7.     Form2.PopupMode            := pmExplicit;
  8.     Form2.PopupParent          := Self;
  9.     Form2.Caption              := 'Hi.. I am Form2';
  10.     //Form2.ShowInTaskBar        := stAlways; // only interesting with a normal
  11.                                             // window (bsSizeable, bsSingle)...
  12.     Form2.BorderStyle          := bsSizeToolWin;
  13.     //Form2.BorderIcons          := []; // On Windows bsSizeable...
  14.     Form2.Constraints.MaxHeight:= Self.Height-20;
  15.     Form2.Constraints.MinHeight:= 200;
  16.     Form2.Constraints.MinWidth := 150;
  17.     Form2.Constraints.MaxWidth := Self.Width;
  18.     Form2.SetBounds            (Left+50, Top+50, Width Div 2, Height);
  19.     Form2.Show;
  20.    End
  21.   Else Form2.Show;
  22.  End;
  23.  
  24.  
  25. Procedure TForm1.btnCloseClick(Sender: TObject);
  26.  Begin
  27.   If Assigned(Form2)
  28.   Then
  29.    Begin
  30.     Form2.Release;
  31.     Form2:= Nil;
  32.    End;
  33.  End;
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.8 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1
// This is polarity (hell) and hell is reigned by lies, nothing but lies. Be careful what you believe ...
// There are 10 types of people in this world, those who understand mind control and ...

rickielynn

  • New Member
  • *
  • Posts: 18
Re: Multiple forms
« Reply #8 on: November 06, 2017, 08:28:08 pm »
I found an implementation of MDI forms for Lazarus at http://wiki.lazarus.freepascal.org/LMDI.  This looks like what I am trying to do with multiple forms.

boz

  • New Member
  • *
  • Posts: 10
    • Roving Dynamics Ltd
Re: Multiple forms
« Reply #9 on: November 06, 2017, 11:32:11 pm »
Hi

MDI is quite old fashioned looking IMHO

For larger projects I embed the form in a panel and have a selection of buttons on the left to show which one I want. this also allows you to split up larger projects into multiple units quite easily not quite MDI but you can also have multiple forms showing at one time like a form embedded in another form and then embedded in another form and example is here => http://rodyne.com/wp-content/uploads/2017/11/catch.png  on this the left hand buttons embed a form in the RHS

so in the above example it you create a project like this => http://rodyne.com/wp-content/uploads/2017/11/catch-ide.png

The code you use to embed the form is like this:

Code: Pascal  [Select][+][-]
  1. var
  2.    RHS:Tform;
  3.  
  4. procedure TMainForm.ShowRHS(FormName:Tform); // Embed named form into RHS Panel and show it. Allows us to split program into different forms
  5. begin
  6.   if RHS<>nil then RHS.Close;
  7.   RHS:=formname;
  8.   RHS.Align := alClient;
  9.   RHS.BorderStyle := bsNone;
  10.   RHS.parent := RHSPanel;
  11.   RHS.show;
  12.   RHSPanel.caption:='';
  13.   cfg.activeform:=string(formname.Name);
  14.   RHS.BorderWidth:=2;
  15. end;      
  16.  
  17. procedure TMainForm.b1Click(Sender: TObject); // all buttons on left hand side have there onclick event go here and we embed the correct form..
  18. begin
  19.   if sender=b1 then ShowRHS(HomeForm) else b1.Font.color:=cldefault;
  20.   if sender=b2 then ShowRHS(SalesOrdersForm) else b2.Font.color:=cldefault;
  21.   if sender=b3 then ShowRHS(DryStockForm) else b3.Font.color:=cldefault;
  22.   if sender=b5 then ShowRHS(SemiFinishedStockForm) else b5.Font.color:=cldefault;
  23.   if sender=b6 then ShowRHS(DemandForm) else b6.Font.color:=cldefault;
  24.   if sender=b7 then ShowRHS(ReportsForm) else b7.Font.color:=cldefault;
  25.   if sender=b8 then ShowRHS(DispatchForm) else b8.Font.color:=cldefault;
  26.   if sender=b9 then ShowRHS(PurchaseOrderForm) else b9.Font.color:=cldefault;
  27.   if sender=b10 then ShowRHS(StockControlForm) else b10.Font.Color:=cldefault;
  28.   if sender=b11 then ShowRHS(AccountingForm) else b11.Font.color:=cldefault;
  29.   if sender=b12 then ShowRHS(WetStockForm) else b12.Font.color:=cldefault;
  30.   TBitBtn(sender).Font.color:=clblue;
  31.   cfg.user.LastForm:=TBitBtn(sender).tag; // so I can save which form the user was looking at and restore it on open
  32. end;
  33.  

Hopefully its obvious and helpful.
Boz
Roving Dynamics Ltd
www.rodyne.com

rwebb616

  • New Member
  • *
  • Posts: 18
Re: Multiple forms
« Reply #10 on: January 23, 2021, 02:58:38 am »
Boz,

I notice in your code you have a cfg object - how do you handle that aspect of your apps?  Do you make a singleton or something and load at startup? I'm a beginner and trying to get my head around the concepts of classes and user interfaces.  I really like your UI for this app how you embed forms in the RHS. 

What is the font color thing about?

Rich

jamie

  • Hero Member
  • *****
  • Posts: 4189
Re: Multiple forms
« Reply #11 on: January 23, 2021, 02:02:04 pm »
Please look at the last date this thread was active and if you were aware of that, then please indicate that you are so that we know you are responding to an ancient post.
The only true wisdom is knowing you know nothing

rwebb616

  • New Member
  • *
  • Posts: 18
Re: Multiple forms
« Reply #12 on: January 23, 2021, 04:44:31 pm »
Yes, I am aware.  I responded because the thread referenced some code I was interested in.  I have no idea if "boz" is even still active - just figured if he is he might get notified and see my question.  I wasn't able to find a way to send a private message (at least that I could find) so I just responded to the thread.

Rich

Edit:  Now that I say that I see it's right under his name ... duh!

boz

  • New Member
  • *
  • Posts: 10
    • Roving Dynamics Ltd
Re: Multiple forms
« Reply #13 on: January 24, 2021, 12:37:24 am »
Hi Rich

I'm Still active, just not a frequent visitor.

Nothing high tech here, cfg is not an object, its just a record where I put misc global variables. (I'm quite old fashioned and Easier to just type cfg. in the IDE and the editor will then show all my global variables using autocomplete so I dont have to remember them!)

Code: Pascal  [Select][+][-]
  1. type
  2.   Tcfg=record
  3.     ActiveForm,CompanyName,TempDir,UserDir,PCName,version,build,ReportPrinter,LabelPrinter,PageSize:string;
  4.     LastBackup:TdateTime;
  5.     MsgTimeout,ActivityTimeout,Session:integer;
  6.     IsProduction:boolean;
  7.   end;
  8.  
  9.  var
  10.    cfg:Tcfg;


So basically cfg.activeform is just a string where I store the last form the user selected so when the user starts the program next time it can open the last form they used

Code: Pascal  [Select][+][-]
  1. for i:=1 to screen.FormCount-1 do
  2.   if screen.Forms[i].Name=string(cfg.activeform) then SHOWRHS(screen.Forms[i],f);
  3.  
Boz
Roving Dynamics Ltd
www.rodyne.com

boz

  • New Member
  • *
  • Posts: 10
    • Roving Dynamics Ltd
Re: Multiple forms
« Reply #14 on: January 24, 2021, 01:06:24 am »
My Delphi winery management program uses the exact same principles but has collapsible panels on the left hand side and 60+ different screens it embeds on the right hand side.  https://rodyne.com/wp-content/uploads/2014/04/bulls-i-winemaking.png  It works well for very large projects that would otherwise become unmanageable as you can swap out forms easily when they depreciate or need a rewrite. I'm generally an open source company but both these projects are not open due to the clients wishes so I cannot give you the source but as I said it should be an easy concept to grasp.

Also I'm an electronics engineer who knows just enough programming to be dangerous so there will be other and probably better ways out there :)

Good luck with your project
Boz
Roving Dynamics Ltd
www.rodyne.com

 

TinyPortal © 2005-2018