[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

Parent Container Control Info

Anton

7/6/2011 1:20:00 AM

I want to create a control / parent / container info.
i.e.
1) input a control and then show its parent and container.
2) using that same control, show what it contains.
3) optionally go back up the tree and show the hierarchy of controls
related to the control input.

So far all my searches have turned up nothing.
The .Parent .Container etc is poorly explained everywhere I have
looked.

So far I have this but not sure the best way to do what I want.I am not
interested in a visual representation like a TreeView, although that
would be an interesting side job. I have little TreeView experience.

I think what I have here gathers all the info but I am hoping there is
a more direct method to just go to the result I want.

Suggestions please.

Public Function ControlTree(oFrm As Form) As String()
' gather the nodes for a tree

On Error Resume Next
Dim asTree() As String
Dim asArgs() As String
Dim lIdx As Long
Dim lX As Long

ReDim asTree(0 To oFrm.Controls.Count - 1)

For lIdx = 0 To oFrm.Controls.Count - 1

asTree(lIdx) = oFrm.Controls(lIdx).Parent.Name & vbTab &
oFrm.Controls(lIdx).Container.Name & vbTab & oFrm.Controls(lIdx).Name
If err.Number Then
asTree(lIdx) = oFrm.Controls(lIdx).Parent.Name & vbTab &
oFrm.Controls(lIdx).Name
err = 0
End If

asArgs = Split(asTree(lIdx), vbTab)
For lX = 0 To UBound(asArgs)
If UBound(asArgs) = 1 Then
sMsg = "Parent=" & asArgs(0) & vbTab & "Control=" &
asArgs(1)
Else
sMsg = "Parent=" & asArgs(0) & vbTab & " Container=" &
asArgs(1) & vbTab & "Control=" & asArgs(2)
End If
Debug.Print sMsg
Next lX

Next lIdx

ControlTree = asTree

End Function 'ControlTree


9 Answers

mm

7/6/2011 1:59:00 AM

0

El 05/07/2011 10:19 p.m., BeeJ escribió:

> Public Function ControlTree(oFrm As Form) As String()

The Parent will always be the form.

mm

7/6/2011 2:25:00 AM

0

El 05/07/2011 10:19 p.m., BeeJ escribió:
> I want to create a control / parent / container info.

Public Function GetContainedControls(nObject As Object, _
Optional ByVal nIndentation As Long = 0) As String

Dim iParent As Form
Dim iStr As String
Dim iCtl As Control

If TypeOf nObject Is Form Then
Set iParent = nObject
Else
On Error Resume Next
Set iParent = nObject.Parent
On Error GoTo 0
End If
If iParent Is Nothing Then Exit Function

iStr = String$(nIndentation, " ") & nObject.Name & vbCrLf
nIndentation = nIndentation + 4

On Error Resume Next
For Each iCtl In iParent
Err.Clear
If iCtl.Container Is nObject Then
If Err.Number = 0 Then
iStr = iStr & GetContainedControls(iCtl, nIndentation)
End If
End If
Next

GetContainedControls = iStr
End Function


Ian Post

7/6/2011 4:23:00 AM

0

Eduardo formulated the question :
> El 05/07/2011 10:19 p.m., BeeJ escribió:
>> I want to create a control / parent / container info.
>
> Public Function GetContainedControls(nObject As Object, _
> Optional ByVal nIndentation As Long = 0) As String
>
> Dim iParent As Form
> Dim iStr As String
> Dim iCtl As Control
>
> If TypeOf nObject Is Form Then
> Set iParent = nObject
> Else
> On Error Resume Next
> Set iParent = nObject.Parent
> On Error GoTo 0
> End If
> If iParent Is Nothing Then Exit Function
>
> iStr = String$(nIndentation, " ") & nObject.Name & vbCrLf
> nIndentation = nIndentation + 4
>
> On Error Resume Next
> For Each iCtl In iParent
> Err.Clear
> If iCtl.Container Is nObject Then
> If Err.Number = 0 Then
> iStr = iStr & GetContainedControls(iCtl, nIndentation)
> End If
> End If
> Next
>
> GetContainedControls = iStr
> End Function

