Results 1 to 8 of 8

Thread: classes & forms

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2001
    Location
    Netherlands
    Posts
    25

    classes & forms

    What is the best way to handle forms with classes?

    I want to know/check (in another class) if a form is terminated.
    When I use:

    dim myfrm as new frm

    sub showme
    if not myfrm is nothing then frm.show vbmodeless -> is this the way??
    end sub

    in a class module.

    Do I have to work with events then or must I define myfrm
    global in a module.

    When I use events:
    dim withevents myfrm as new frm

    sub showme
    if not myfrm is nothing then frm.show vbmodeless
    end sub

    Private Sub myfrm_NotifyTerminate()
    ??? set myfrm = nothing -> will cause an internall error because it hasn't handled the sub terminate in the form
    end sub

  2. #2
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    If not myfrm is nothing then
    'it isn't terminated
    else
    'it is terminated
    end if

    Be sure in the class terminate event to set the form to nothing if it isn't terminated yet.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  3. #3
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    VB Code:
    1. Dim MyForm   As Form1
    2.    Set MyForm = New Form1
    3.    Load MyForm
    4.    MyForm.Show
    5.    Set MyForm = Nothing
    6.    If MyForm Is Nothing then
    7.       'This is the problem, since MyForm is nothing, but it's still visible and loaded into memory :D
    8.       MsgBox "Woooooooooooooooooooooooof"
    9.    End If

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2001
    Location
    Netherlands
    Posts
    25
    Okay, again

    1) in the class I show/load the form
    2) the user terminates the form
    3)later on, the sub showme is called again (by me), so this somehow has to determine if myfrm is/or isn't nothing.

    A terminate will cause an unload/visible=false but will not set the object myfrm = nothing
    how to accomplish this?

  5. #5
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Gotcha

    In class module
    VB Code:
    1. Private MyForm   As Form1
    2.  
    3. Public Sub Show()
    4.    If MyForm Is Nothing Then
    5.       Set MyForm = New Form1
    6.    End If
    7.    MyForm.Show
    8. End Sub
    9.  
    10. Private Sub Class_Terminate()
    11.    Set MyForm = Nothing
    12. End Sub
    In Form1
    VB Code:
    1. Private Sub Form_Unload(Cancel As Integer)
    2.    Cancel = True
    3.    Me.Hide
    4. End Sub
    The problem here is if you destroy the class before the user closes the form, as this would just hide the form and it would stay like that until u turned your PC off You would have to add the following in the class terminate event:
    VB Code:
    1. Private Sub Class_Terminate()
    2.    If Not MyForm Is Nothing then
    3.       UnLoad MyForm
    4.       Set MyForm = Nothing
    5.    End If
    6. End Sub
    Does that make sense?

    Is that what you wanted?

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2001
    Location
    Netherlands
    Posts
    25
    Thanks Wokawidget, yes this makes sence.

    This is what I wanted to do.
    (okay you'll hide instead of setting myform = nothing)

    But now my problem is that I'm working (only in this case) with VBA and not VB.

    When I hide the object, I can't make it visible again.
    (can't acces the properties) .visible=true

    Any cluess?

  7. #7

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Aug 2001
    Location
    Netherlands
    Posts
    25
    Thanks again Wokawidget,

    I was using the wrong method-event:

    Private Sub UserForm_Terminate
    Me.Hide
    End Sub

    But this unloads the object when invoked.

    Prior to this UserForm_QueryClose runs (equals Form_unload)
    in form:

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Cancel = True
    Me.Hide
    End Sub

    Now the Class.sub.showme works fine.


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