Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

VB Items in a Frame

Hi.

Is there a way to change a common property at runtime for all the items in a frame or form?

i.e.
For each THING in thisFORM
      THING.BackColor = vbWhite
Next THING

OR for a Frame?

Cheers  

Sheepie
0
SheepDip
Asked:
SheepDip
3 Solutions
 
kapil_mohtaCommented:

It's exactlly the way you think :

Dim ctrl As Control

For Each ctrl In Me.Controls
    ctrl.BackColor = vbRed
Next

Kapil
0
 
Thandava VallepalliCommented:
Hello,

Place 5 textboxes and 1 command button in the form.  And place the code in code window.

===========================
Private Sub Command1_Click()
    Dim ctrl As Control
    For Each ctrl In Form1.Controls
        ctrl.FontBold = True
    Next ctrl
End Sub
===========================

Now run the form. Press the button all the controls in the form text becomes bold.

So here is the syntax to access all the controls in the form

"form_name.Controls" will return collection of all the controls

All the best,
V.Thandava Krishna.
0
 
TimCotteeCommented:
Hi SheepDip,

For controls in a form this is as kapil_mohta suggests. However as a frame has no .controls collection and all controls placed on it are actually part of the form's .controls collection you need to check whether the control is contained in the frame or not:

For Each Ctrl In Me.Controls
  If Ctrl.Container Is Frame1 Then 'Check whether the control is contained in the frame
    'Do something with it
  End If
Next

Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 
SheepDipAuthor Commented:
Thks all.

Kept getting an error as certain objects did not have the backcolor property (and i couldn't be arsed finding out which ones - there are a lot). So dropped in the dreaded catch all on error resume next.

     For Each ctrlA In frmTicket.Controls ' Change ticket colour = SELL
            On Error Resume Next
            ctrlA.BackColor = &HC0C0FF
      Next ctrlA
      On error goto 0
 
cheers again

SD
0
 
kapil_mohtaCommented:
To know the control type , you can use : typeof

i.e.
For Each ctrl In Me.Controls
    if typeof ctrl is textbox then ' say
        ctrl.BackColor = vbRed
    endif
Next

Kapil
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now