Perfect!
Thank you!
I am able to follow it too.
I knew recursion was the answer but could not figure it out.


mm

7/6/2011 7:43:00 AM

0

El 06/07/2011 01:23 a.m., BeeJ escribió:

> Perfect!
> Thank you!
> I am able to follow it too.
> I knew recursion was the answer but could not figure it out.

You're welcome.

Ian Post

7/6/2011 3:31:00 PM

0

It happens that BeeJ formulated :
> Eduardo formulated the question :
>> El 05/07/2011 10:19 p.m., BeeJ escribió:
>>> I want to create a control / parent / container info.
>>
>> Public Function GetContainedControls(nObject As Object, _
>> Optional ByVal nIndentation As Long = 0) As String
>>
>> Dim iParent As Form
>> Dim iStr As String
>> Dim iCtl As Control
>>
>> If TypeOf nObject Is Form Then
>> Set iParent = nObject
>> Else
>> On Error Resume Next
>> Set iParent = nObject.Parent
>> On Error GoTo 0
>> End If
>> If iParent Is Nothing Then Exit Function
>>
>> iStr = String$(nIndentation, " ") & nObject.Name & vbCrLf
>> nIndentation = nIndentation + 4
>>
>> On Error Resume Next
>> For Each iCtl In iParent
>> Err.Clear
>> If iCtl.Container Is nObject Then
>> If Err.Number = 0 Then
>> iStr = iStr & GetContainedControls(iCtl, nIndentation)
>> End If
>> End If
>> Next
>>
>> GetContainedControls = iStr
>> End Function
>
> Perfect!
> Thank you!
> I am able to follow it too.
> I knew recursion was the answer but could not figure it out.

I needed the control(index) so I added/changed as follows.
Does this look OK?

' ADDED
Dim sIdx as String

On Error Resume Next
sIdx = nObject.Index
If err.Number = 0 Then
sIdx = "(" & sIdx & ")"
Else
sIdx = ""
err.Clear
End If

'CHANGED
iStr = String$(nIndentation, " ") & nObject.Name & sIdx & vbCrLf


Ian Post

7/6/2011 3:58:00 PM

0

BeeJ laid this down on his screen :
> Eduardo formulated the question :
>> El 05/07/2011 10:19 p.m., BeeJ escribió:
>>> I want to create a control / parent / container info.
>>
>> Public Function GetContainedControls(nObject As Object, _
>> Optional ByVal nIndentation As Long = 0) As String
>>
>> Dim iParent As Form
>> Dim iStr As String
>> Dim iCtl As Control
>>
>> If TypeOf nObject Is Form Then
>> Set iParent = nObject
>> Else
>> On Error Resume Next
>> Set iParent = nObject.Parent
>> On Error GoTo 0
>> End If
>> If iParent Is Nothing Then Exit Function
>>
>> iStr = String$(nIndentation, " ") & nObject.Name & vbCrLf
>> nIndentation = nIndentation + 4
>>
>> On Error Resume Next
>> For Each iCtl In iParent
>> Err.Clear
>> If iCtl.Container Is nObject Then
>> If Err.Number = 0 Then
>> iStr = iStr & GetContainedControls(iCtl, nIndentation)
>> End If
>> End If
>> Next
>>
>> GetContainedControls = iStr
>> End Function
>
> Perfect!
> Thank you!
> I am able to follow it too.
> I knew recursion was the answer but could not figure it out.

I added this routine to get the hierarchy for a control.
The result of your routine, with my mods, was placed in g_sCtrlTree
Does this look OK?

usage
Debug.Print "Control Family " & ControlHierarchy("optUSBSel(0)")

Public Function ControlHierarchy(sCtrlName As String) As String

On Error GoTo ControlHierarchyErr

Dim asLine() As String
Dim lX As Long
Dim sFamily As String
Dim lIndent As Long

asLine = Split(g_sCtrlTree, vbCrLf)

