Help needed to create forms

Post

Posted
Rating:
#1 (In Topic #371)
Regular
bill-lancaster is in the usergroup ‘Regular’
 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
Online now: No Back to the top

Post

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

bill-lancaster said

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

Code (gambas)

  1. Public Sub Form_Close()
  2.  
  3.   FormX.Close
  4.  

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).
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
bill-lancaster is in the usergroup ‘Regular’
 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?
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
bill-lancaster is in the usergroup ‘Regular’
 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!
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.


Code (gambas)

  1. hForm As Form
  2.  
  3. Public Sub Form_Open()
  4.  
  5.   Dim siCount As Short
  6.  
  7.   Application.MainWindow = Me
  8.  
  9.   For siCount = 1 To 10
  10.     With hForm = New Form
  11.       .Name = "xx"
  12.       .X = siCount * 50
  13.       .Y = siCount * 50
  14.       .W = 200
  15.       .H = 200
  16.       .Caption = "new " & Str(siCount)
  17.       .Show
  18.     End With
  19.   Next
  20.  
  21.  
  22.  
  23. Public Sub Button1_Click()
  24.  
  25.  
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
bill-lancaster is in the usergroup ‘Regular’
 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?
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
stevedee is in the usergroup ‘Regular’
Have you tried putting:-

Code (gambas)

  1. Public Sub Form_Close()
  2.  
  3.   For Each cwin In Windows
  4.     cwin.Close
  5.   Next
  6.  

…in your main form?
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Guru
cogier is in the usergroup ‘Guru’
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 :!: )
Online now: No Back to the top

Post

Posted
Rating:
#9
Regular
bill-lancaster is in the usergroup ‘Regular’
 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
Online now: No Back to the top
1 guest and 0 members have just viewed this.