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
Help needed to create forms
-
- Posts: 190
- Joined: Tuesday 26th September 2017 3:17pm
- Location: NW England
Re: Help needed to create forms
You basically need to reference each form in the main form's close event:-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...
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).-
- Posts: 190
- Joined: Tuesday 26th September 2017 3:17pm
- Location: NW England
Re: Help needed to create forms
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?
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?
-
- Posts: 190
- Joined: Tuesday 26th September 2017 3:17pm
- Location: NW England
Re: Help needed to create forms
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!
It work though!
- cogier
- Site Admin
- Posts: 1118
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Help needed to create forms
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.
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
-
- Posts: 190
- Joined: Tuesday 26th September 2017 3:17pm
- Location: NW England
Re: Help needed to create forms
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?
Any ideas?
Re: Help needed to create forms
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?- cogier
- Site Admin
- Posts: 1118
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Help needed to create forms
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 )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?
-
- Posts: 190
- Joined: Tuesday 26th September 2017 3:17pm
- Location: NW England
Re: Help needed to create forms
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
If cwin.Name = "FDocShow" Then cwin.Close
does the job
Thanks again