By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,243 Members | 1,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,243 IT Pros & Developers. It's quick & easy.

How to check if form is already open - not using mdi forms

P: 40
Current application is built in VB.net 2005
what it does is open other small applications within the form. I'm not using mdi I probably should.

Here is my problem
The application load - main form only contains a menu strip - once selecting a menu option another form opens within the main form.

What I want the app to do is; to check to make sure that the sub form is not already open. If it is bring it to front.
My code..
I created a class - clsOpenForms
this class only holds public booleans for each form they are all set to false

Expand|Select|Wrap|Line Numbers
  1. Public Class clsOpenForms
  2.     Public isfrm_mathopen As Boolean = False
  3. End Class
My main form has the code
[HTML]dim open_frm as new clsOpenfroms[/HTML]
Expand|Select|Wrap|Line Numbers
  1. Private Sub mnu_Math_app(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_Math_app.Click
  2.  
  3. Dim objmath As New frmMath_app
  4. ' If isfrm_mathopen is false then frm is not open - so load form and bring to front
  5.  
  6. if not open_frm.isfrm_mathopen then
  7. objmath.toplevel = false
  8. me.controls.add(objmath)
  9. objmath.show
  10. objmath.bringtofront
  11.  
  12. 'form is now open set isfrm_mathopen to true
  13. open_frm.isfrm_mathopen = true
  14. else
  15. objmath.bringtofront
  16. end sub
Finally my math form has a on closing event that sets isfrm_mathopen to false on closing - so that form can open again once closed.

Expand|Select|Wrap|Line Numbers
  1. dim open_frm as new clsOpenfroms
  2.     Private Sub frmMath_app_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  3. open_frm.mathopen =false
  4.     End Sub
This seems real simple but not working...I know I've done something like this before...Any help will be great...If not...Mdi will have to do...
Aug 29 '08 #1
Share this Question
Share on Google+
2 Replies


Shashi Sadasivan
Expert 100+
P: 1,435
Use My.Application.OpenForms to get a list of all the open forms in the application.

They will be of "Form" object type, so you will need to check if they are of your inherited Form object type like
Expand|Select|Wrap|Line Numbers
  1. if(form is MyMathForm)
Aug 29 '08 #2

P: 40
Issue is resolved this is what I did...Thanks

Expand|Select|Wrap|Line Numbers
  1.         Dim objmath
  2.  As New frmMath_app
  3.  
  4.         getopenforms(frmMath_app)
  5.         If open_forms.isFrm_mathopen = True Then
  6.             objmath.BringToFront()
  7.         Else
  8.             objexitematt.TopLevel = False
  9.             Me.Controls.Add(objmath)
  10.             objmath.Show()
  11.             objmath.BringToFront()
  12.         End If
Expand|Select|Wrap|Line Numbers
  1.  
  2.  Public Function getopenforms(ByVal frm As Form)
  3.         For Each frmCheck As Form In My.Application.OpenForms
  4.             If frmCheck.Name = frm.Name Then
  5.                 open_forms.isFrm_mathopen = True
  6.                 Exit For
  7.             End If
  8.             open_forms.isfrm_mathopen = False
  9.         Next
  10.         Return open_forms
  11.     End Function
And got rid of that closing event...

Thanks for you input.
Aug 29 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.