How can I embed a form in a Panel?

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
User avatar
mandarin
Posts: 6
Joined: Friday 29th November 2024 6:43am

How can I embed a form in a Panel?

Post by mandarin »

Hello!

I 'm a novice user of Gambas3 (version 3.19.5 on Linux Mint 21.3 and 22.0 -different computers- / installed Gambas3 the right way on both ;) ), and I 'd like to ask how can I embed a form in a Panel. (Form / Panel same dimensions, designated as neither expandable, nor shrinkable.)

I coded thus (with Form1 already designed) :


Public Sub FMain()
  Application.MainWindow = Window
  Panel1.Enabled = True
  FMain.Children = Panel1
  Form1.Parent = FMain.Panel1
  Form1.Move(0, 0)
End

Public Sub Menu1_Click()
  Form1.Show()
End


...but my Form1 seems hovering over the panel (and at different starting coordinates) and not inside it. Also, when I insert the "Form1.Parent = FMain.Panel1" directive in the Public Sub Menu1_Click() subroutine, the program execution stops with a "read-only" error on Form1.Parent.

Please note that I started the project as a graphical application, and checked the Project / Properties / Components as displayed on the attachment.

So:

- What did I do wrong?
- And why do I need a double press on the Menu1 area, to get a result (even erroneous)?

Thank you!
Attachments
Project-Properties-Components.png
Project-Properties-Components.png (42.72 KiB) Viewed 585 times
User avatar
thatbruce
Posts: 244
Joined: Saturday 4th September 2021 11:29pm

Re: How can I embed a form in a Panel?

Post by thatbruce »

mandarin wrote: Friday 29th November 2024 7:10am
I coded thus (with Form1 already designed) :


Public Sub FMain()
  Application.MainWindow = Window
  Panel1.Enabled = True
  FMain.Children = Panel1
  Form1.Parent = FMain.Panel1
  Form1.Move(0, 0)
End

Public Sub Menu1_Click()
  Form1.Show()
End

This is wrong in so may ways!
I presume that is the code for FMain?

Public Sub FMain()                                          <-- Strange name! Did you mean Public Sub Main?  
  Application.MainWindow = Window                    <-- No, not a good idea
  Panel1.Enabled = True                                       <-- Should be enabled by default but doesn't really add anything anyway
  FMain.Children = Panel1                                   <-- Definitely BAD! This is trying to destroy the FMain structure!
  Form1.Parent = FMain.Panel1                          <-- Form1 has not been instantiated. Why this doesn't raise a fatal error I cant even guess.
  Form1.Move(0, 0)                                              <-- No,no,no
End

Public Sub Menu1_Click()
  Form1.Show()
End


Try this but first try to understand it from the above comments.
In FMain code:
Public Sub Form_Open()
Dim subform as new Form1(Panel1)
Application.MainWindow=Me
End


As I have said many times before:
Read every word in the help, every word.

hth
thatbruce
User avatar
mandarin
Posts: 6
Joined: Friday 29th November 2024 6:43am

Re: How can I embed a form in a Panel?

Post by mandarin »

Thanks a lot! You saved me a couple of months of searching through the Gambas3 documentation! ;)

I tried this scheme and it works fine (still necessary to click twice on Menu1, though) :

Public Sub Form_Open()
  Application.MainWindow = Me
  Dim subform As New Form1(Panel1)
  subform.Visible = False
End

Public Sub Menu1_Click()
  Dim subform As New Form1(Panel1)
  subform.Visible = True
  subform.Show
End


(On my previous try, Form1 sat correctly on (0, 0); but the dimensions of Panel1 were slightly bigger, due to my fault.)
User avatar
BruceSteers
Posts: 1931
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How can I embed a form in a Panel?

Post by BruceSteers »

Welcome to the wonderful world of gambas.

There are 2 ways to embed a Form into Container...

Method 1
As an instance (will be like a copy of FMain and you can add as many as you want)
(Like other Bruce says)

Private $hFMain As FMain 

Public Sub EmbedForm()

  $hFMain = New FMain(Panel1) As FMain
  $hFMain.Show

End


Now reference the embeded form using $hFMain


Method 2 , Directly add FMain

FMain.Load(Panel1)
FMain.Show


Now reference the embeded form as FMain

The second method using Form.Load can only work with Forms. The first method using New cn be used to embed any object/control into any container.


And like the other Bruce says ,, read the wiki more. then read it some more :)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1931
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How can I embed a form in a Panel?

Post by BruceSteers »

mandarin wrote: Friday 29th November 2024 12:11pm Thanks a lot! You saved me a couple of months of searching through the Gambas3 documentation! ;)

I tried this scheme and it works fine (still necessary to click twice on Menu1, though) :

Public Sub Form_Open()
  Application.MainWindow = Me
  Dim subform As New Form1(Panel1)
  subform.Visible = False
End

Public Sub Menu1_Click()
  Dim subform As New Form1(Panel1)
  subform.Visible = True
  subform.Show
End


(On my previous try, Form1 sat correctly on (0, 0); but the dimensions of Panel1 were slightly bigger, due to my fault.)
You are creating and adding Form1 twice there.

you should only create it once then use the Visible property to show or hide it..

Public Sub Form_Open()
  Application.MainWindow = Me
  Form1.Load(Panel1)
  Form1.Visible = False
End

Public Sub Menu1_Click()
  Form1.Visible = Not Form1.Visible  ' toggle visibility
End
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1931
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How can I embed a form in a Panel?

Post by BruceSteers »

mandarin wrote: Friday 29th November 2024 12:11pm
Thanks a lot! You saved me a couple of months of searching through the Gambas3 documentation! ;)
I think you mean a couple of "minutes" ;)
It's not that much of a maze.

Pages of interest...
https://gambaswiki.org/wiki/comp/gb.qt4
https://gambaswiki.org/wiki/comp/gb.form

https://gambaswiki.org/wiki/lang
https://gambaswiki.org/wiki/cat
https://gambaswiki.org/wiki/howto

Also check out the Application repository https://gambaswiki.org/wiki/app
And the Farm (select farm from gambas welcome screen)
Examining other peoples code can be a great learning tool :)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
mandarin
Posts: 6
Joined: Friday 29th November 2024 6:43am

Re: How can I embed a form in a Panel?

Post by mandarin »

Thanks a lot, gentlemen!

Gambas3 is indeed a wonderful tool.
Post Reply