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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wait for Form to Unload

Status
Not open for further replies.

Jen53403

Programmer
Jul 17, 2006
22
Hi, I'm just learning VB, and I'm coding an Excel add-in using VSTO. I'm trying to show a form, wait for the user to click OK on the form so it unloads, then continue execution from the function that called the form. I did some searching and found the following code which is supposed to accomplish that, but Visual Studio gives me the error that Forms and DoEvents() are not declared. Is there a namespace I'm missing for these? Or is there another solution to my problem? I'm using VB version 2005. Thanks!!

Code:
    Public Sub WaitOnFormUnload(ByRef formName As String)
        Dim bIsLoaded As Boolean
        Dim I As Long
        bIsLoaded = True
        Do While bIsLoaded = True
            bIsLoaded = False
            For I = 0 To Forms.Count - 1
                If Forms(I).Name = formName Then
                    bIsLoaded = True
                    DoEvents()
                    Exit For ' breaks the for loop
                End If
            Next I
        Loop
    End Sub
 
For the DoEvents() method, prefix it with "Application.". I believe the namespace for Forms is System.Windows.
 
To loop through the open forms, do this:

For Each frm As Form In Application.OpenForms
If frm.Name = formName Then
bIsLoaded = True
DoEvents()
Exit For ' breaks the for loop
End If
Next

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
The Application namespace doesn't appear to have a DoEvents() method. System.Windows.Forms doesn't have a Count property or anything that looks like a count. Application doesn't have a Forms or OpenForms in it. I read something about a forms collection, but I can't find that using Intellisense either.

Is there another way to check whether a specific form has closed? Can the form raise an event or change a variable when it closes that another process can detect?

I'd like to try a simple implementation like this, but I don't know where to find the sleep() method.

Code:
Do While usr_but_frm_closed = False
' sleep for a second
Loop
 
Try typing the calls out:

System.Windows.Forms.Application.DoEvents

System.Windows.Forms.Application.OpenForms





I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Jebenson, your suggestions worked, but now I have an even worse problem... Excel now fails to load my toolbar containing this code! I got some error message; I can't remember what it said and can't reproduce the error, but I think it was something about a process not being able to disconnect, and did I want to stop those processes or not. I clicked yes, and Excel loaded fine but missing my toolbar. Next time I clicked no, and now I receive no error messages, my toolbar doesn't show up although it's listed when I go to Tools > Addins, and a different working version of the toolbar which I previously installed does not show up either. I assume Excel is blocking it because it caused fatal errors in the past. How can I reverse that?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top