Results 1 to 26 of 26

Thread: [RESOLVED] Check if form has been loaded already

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [RESOLVED] Check if form has been loaded already

    My application starts with Form1 and then at some point the user can click a button to load Form2 and do some job. When done, Form2 is hidden, not unloaded.

    I want to test at a specific place in Form1 whether Form2 has already been loaded (and hidden) or not.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Check if form has been loaded already

    If you only have 2 forms, check the Forms.Count property. If that returns 2, then check Form2.Visible. But don't check .Visible first else you'll load the form if it wasn't already loaded.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: Check if form has been loaded already

    Try this;

    Code:
    Private Sub Command1_Click()
        
        Form2.Show
            
    End Sub
    
    Function IsForm2Loaded(Visible As Boolean) As Boolean
    
        Dim Fm As Form
        
        For Each Fm In Forms()
            If Fm.Name = "Form2" Then
                IsForm2Loaded = True
                If Fm.Visible Then
                    Visible = True
                End If
                Exit Function
            End If
        Next
    
    End Function
    
    Private Sub Command2_Click()
            
        Dim Visible As Boolean
            
        If IsForm2Loaded(Visible) Then
            MsgBox "Form2 is Loaded and " & IIf(Visible, "Visible", "Hidden")
        Else
            MsgBox "Form2 is not Loaded"
        End If
    
    End Sub

  4. #4

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Check if form has been loaded already

    Quote Originally Posted by LaVolpe View Post
    If you only have 2 forms, check the Forms.Count property. If that returns 2, then check Form2.Visible. But don't check .Visible first else you'll load the form if it wasn't already loaded.
    Indeed, there are only 2 forms. Form2 is loaded in modal mode and I don't need to check for visibility.

    Thanks.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  5. #5

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Check if form has been loaded already

    It works fine. Thank you!
    Quote Originally Posted by Magic Ink View Post
    Try this;...
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  6. #6

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Check if form has been loaded already

    Quote Originally Posted by Me View Post
    It works fine. Thank you!
    As a matter of fact I answered too fast and overlooked the fact you're not passing the forms's name as parameter. In the example I proposed it was Form2 but in general I'd like to check for any specific form.

    The problem I ran into with other code I found in the forums is the form gets automatically loaded the moment its name is encountered.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  7. #7
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: Check if form has been loaded already

    >you're not passing the forms's name as parameter

    I'll leave that to you ...

  8. #8

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Check if form has been loaded already

    Quote Originally Posted by Magic Ink View Post
    >you're not passing the forms's name as parameter

    I'll leave that to you ...
    All right so I'll keep asking for help.

    Is there any way then to retrieve and assign all the project's form names to string variables without having them load at the same time?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Check if form has been loaded already

    Is there any way then to retrieve and assign all the project's form names to string variables without having them load at the same time?
    not dynamically, that can only be done with forms that are already loaded
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Check if form has been loaded already

    You could read them from your vbp file then save them to a txt file that would be included with the program and read them from there into your variables at launch.

  11. #11
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Check if form has been loaded already

    Krtx

    Picking up on DataMiser's idea, as an alternative, you
    could read the .vbw file .. it is even shorter.

    Spoo

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Check if form has been loaded already

    The only problem with reading from the vbw is that you may not be able to tell forms from modules from classes depending on your naming conventions.

    The vbp file shows you which are forms making it very easy to grap them all with a few lines of code.

    Example vbw
    Code:
    frmLogin = 44, 44, 579, 488, , 22, 22, 557, 466, C
    mdiMain = -139, 85, 396, 529, , 44, 44, 579, 488, C
    modLOtimer = 0, 0, 535, 444,
    Example vbp
    Code:
    Form=frmLogin.frm
    Form=mdiMain.frm
    Module=modLOtimer; modLOtimer.bas

  13. #13
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Check if form has been loaded already

    I used to write programs with several forms. I generally tracked them being loaded by establishing a global boolean array variable. During the Form Load event, I set it to True. Then I set it to False when the form was unloaded. That worked for me, but I often wished there was a simpler procedure.
    Doctor Ed

  14. #14
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Check if form has been loaded already

    DataMiser

    You are absolutely correct .. the unspoken implication
    of my vbw suggestion was the very naming convention
    issue that you mention.

    Clearly, the vbp's Form= prefix makes it a snap, even
    if there are a few more lines in that file to read.

    BTW, do you have any idea what the parameters in the
    vbw file refer to?

    Spoo

  15. #15
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Check if form has been loaded already

    Doc

    Your "brute force" approach may be the easiest and hence
    the most elegant afterall.

    Spoo

  16. #16
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: Check if form has been loaded already

    I'm with Code Doc on this one.
    I also use an array to track which forms are loaded.

  17. #17
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Check if form has been loaded already

    Quote Originally Posted by Spoo View Post

    BTW, do you have any idea what the parameters in the
    vbw file refer to?

    Spoo
    No idea at all, I was wondering the same thing.

    btw as to the topic at hand there is a very easy way to find out which forms are currently loaded.
    Code:
    Dim f As Form
    For Each f In Forms
        Debug.Print f.Name
    Next

  18. #18

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Check if form has been loaded already

    Both parsing the vbp file at program start and CodeDoc's suggestion seem reasonable and easy enough. Offhand I don't see which is more convenient, maybe I'll implement both and see which one I'm happier with.

    Thanks to all of you folks.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  19. #19

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Check if form has been loaded already

    Quote Originally Posted by DataMiser View Post
    ...
    Code:
    Dim f As Form
    For Each f In Forms
        Debug.Print f.Name
    Next
    Doesn't that load the forms?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  20. #20

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Check if form has been loaded already

    Quote Originally Posted by DataMiser View Post
    You could read them from your vbp file then save them to a txt file that would be included with the program and read them from there into your variables at launch.
    I suppose there's no need to save them to a text file. After they've been read from the vbp file you simply store them in an array.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  21. #21
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: [RESOLVED] Check if form has been loaded already

    >Doesn't that load the forms?

    It does not because the Forms Collection only includes the Loaded Forms.

  22. #22

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Check if form has been loaded already

    Quote Originally Posted by Magic Ink View Post
    >Doesn't that load the forms?

    It does not because the Forms Collection only includes the Loaded Forms.
    I see...

    So, working on your idea I finally have used this code:
    VB Code:
    1. 'Array of currently loaded forms
    2. Dim ldForms() As String
    3. Private Sub Form_Click()
    4.     Dim i As Integer
    5.     Dim f As Form
    6.    
    7.     'Assign names of loaded forms to array
    8.     i = 0
    9.     For Each f In Forms
    10.         ReDim Preserve ldForms(i)
    11.         ldForms(i) = f.Name
    12.         i = i + 1
    13.     Next
    14.    
    15.     'Check the code
    16.     For i = 0 To UBound(ldForms)
    17.         Debug.Print ldForms(i)
    18.     Next
    19.    
    20. End Sub

    Also, I have implemented the vbp file parsing option which provides all the projet's form names like this:
    VB Code:
    1. Private Sub GetFormsNames()
    2.     Dim ff As Integer, i As Integer
    3.     Dim s As String
    4.    
    5.     i = 0
    6.     ff = FreeFile
    7.     Open App.Path & "\" & App.Title & ".vbp" For Input As #ff
    8.         Do
    9.             Line Input #ff, s
    10.             If Left(s, 5) = "Form=" Then
    11.                 ReDim Preserve pfNames(i)
    12.                 pfNames(i) = Mid(s, 6)
    13.                 i = i + 1
    14.             End If
    15.         Loop While Not EOF(ff)
    16.     Close #ff
    17.    
    18. End Sub
    where pfNames has been declared elsewhere (possibly as public in a module):
    Public pfNames() As String

    and then you'd have to check the array to find out which forms are loaded.

    Summing up, it's obvious that your solution is by far the easiest and most convenient, (hats off to you)
    Last edited by krtxmrtz; Feb 21st, 2012 at 05:40 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  23. #23
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Check if form has been loaded already

    Quote Originally Posted by krtxmrtz View Post
    I suppose there's no need to save them to a text file. After they've been read from the vbp file you simply store them in an array.
    The reason I suggested saving them to a text file is that you could include them with the program when you ship it. Of course if the program is only going to be ran where the source code is located then the vbp would be fine but if it is to be distributed most people would not want to include any of the source files.

  24. #24

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Check if form has been loaded already

    Quote Originally Posted by DataMiser View Post
    The reason I suggested saving them to a text file is that you could include them with the program when you ship it. Of course if the program is only going to be ran where the source code is located then the vbp would be fine but if it is to be distributed most people would not want to include any of the source files.
    You're quite right, I didn't think of that.
    So before distributing you could even create and edit the text file manually -unless the number of forms is so large that makes it worth parsing the vbp file.
    Last edited by krtxmrtz; Feb 21st, 2012 at 11:35 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  25. #25
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: [RESOLVED] Check if form has been loaded already

    Pehaps I'm missing something but just what is the point of including the vbp file or a text file including the Form names within it. Surely if all the Form names are known at design time then you can hard code them and built an array/ list if you must; there is little point complicating things by importing static data from an external file.

  26. #26
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] Check if form has been loaded already

    It would depend on the project and the number of forms. You could hard code them into the project by hand and go back and make changes as needed. You could write a little utility that would create the text file at any time with only a few lines of code. Then add a function to your program that could read the file. You could create a little utility that would create the code for hard coding at the click of a button then paste that into your project.

    IMO hard coding by hand would make sense only if it were a small project and you do not plan to do this with other projects in the future, otherwise it makes sense to do it with a few lines of code that you can use over and over as needed.

    Maybe I'm just lazy but that is what I would do

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width