Solved

VB Containers

Posted on 1998-11-25
3
444 Views
Last Modified: 2013-12-26
Is it possible to cycle through the controls in a container (frame or picture box) on a given form?  I know that the following code cycles through all controls on that form... but does not limit itself to the controls in that container.
Overview:  Place a picture box on the form and place a few text boxes in it.  As well as that, place a few text boxes just on the form.  Add a button and in the click event place the following code -

call clear_boxes(picture1)

Then add this code:

Public Sub clear_boxes(box As Object)
  Dim myobj As Object
  For Each myobj In box.Container
'..I know this gives the form, and not the container but???
    If TypeOf myobj Is TextBox Then
      myobj.Text = ""
    End If
  Next myobj
End Sub

0
Comment
Question by:tommy_boy
3 Comments
 
LVL 3

Expert Comment

by:vbWhiz
ID: 1488528
It's not exactly what you were looking for but you can check if the current control's (myobj) container is the box.

Your Modified Code:
Public Sub clear_boxes(box As Object)

  Dim myobj As Object
  For Each myobj In box.Parent
'now this actually gives the form, not the box's container
    If TypeOf myobj Is TextBox Then
        If Not TypeOf myobj.container Is Form Then
            If These_Are_Equal(myobj.container, box) Then
                myobj.Text = ""
            End If
        End If
    End If
  Next myobj
End Sub

Public Function These_Are_Equal(container As Object, box As Object) As Boolean

On Error GoTo No_Control_Array
These_Are_Equal = container.Name = box.Name And container.Index = box.Index

Exit Function

No_Control_Array:
These_Are_Equal = container.Name = box.Name

End Function


[-Note- the container property actually gives you a reference to whatever control contains whatever you are looking at. So box.container actually returns whatever contains the box(Usually a form but possibly another container control). If you were to take your original code and put one picturebox inside of another and try to run this routine on the inner-most picturebox, you would recieve an error (Object doesn't support this property or method). Another problem is control arrays (all the elements have the same name). You cannot check the index property of a control if it isn't inside an array, but if it is inside an array you MUST compare the index value to make sure the controls are the same. My answer to the array problem is to have a second function (These_Are_Equal) which first tries to compare indexes but if it encounters problems it just compares names. The .parent property returns the form so we can cycle through it's controls.]

I hope this helps you out.
0
 
LVL 1

Author Comment

by:tommy_boy
ID: 1488529
Sorry VBWhiz, just got on, and I have already found the problem. I was only refering to the name not the index of the control array (ie frame1(0), frame2(2) etc) The code is below if you need it at any time.  And yes I know it is a little messy, but I had to do it quickly.  Cheers!
This removes text from textboxes placed on a form inside frames.  The frames are a control array, and the textboxes can be anything they want to be.

Public Sub clear_boxes(f as form, inx As Integer, box as object)
  Dim myobj As Object
  For Each myobj In f
    If TypeOf myobj Is TextBox And myobj.Container.Name = box.Name Then '..now check to see if the index of the frame is the same as we require
      If myobj.Container.index = inx Then
        myobj.Text = ""
      End If
    End If
  Next myobj
End Sub



0
 
LVL 2

Accepted Solution

by:
JiaH earned 100 total points
ID: 1488530
How about this one:

Public Sub Clear_Boxes(box As Object)
    Dim ctrX As Control
    For Each ctrX In Controls
        If (TypeOf ctrX Is TextBox) And (ctrX.Container Is box) Then
            ctrX = ""
        End If
    Next
End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now