Computing.Net > Forums > Programming > checking if a form is open in vb

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

checking if a form is open in vb

Reply to Message Icon

Original Message
Name: Dale
Date: June 27, 2002 at 07:44:42 Pacific
Subject: checking if a form is open in vb
Comment:

Does anyone know how I can check if a form is open in visual basic?


Report Offensive Message For Removal


Response Number 1
Name: MJ
Date: June 27, 2002 at 10:37:26 Pacific
Reply: (edit)

I think it's like...

If Form1.Enabled = True

or Form1.Open = True

or something...

or you can set a global variable = 1 whenever
the form is open and = 0 whenever it is closed..

and depending on that value, write the code you want to..

like..

if variable = 1 'i.e. if the form is open
....
...
...
elseif variable = 2 'i.e. if the form is closed
....
...
...
endif

someone else here might have a better solution.. I've dont VB about a year ago and i actually did this... but i forgot... sorry


http://mahurshi.tripod.com/mainframes.htm


Report Offensive Follow Up For Removal

Response Number 2
Name: Jeff J
Date: June 27, 2002 at 11:57:19 Pacific
Reply: (edit)

VB5/6 have a feature that makes it unnecessary to load a form [class] before using it. With COM Automation, as much as accessing any function of the form, will automatically load it into memory. That's why we don't have to code:

Load MyForm
MyForm.Show

we just call

MyForm.Show

even if it had never been loaded before. To see this behaviour for yourself, just paste this into any .frm file:

Private Sub Form_Load()
MsgBox Me.Name & " is loading."
End Sub

Private Sub Form_Unload(Cancel As Integer)
MsgBox Me.Name & " is unloading."
End Sub

You can also set a global variable in those events, to keep track of whether the form is currently loaded or not. Checking Enabled or Visible or what not, will only load the form if it isn't already. I had such a nightmare experience once, when I was called on to fix a project with over 10 forms in it; modals and non-modals were mixed, multiple instances, what a job.

Unfortunately, unloading does not always work as expected, because if any COM object still holds a reference to anything in the form, it will not be unloaded. This sort of thing is a vexing problem with all automatic-object languages, especially when they pretend pointers don't exist (you forfeit control). That's because it's extremely difficult for the language to know when you no longer intend to keep working with an object. Whether reference counting, garbage collection, or whatever means is employed, it's always a calculated compromise. Best is to know when to manually destroy objects, but most programmers find it difficult to do. Languages like VB, Python, Java, C#, and others, attempt to do this for us, but it makes dealing with these things at any serious level difficult and very confusing. Enough of my sermon :)


Report Offensive Follow Up For Removal

Response Number 3
Name: Michael Jackson
Date: June 27, 2002 at 17:22:34 Pacific
Reply: (edit)

yeah,... so it is

If MyForm.Show = True
(i.e. the form is opened now)

I forgot it.. hmm ... Form1.Show

I used it once in my Numerology program

http://mahurshi.tripod.com/mainframes.htm


Report Offensive Follow Up For Removal

Response Number 4
Name: Burbble
Date: June 30, 2002 at 07:19:16 Pacific
Reply: (edit)

.Show is not a property, it's a method. Therefore if you tried using

If MyForm.Show = True Then
etc...
End If

It would not work.

To check if a form is open, I think the best way would be to set a Public Boolean variable (in a module) to True when the form loads, and to False when it unloads. Then, any form could read the variable without loading the form again.

-Burbble


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you own an iPhone?

Yes
No, but soon
No


View Results

Poll Finishes In 7 Days.
Discuss in The Lounge
Poll History




Data Recovery Software