|
-
Dec 3rd, 2018, 06:00 AM
#1
Thread Starter
Fanatic Member
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
-
Dec 3rd, 2018, 07:26 AM
#2
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.
-
Dec 3rd, 2018, 07:56 AM
#3
Thread Starter
Fanatic Member
Re: view controls parent form in Modal called
 Originally Posted by Elroy
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
-
Dec 3rd, 2018, 07:58 AM
#4
Thread Starter
Fanatic Member
Re: view controls parent form in Modal called
 Originally Posted by Elroy
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
-
Dec 3rd, 2018, 09:14 AM
#5
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
-
Dec 3rd, 2018, 09:37 AM
#6
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.
-
Dec 3rd, 2018, 11:15 AM
#7
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:
' Form2
Option Explicit
Private m_oOwnerForm As VB.Form
Friend Function frShow(oOwnerForm As VB.Form) As Boolean
Set m_oOwnerForm = oOwnerForm
Me.Show vbModal, m_oOwnerForm
Set m_oOwnerForm = Nothing
End Function
Private Sub Form_Load()
m_oOwnerForm.Caption = "Clicked at " & Timer
End Sub
. . . and use it like this
thinBasic Code:
' Form1
Option Explicit
Private Sub Form_Click()
With New Form2
If .frShow(Me) Then
MsgBox "Form2 confirmed!", vbExclamation
End If
End With
End Sub
cheers,
</wqw>
-
Dec 3rd, 2018, 11:25 AM
#8
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.
-
Dec 3rd, 2018, 12:44 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|