Is a form already loaded?

  • Thread starter Thread starter HockeyFan
  • Start date Start date
H

HockeyFan

I have a form that might need to load another form. But if that form
is already loaded, I need to know this so that I can tell the form to
reinitialize to a specific set of data. But how do I know if that
form is already up? When I do a Show, there's nothing that tells me
that the form is already there.
 
HockeyFan said:
I have a form that might need to load another form. But if that form
is already loaded, I need to know this so that I can tell the form to
reinitialize to a specific set of data. But how do I know if that
form is already up? When I do a Show, there's nothing that tells me
that the form is already there.


Hi,

The simplest way to get this to store the reference when you load the form.
If the reference exists when you later run the same code, the form is loaded.
Make sure the reference is reset to null when the form closes, which can be
done by subscribing to its closing event.
 
Hi,
I have a form that might need to load another form. But if that form
is already loaded, I need to know this so that I can tell the form to
reinitialize to a specific set of data. But how do I know if that
form is already up? When I do a Show, there's nothing that tells me
that the form is already there.

you can test against the My.Application.OpenForms collection (FW 2.0+).
Here's what I'm using:

''' <summary>
''' Returns whether the passed form is currently open/loaded.
''' </summary>
''' <param name="frm">The form to be tested.</param>
''' <returns>True if the form is open (i.e. contained in the
My.Application.OpenForms collection), False otherwise.</returns>
Public Shared Function IsOpen(ByVal frm As Form) As Boolean
Dim fIsOpen As Boolean = False
For Each frmCheck As Form In My.Application.OpenForms
If ReferenceEquals(frmCheck, frm) Then
fIsOpen = True
Exit For
End If
Next
Return fIsOpen
End Function

Cheers,
Olaf
 
Olaf Rabbachin said:
Hi,


you can test against the My.Application.OpenForms collection (FW 2.0+).
Here's what I'm using:

''' <summary>
''' Returns whether the passed form is currently open/loaded.
''' </summary>
''' <param name="frm">The form to be tested.</param>
''' <returns>True if the form is open (i.e. contained in the
My.Application.OpenForms collection), False otherwise.</returns>
Public Shared Function IsOpen(ByVal frm As Form) As Boolean
Dim fIsOpen As Boolean = False
For Each frmCheck As Form In My.Application.OpenForms
If ReferenceEquals(frmCheck, frm) Then
fIsOpen = True
Exit For
End If
Next
Return fIsOpen
End Function

Since you already have a reference to the Form in this code you could simply
check frm.Visible. If you only have a name, then looping through OpenForms
could make sense, but Control.Name is not guaranteed to be unique.

PS! Application.OpenForms will in some cases not report all open Forms. In
my test project it failed to report any form at all so I wouldn't trust it
blindly.
 
Hi Morten,
Since you already have a reference to the Form in this code you could simply
check frm.Visible. If you only have a name, then looping through OpenForms
could make sense, but Control.Name is not guaranteed to be unique.

well, what if the form is loaded but invisible? :-)
PS! Application.OpenForms will in some cases not report all open Forms. In
my test project it failed to report any form at all so I wouldn't trust it
blindly.

Do you have some code fragment to repro that behaviour? I have been using
that code ever since I installed 2.0 (i.e. VS2005 + VS2008) and never
experienced that problem. I can't actually tell as to how many times the
function is being used though ...

Cheers,
Olaf
 
Olaf Rabbachin said:
well, what if the form is loaded but invisible? :-)

Then it would fail, which is why I would instead keep a reference and
consider any non null form reference as open, and null references as not open.
Do you have some code fragment to repro that behaviour? I

Add a button and attach the click event to reproduce the behaviour.
Openforms only reports 1 open form, both in .Net 2.0 and 3.5 using VS 2005
and VS 2008.


Form2 f2 = new Form2();
public Form1()
{
InitializeComponent();
f2.Show();
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Application.OpenForms.Count.ToString());
}
 
Hi,
Add a button and attach the click event to reproduce the behaviour.
Openforms only reports 1 open form, both in .Net 2.0 and 3.5 using VS 2005
and VS 2008.

everything's working as expected here, both for C# and VB under VS2008
(tested for FW 2.0, 3.0, 3.5). No idea what might be wrong on your end, but
that certainly isn't happening here and nobody ever reported any problem
with that functionality to me.

If anyone wants to test this as well, here's a link to the two VS-2008
solutions that I created (29KB):
http://www.intuidev.de/otherstuff/openforms_test.zip
Both solutions are targetting the 2.0 FW.

Cheers,
Olaf
 
Olaf Rabbachin said:
Hi,


everything's working as expected here, both for C# and VB under VS2008
(tested for FW 2.0, 3.0, 3.5). No idea what might be wrong on your end, but
that certainly isn't happening here and nobody ever reported any problem
with that functionality to me.

If anyone wants to test this as well, here's a link to the two VS-2008
solutions that I created (29KB):
http://www.intuidev.de/otherstuff/openforms_test.zip
Both solutions are targetting the 2.0 FW.

Cheers,
Olaf

Hi,

Upon further investigation it seems like Visual Studio 2005 reports 0 open
forms, whereas Visual Studio 2008 reports 1. This result was consistent on
two different windows xp machines. My Windows Vista machine, however,
reported 2 open forms under vs 2008. Vista and vs 2005 is untested.
 
Hi,
Upon further investigation it seems like Visual Studio 2005 reports 0 open
forms, whereas Visual Studio 2008 reports 1. This result was consistent on
two different windows xp machines. My Windows Vista machine, however,
reported 2 open forms under vs 2008. Vista and vs 2005 is untested.

I have done the same tests for VS2005 and, again, neither C# nor VB report
1 open form, both report 2 here!

Cheers,
Olaf
 
Back
Top