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

how do you check if a control is within another control?

Status
Not open for further replies.

STPMB

Programmer
Sep 13, 2002
28
US
How do you know if one control such as an option button is within another control such as a frame?
 

The following example has 2 command buttons (Command1,Command2) a frame (Frame1) that contains an option button (Option1) and another option button (Option2) directly on the form (Cut and paste the code in a demo project).

[tt]Option Explicit

Private Sub Command1_Click()
MsgBox Option1.Container & " " & Option1.Parent.Name
End Sub

Private Sub Command2_Click()
On Error GoTo ErrEh
MsgBox Option2.Container
Exit Sub
ErrEh:
MsgBox Option2.Parent.Name
End Sub

[/tt]

I hope this helps.
 
Try this if you want to know if it is contained at any level e.g. control inside a frame inside a frame.

Public Function IsContainedIn(objContained As Object, _
objContainer As Object) As Boolean
'*****
' objContained is to be tested for being contained in
' objContainer.
'*****
Dim objW As Object
'*****
'* climb up Container tree until a container
'* matches the one being tested or we reach the top container.
'*****
Set objW = objContained
Dim objWContainer As Object
Dim lngErr As Long
Do
If TypeOf objW Is Form Then ' at the top of the container chain
IsContainedIn = False
Exit Do
End If
' If our container is one being checked then we are contained
On Error Resume Next
' e.g. DataControl has no Container Property
Set objWContainer = objW.Container
lngErr = Err
On Error GoTo 0
If lngErr Then Exit Do ' No container property
If objWContainer Is objContainer Then
IsContainedIn = True
Exit Do
End If
' Climb up to the next container
Set objW = objWContainer
Loop
End Function
Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top