How can I embed a form in a Panel?

Post

Posted
Rating:
#1 (In Topic #1376)
Avatar
Regular
mandarin is in the usergroup ‘Regular’
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) :


Code (gambas)

  1. Public Sub FMain()
  2.   Application.MainWindow = Window
  3.   Panel1.Enabled = True
  4.   FMain.Children = Panel1
  5.   Form1.Parent = FMain.Panel1
  6.   Form1.Move(0, 0)
  7.  
  8. Public Sub Menu1_Click()
  9.   Form1.Show()
  10.  

…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!

Image

(Click to enlarge)


Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
thatbruce is in the usergroup ‘Regular’

mandarin said


I coded thus (with Form1 already designed) :


Code (gambas)

  1. Public Sub FMain()
  2.   Application.MainWindow = Window
  3.   Panel1.Enabled = True
  4.   FMain.Children = Panel1
  5.   Form1.Parent = FMain.Panel1
  6.   Form1.Move(0, 0)
  7.  
  8. Public Sub Menu1_Click()
  9.   Form1.Show()
  10.  


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

Code (gambas)

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

Try this but first try to understand it from the above comments.
In FMain code:

Code (gambas)

  1. Public Sub Form_Open()
  2. Dim subform as new Form1(Panel1)
  3. Application.MainWindow=Me
  4.  

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

hth
thatbruce

Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
mandarin is in the usergroup ‘Regular’
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) :

Code (gambas)

  1. Public Sub Form_Open()
  2.   Application.MainWindow = Me
  3.   Dim subform As New Form1(Panel1)
  4.   subform.Visible = False
  5.  
  6. Public Sub Menu1_Click()
  7.   Dim subform As New Form1(Panel1)
  8.   subform.Visible = True
  9.   subform.Show

(On my previous try, Form1 sat correctly on (0, 0); but the dimensions of Panel1 were slightly bigger, due to my fault.)

Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
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)

Code (gambas)

  1.  
  2. Private $hFMain As FMain
  3.  
  4. Public Sub EmbedForm()
  5.  
  6.   $hFMain = New FMain(Panel1) As "FMain"
  7.   $hFMain.Show
  8.  
  9.  
  10.  
Now reference the embeded form using $hFMain


Method 2 , Directly add FMain

Code (gambas)

  1.  
  2. FMain.Load(Panel1)
  3. FMain.Show
  4.  
  5.  
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 :)
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’

mandarin said

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) :

Code (gambas)

  1. Public Sub Form_Open()
  2.   Application.MainWindow = Me
  3.   Dim subform As New Form1(Panel1)
  4.   subform.Visible = False
  5.  
  6. Public Sub Menu1_Click()
  7.   Dim subform As New Form1(Panel1)
  8.   subform.Visible = True
  9.   subform.Show

(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..

Code (gambas)

  1. Public Sub Form_Open()
  2.   Application.MainWindow = Me
  3.   Form1.Load(Panel1)
  4.   Form1.Visible = False
  5.  
  6. Public Sub Menu1_Click()
  7.   Form1.Visible = Not Form1.Visible  ' toggle visibility
  8.  
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’

mandarin said


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…
/comp/gb.qt4 - Gambas Documentation
/comp/gb.form - Gambas Documentation

/lang - Gambas Documentation
/cat - Gambas Documentation
/howto - Gambas Documentation

Also check out the Application repository /app - Gambas Documentation
And the Farm (select farm from gambas welcome screen)
Examining other peoples code can be a great learning tool :)
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
mandarin is in the usergroup ‘Regular’
 Thanks a lot, gentlemen!

Gambas3 is indeed a wonderful tool.

Online now: No Back to the top
1 guest and 0 members have just viewed this.