Results 1 to 9 of 9

Thread: view controls parent form in Modal called

  1. #1

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Question view controls parent form in Modal called

    Hi

    Are some way to know property some controls of the parent form when the child is called like modal ?

    Example I have 5 forms , Two may be parent others 3 child
    form1 = parent
    form2= parent
    form3,form4, form5 ==> child

    Code:
      ' inside form1
        form3.show vbmodal
        'inside form3 a need to know text of textbox example of Form1(or other control)

    Code:
      ' inside form1
        form4.show vbmodal
        'inside form4 a need to know text of textbox example of Form1 (or other control)
    Code:
      ' inside form2
        form4.show vbmodal
        'inside form4 a need to know text of textbox example of Form2 (or other control)
    How can I to know what Form (parent) is calling

    Tia

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: view controls parent form in Modal called

    Hmmm, I'm not sure I see any problem. Maybe you could show us some sample code that won't work.

    For instance, let's take your first scenario (Form1 and Form3). And let's assume that Form1 has a Text1 textbox on it. From within Form3, all we'd need to do is something like: MsgBox Form1.Text1.Text

    Code:
    
    ' inside form1
    Form3.Show vbModal
    
    
    
    'inside form3 a need to know text of textbox example of Form1(or other control)
    MsgBox Form1.Text1.Text
    
    

    And that's independent of whether Form3 is shown modal or not. That's just the way you read properties of controls on another form.

    So, if you could show us the problem you're having, we could possibly help more.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Re: view controls parent form in Modal called

    Quote Originally Posted by Elroy View Post
    Hmmm, I'm not sure I see any problem. Maybe you could show us some sample code that won't work.

    For instance, let's take your first scenario (Form1 and Form3). And let's assume that Form1 has a Text1 textbox on it. From within Form3, all we'd need to do is something like: MsgBox Form1.Text1.Text

    Code:
    
    ' inside form1
    Form3.Show vbModal
    
    
    
    'inside form3 a need to know text of textbox example of Form1(or other control)
    MsgBox Form1.Text1.Text
    
    

    And that's independent of whether Form3 is shown modal or not. That's just the way you read properties of controls on another form.

    So, if you could show us the problem you're having, we could possibly help more.

    Good Luck,
    Elroy
    Thank, but how can I to know what form is parent, example may be
    Code:
      ' inside form1
    Form3.Show vbModal
    
    'or  inside form2 
    Form3.Show vbModal

  4. #4

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Re: view controls parent form in Modal called

    Quote Originally Posted by Elroy View Post
    Hmmm, I'm not sure I see any problem. Maybe you could show us some sample code that won't work.

    For instance, let's take your first scenario (Form1 and Form3). And let's assume that Form1 has a Text1 textbox on it. From within Form3, all we'd need to do is something like: MsgBox Form1.Text1.Text

    Code:
    
    ' inside form1
    Form3.Show vbModal
    
    
    
    'inside form3 a need to know text of textbox example of Form1(or other control)
    MsgBox Form1.Text1.Text
    
    

    And that's independent of whether Form3 is shown modal or not. That's just the way you read properties of controls on another form.

    So, if you could show us the problem you're having, we could possibly help more.

    Good Luck,
    Elroy
    Thank, but how can I to know what form is parent, example may be
    Code:
      ' inside form1
    Form3.Show vbModal
    
    'or  inside form2 
    Form3.Show vbModal

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: view controls parent form in Modal called

    Put a property in each Child-Form
    UNTESTED!

    Code:
    'in form3 or 4 or 5
    Public MyParent As Form
    
    'in form1 or form2
    Load form3
    Set form3.MyParent=Me
    form3.Show vbModal
    
    'in form3 again
    Debug.Print Me.MyParent.Textbox1.Text
    Last edited by Zvoni; Dec 3rd, 2018 at 09:17 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: view controls parent form in Modal called

    Or, you can just put something like the following in the modal form:

    Code:
    
    Option Explicit
    '
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    '
    
    Private Sub Form_Click()
        Dim frmMyOwner As Form
        '
        Set frmMyOwner = GetOwner(Me.hwnd)
        If Not frmMyOwner Is Nothing Then
            MsgBox frmMyOwner.Caption
        End If
    End Sub
    
    Private Function GetOwner(hTheHwnd As Long) As Form
        Const GW_OWNER  As Long = 4&
        Dim frm         As Form
        Dim hOwner      As Long
        '
        hOwner = GetWindow(hTheHwnd, GW_OWNER)
        For Each frm In Forms
            If frm.hwnd = hOwner Then
                Set GetOwner = frm
                Exit For
            End If
        Next
    End Function
    
    

    And, to test, I just put this in the form that loaded the modal form:

    Code:
    
    Option Explicit
    
    Private Sub Form_Click()
        Form2.Show vbModal, Me
    End Sub
    
    

    Enjoy,
    Elroy

    EDIT1: And just an FYI, the "Me" form is the default parent when none is specified (when showing modally), so you really don't even need to say that when showing your modal form.

    EDIT2: Just as another FYI, that GetOwner function could easily be pulled out and place into some general purpose BAS module (along with the GetWindow API). That way, it could be used anywhere in your program without code duplication.
    Last edited by Elroy; Dec 3rd, 2018 at 09:50 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    6,167

    Re: view controls parent form in Modal called

    @Elroy: This is not working if current and owner form are from different projects as Forms collection is singleton per project.

    Best would be to encapsulate the whole interaction in a frShow method like this
    thinBasic Code:
    1. ' Form2
    2. Option Explicit
    3.  
    4. Private m_oOwnerForm As VB.Form
    5.  
    6. Friend Function frShow(oOwnerForm As VB.Form) As Boolean
    7.     Set m_oOwnerForm = oOwnerForm
    8.     Me.Show vbModal, m_oOwnerForm
    9.     Set m_oOwnerForm = Nothing
    10. End Function
    11.  
    12. Private Sub Form_Load()
    13.     m_oOwnerForm.Caption = "Clicked at " & Timer
    14. End Sub
    . . . and use it like this
    thinBasic Code:
    1. ' Form1
    2. Option Explicit
    3.  
    4. Private Sub Form_Click()
    5.     With New Form2
    6.         If .frShow(Me) Then
    7.             MsgBox "Form2 confirmed!", vbExclamation
    8.         End If
    9.     End With
    10. End Sub
    cheers,
    </wqw>

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: view controls parent form in Modal called

    @Wqweto: Truth be told, what you've suggested is precisely how I do it. I show very few forms modally. However, when I do, I always create a "ShowModal" procedure, and call that to actually show the form. The fact that I call a procedure allows me to pass in whatever arguments (possibly including an owner) when the form is shown.

    (And, I didn't always do it that way. But splashing around in these forums convinced me that that was a good way to do it. It's just a bit tricky because I use the Auto-Instantiation names of the forms, and also always un-instantiate the forms' COM objects when unloading. For modal forms with the "ShowModal" procedure, that can be a bit tricky.)

    And also, just because I didn't always have the habit of including the "Me" as owner when showing non-modal forms, I typically do a Load, and then some "Set NewForm.frmParent = Me" before showing these non-modal forms.

    However, if we're working within a single project, what I've provided will work just fine.

    Best Regards,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: view controls parent form in Modal called

    @mutley: Maybe it's also worth pointing out that any window (anything with a hWnd, Form or Control) can have either/both a Parent and an Owner. I say this because, in your title, you use the word "Parent", when I really think you mean "Owner". Or maybe you just mean something entirely different, such as an arbitrary form that loaded your current form, which would be a third concept. We might call that third concept something like FormWhoLoadedMe.

    Just briefly, a window's Parent has mostly to do with visual nesting, such as controls being within a frame, etcetera.

    A window's Owner has mostly to do with zorder and minimizing.

    A window's FormWhoLoadedMe is even more independent, with no "built-in" relationship between the two, other than what you define in code.

    And just to address Wqweto's point a bit more, here's code where you could actually set a form's owner if you really wanted to. It's easy to do within a single project with the .Show arguments. However, here's how to do it with an API call:

    Code:
    
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    
    Public Sub SetOwner(hWndTarget As Long, hWndParent As Long)
        ' GWL_HWNDPARENT is REALLY setting the OWNER.  It's just badly named.  A full explanation:
        '       Retrieve the Owner window       GetWindow(hWnd, GW_OWNER)                    
        '       Retrieve the Parent window      GetAncestor(hWnd, GA_PARENT)            
        '       Set the new Owner window        SetWindowLong(hWnd, GWL_HWNDPARENT, hWndNewOwner)
        '       Set the new Parent window       SetParent(hWnd, hWndNewParent)
        '
        '   An OWNED window is always above its OWNER in the z-order.
        '   The system automatically destroys an OWNED window when its OWNER is destroyed.
        '   An OWNED window is hidden when its OWNER is minimized.
        '
        Const GWL_HWNDPARENT As Long = &HFFFFFFF8
        SetWindowLong hWndTarget, GWL_HWNDPARENT, hWndParent
    End Sub
    

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

Tags for this Thread

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