Solved

VB Containers

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

774 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