Solved

VB Containers

Posted on 1998-11-25
3
442 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

762 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

18 Experts available now in Live!

Get 1:1 Help Now