Solved

VB.net Find out if Hidden Form is Open

Posted on 2014-02-21
6
2,435 Views
Last Modified: 2014-02-21
Hi,

How do I find out if a form that is set to "visible = false" is open?

I have tried the following code but it does not work.

Thanks, Greg

Public Function IsFormOpen(ByVal frm As Form) As Boolean
        If Application.OpenForms.OfType(Of Form).Contains(frm) Then
            Return True
        Else
            Return False
        End If
    End Function


Private Sub btnFind_Click(sender As System.Object, e As System.EventArgs) Handles btnFind.Click
        If IsFormOpen(frm_Printers_SearchResult) Then
            MsgBox("open")
        End If
    End Sub

Open in new window

0
Comment
Question by:spen_lang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 39876397
Try with bellow function code;

Public Shared Function isFormOpen(ByVal formName As String) As Boolean
    Dim frm As Form
    Dim i As Integer = 0
    Dim found As Boolean = False
 
    While i < Application.OpenForms.Count
        frm = Application.OpenForms.Item(i)
 
        If frm.Name.ToLower = formName.ToLower Then
            found = True
            Exit While
        End If
 
        i += 1
    End While
 
    Return found
End Function

Open in new window

0
 

Author Comment

by:spen_lang
ID: 39876550
The function returns TRUE if the form is visible but returns FALSE if the forms ".visible = false"
0
 
LVL 12

Accepted Solution

by:
ktaczala earned 500 total points
ID: 39876847
Just set a public variable and check for true or false

For example in form1:

dim frm2 as form2
form2visible = true
frm2.open()

and in frm2 close event
form1.form2visible = false
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39876905
To what end?...possibly so you don't create another instance when one is already open?

If yes, then don't use the "new" keyword.  Instead, only access the "default instance" of the form, using only the name of it like this:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        frm_Printers_SearchResult.Show()
    End Sub

Open in new window


If you don't like default instances, then follow the singleton pattern.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39876911
Change this

Public Function IsFormOpen(ByVal frm As Form) As Boolean
        If Application.OpenForms.OfType(Of Form).Contains(frm) Then
            Return True
        Else
            Return False
        End If
    End Function

Open in new window



to following

Public Function IsFormOpen(ByVal frm As Type) As Boolean
     For Each openfrm As Form In My.Application.OpenForms
           If TypeOf(openfrm) Is frm Then
               Return True
               Exit For
               Exit Function
     Next
     Return False
End Function

Open in new window



and then call it like

IsFormOpen(GetType(Form1))
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39876913
Ah too late.
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question