Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding unloaded Forms 1

Status
Not open for further replies.

pmcdaniel

Programmer
Feb 9, 2007
127
US
When I close my MDI application I save all forms which are showing so I can have them show again the next time the application is opened.

Code:
varUtilitySettings = GetAllSettings(appname:="My Utility", Section:="Open Forms")

  For bytCounter = LBound(varUtilitySettings, 1) To UBound(varUtilitySettings, 1)
 varUtilitySettings(bytCounter, 1)
      For Each objForm In Forms
        If objForm.Name = varUtilitySettings(bytCounter, 1) Then
          objForm.Show
          Exit For
        End If
      Next 'objForm
  Next 'bytCounter

This, however, does not work because the forms are not loaded and it appears I can only use the above "For Each" loop on loaded forms. Is there a way for me to find all forms in my VB project whether already loaded or not without hard-coding each form name?

thanks
 
No, I don't think so, but if the point of your code is to restore the MDI child windows that were open when the app closed, why do you need to know the names of the forms that weren't open?

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Just for grins :)

Code:
Private Sub Command1_Click()

    Dim FSO, FileName, TextStream
    Dim strText As String

    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    
    FSO.CopyFile "C:\@@Testprojects\FSO TextReadLoop\project1.vbp", "C:\Text.txt"
    
    Set FileName = FSO.GetFile("C:\Text.txt")
    
    Set TextStream = FileName.OpenAsTextStream(ForReading, _
          TristateUseDefault)
          
    Do While Not TextStream.AtEndOfStream
        strText = TextStream.ReadLine
        
        If Mid(strText, 1, 5) = "form=" Then
            Debug.Print Mid(strText, 6, 40)
        End If
        
    Loop
    
    TextStream.Close
    Set TextStream = Nothing
    
    FSO.DeleteFile ("C:\Test.txt")

End Sub
 
Oops…I hate when people give code snippants without the references. Use Microsoft Scripting Runtime. Also I use Option Compare Text all the time. If you don’t capitalize “Forms=”. I took out the hardcoded app name.

Code:
Option Explicit
Option Compare Text

Private Sub Command1_Click()

    Dim FSO, FileName, TextStream
    Dim strText As String
    Dim strAppPath As String
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    strAppPath = App.Path & "\" & App.EXEName & ".vbp"
    
    If FSO.FileExists("C:\Text.txt") Then
        FSO.DeleteFile ("C:\Text.txt")
    End If
    
    FSO.CopyFile strAppPath, "C:\Test.txt"

    Set FileName = FSO.GetFile("C:\Test.txt")
    
    Set TextStream = FileName.OpenAsTextStream(ForReading, _
          TristateUseDefault)
          
    Do While Not TextStream.AtEndOfStream
        strText = TextStream.ReadLine
        If Mid(strText, 1, 5) = "form=" Then
            Debug.Print Mid(strText, 6, 40)
        End If
    Loop
    
    TextStream.Close
    Set TextStream = Nothing
    
    FSO.DeleteFile ("C:\Test.txt")
    FSO.CreateTextFile ("C:\Test.txt")

End Sub

 
Thanks Tyson but I want to show the forms the next time the application starts. It appears your code just provides the names.
 
That's true. I thought that's you were asking…how to get the name of all forms not just active ones. I thought you were going to take it from there. Let me think about it.
 
I don't know why you want to do it but this seems to work with a quick test :)

Make sure you exclude your start up form.

Code:
Dim frm As Form
    Dim strFormName As String
    Do While Not TextStream.AtEndOfStream
        strText = TextStream.ReadLine
        If Mid(strText, 1, 5) = "form=" And Trim(Mid(strText, 6, 35)) <> "form1.frm" Then
            strFormName = Replace(Trim(Mid(strText, 6, 35)), ".frm", "")
            Set frm = Forms.Add(strFormName)
            frm.Show
        End If
    Loop
 
This will only work in the IDE, not if you package and deploy an application
 
I just created an exe and it DOES work. I have the proper error routines included since the values won't exist in the registry the first time the app is run.

Here's a snippet of the code:
Code:
Dim varUtilitySettings As Variant
Dim bytCounter As Byte
Dim objForm As Form

  varUtilitySettings = GetAllSettings(appname:="My Utility", Section:="Open Forms")

  For bytCounter = LBound(varUtilitySettings, 1) To UBound(varUtilitySettings, 1)
    Set objForm = Forms.Add(varUtilitySettings(bytCounter, 1))
    objForm.Show
  Next 'objForm
  
  Set objForm = Nothing

If a deployed version won't work wouldn't the exe I created fail to work?
 
I’m not sure what you mean strongm…I also made an exe out of it and it worked.
 
This will only work in the IDE, not if you package and deploy an application

I just created an exe and it DOES work.

Did Strongm just get pwned or whatever the kids are saying these days. Hehe, probably not, but I'm just having fun :)

Tom
 
Oh…the vbp will need to be in the App.Path & "\" & App.EXEName folder or somewhere else to be found to read.
 
>Did Strongm just get pwned

Nope

>I just created an exe and it DOES work. I have the proper error routines included since the values won't exist in the registry the first time the app is run.

Creating an EXE is not the same thing as packaging and deploying the application. Once you package abnd deploy to a different machine you will not have access to the project file (.vbp) that Tyson's technique relies on to get all the form names. So it doesn't matter how much error checking you have, the registry will never get filled with the correct values.

> I also made an exe out of it and it worked

See above
 
Strongm? Just out of pure respect for your wisdom I am curious as to insight as I always learn something.

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
>Oh...the vbp will need to be

I see Tyson has figured out what I was alluding to
 
Strongm I was truly just poking fun, much respect. :)

Tom
 
Funny thing Strongm, I skipped right past that and mentally noted that would need to be done but yet did not think to comment. No wonder you are this forum's most illustrious member!

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
I am not much of a programmer but it appears that an array of binaries vars i.e. FormOpen() could be saved and as forms were opened or closed the variables could be switched on/off appropriately. The querry Unload for each form could be used to manage the closed ones. An inspection at querry unload of MDI form could save the open numbers to a file. This file could be examined at program open to grab the ones that were not closed formally last run.
Will that approach work?
 
>Once you package abnd deploy to a different machine you will not have access to the project file (.vbp) that Tyson's technique relies on to get all the form names.

>the vbp will need to be in the App.Path & "\" & App.EXEName folder or somewhere else to be found to read.

Keep in mind that if you ship the .vbp files you also ship the source code, which is not generally thought of as a good idea. Therefore, Tyson's solution devolves into the idea of reading the form names from a text file that is shipped with the application. As a general rule, it's better to use a resource file for this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top