Help needed to create forms

Post your Gambas programming questions here.
Post Reply
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Help needed to create forms

Post by bill-lancaster »

I have a project where I want to display information in one or more forms
With hForm = New Form
.Name = "xx"
.X = 50
.Y = 50
.W = 200
.H = 200
.Caption = "new"
.Show
End With
This code will create a number of new forms but they still exist after the project is closed, how can I close them when the project is closed?
Any help appreciated
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Help needed to create forms

Post by stevedee »

bill-lancaster wrote: Friday 24th January 2020 12:35pm...This code will create a number of new forms but they still exist after the project is closed...
You basically need to reference each form in the main form's close event:-
Public Sub Form_Close()
  
  FormX.Close
  
End
But exactly how you do this may depend upon how you are creating the additional forms (i.e. whether you can simply do this using known form names, or whether you need a more flexible approach).
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: Help needed to create forms

Post by bill-lancaster »

Thanks Stevedee,
You're right, referencing the forms is the question.
If a form call hForm is created (as per my example) then it can be destroyed with 'hForm.Close', but the code can create multiple forms and 'hForm.Close' closes only the first one created since the others won't have the same name.
I imaging the 'handle' property might be useful here but how?
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: Help needed to create forms

Post by bill-lancaster »

One solution is to create an array of forms, not very elegant because the number of forms possible is set at declaration.
It work though!
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Help needed to create forms

Post by cogier »

I think the solution you are looking for is Application.MainWindow. You just need a Form with a button on it called 'Button1' to run this code. Run the code then click the button.

The help is here.

hForm As Form

Public Sub Form_Open()

  Dim siCount As Short

  Application.MainWindow = Me

  For siCount = 1 To 10
    With hForm = New Form
      .Name = "xx"
      .X = siCount * 50
      .Y = siCount * 50
      .W = 200
      .H = 200
      .Caption = "new " & Str(siCount)
      .Show
    End With
  Next

  Me.Raise

End

Public Sub Button1_Click()

  Me.Close

End
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: Help needed to create forms

Post by bill-lancaster »

Thank you cogier, that works nicely, however, I realise that I have a main form from which is opened a second form (Form2). New windows generated from Form2 don't close with hForm.Close. Have tried Application.MainWindow = Me and Application.MainWindow = fMain but the new forms don't close.
Any ideas?
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Help needed to create forms

Post by stevedee »

Have you tried putting:-
Public Sub Form_Close()
Dim cwin As Window

  For Each cwin In Windows
    cwin.Close
  Next 
  
End
...in your main form?
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Help needed to create forms

Post by cogier »

Thank you cogier, that works nicely, however, I realise that I have a main form from which is opened a second form (Form2). New windows generated from Form2 don't close with hForm.Close. Have tried Application.MainWindow = Me and Application.MainWindow = fMain but the new forms don't close.
Any ideas?
Can you provide some code as to how you are doing this please, that is if Steve's answer isn't the solution. (Which it probably is as he's good :!: )
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: Help needed to create forms

Post by bill-lancaster »

Yes, the code does the job well, I actually need to keep Form2 so:-
If cwin.Name = "FDocShow" Then cwin.Close
does the job
Thanks again
Post Reply