For lX = 0 To UBound(asLine)
If InStr(1, asLine(lX), sCtrlName) > 0 Then
Exit For
End If
Next lX

If lX <= UBound(asLine) Then
' back up
lIndent = Len(asLine(lX)) - Len(LTrim$(asLine(lX)))
sFamily = asLine(lX)
Do
lIndent = lIndent - clIndent
Do
lX = lX - 1
Loop Until (Len(asLine(lX)) - Len(LTrim$(asLine(lX)))) =
lIndent

sFamily = asLine(lX) & sFamily
Loop Until lIndent = 0
End If

ControlHierarchy = sFamily

ControlHierarchyExit:
Exit Function

ControlHierarchyErr:
Debug.Assert False
Resume ControlHierarchyExit

End Function 'ControlHierarchy


mm

7/6/2011 10:23:00 PM

0

El 06/07/2011 12:58 p.m., BeeJ escribió:

This is what you want?

usage
Debug.Print "Control Family " & GetControlHierarchy(optUSBSel(0))

(Now you send the control, not the control's name)

Public Function GetControlHierarchy(nControl As Control) As String
Dim iParent As Form
Dim iCtl As Control
Dim iStr As String


Set iParent = nControl.Parent
Set iCtl = nControl

iStr = "." & ControlName(iCtl)
Do Until iCtl.Container Is iParent
iStr = "." & ControlName(iCtl.Container) & iStr
Set iCtl = iCtl.Container
Loop
GetControlHierarchy = iParent.Name & iStr
End Function

Private Function ControlName(nControl As Control)
Dim sIdx As String

ControlName = nControl.Name

On Error Resume Next
sIdx = nControl.Index
If Err.Number = 0 Then
sIdx = "(" & sIdx & ")"
Else
sIdx = ""
Err.Clear
End If

ControlName = ControlName & sIdx
End Function

Anton

7/8/2011 9:51:00 PM

0

Eduardo used his keyboard to write :
> El 06/07/2011 12:58 p.m., BeeJ escribió:

> This is what you want?

> usage
> Debug.Print "Control Family " & GetControlHierarchy(optUSBSel(0))

> (Now you send the control, not the control's name)

> Public Function GetControlHierarchy(nControl As Control) As String
> Dim iParent As Form
> Dim iCtl As Control
> Dim iStr As String


> Set iParent = nControl.Parent
> Set iCtl = nControl

> iStr = "." & ControlName(iCtl)
> Do Until iCtl.Container Is iParent
> iStr = "." & ControlName(iCtl.Container) & iStr
> Set iCtl = iCtl.Container
> Loop
> GetControlHierarchy = iParent.Name & iStr
> End Function

> Private Function ControlName(nControl As Control)
> Dim sIdx As String

> ControlName = nControl.Name

> On Error Resume Next
> sIdx = nControl.Index
> If Err.Number = 0 Then
> sIdx = "(" & sIdx & ")"
> Else
> sIdx = ""
> Err.Clear
> End If

> ControlName = ControlName & sIdx
> End Function

Well that is great for the direct approach for one given control but I
was going for a more general case where I could specify optUSB and get
all controls listed with optUSB in their name. I am tinkering with the
use of Like and maybe RegEx.

Thank you for all your insights.


mm

7/9/2011 9:05:00 AM

0

El 08/07/2011 06:51 p.m., BeeJ escribió:

> Well that is great for the direct approach for one given control but I
> was going for a more general case where I could specify optUSB and get
> all controls listed with optUSB in their name. I am tinkering with the
> use of Like and maybe RegEx.
>
> Thank you for all your insights.

OK, then add this function:

Public Function GetHierarchy(nForm As Form, nControlName As String)
Dim iCtl As Control
Dim iStr As String

For Each iCtl In nForm
If iCtl.Name Like "*" & nControlName & "*" Then
iStr = iStr & GetControlHierarchy(iCtl) & vbCrLf
End If
Next
GetHierarchy = iStr
End Function

Usage:
Debug.Print GetHierarchy(Me, "Label")