Grouping controls

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it possible to group specific controls from other controls of the same
type in a Userform? I cannot use a Frame because the controls are in
different locations in the form! Let's say that I want to set a specific
bunch of labels with red forecolour when a certain condition is met (pressing
a button f.i.) but all the other labels stay
with black forecolour. Another issue would be to disable a bunch of
Textboxes that
are not related to the current task in the UserForm.
I tried to manipulate their Name, ex. ALabel1, BLabel1, ALabel2,
BLabel2....etc.
and then refer to the names as group of controls, but I'm not certain how to
accomplish this, so I better ask you experts out there!
Best regards
Mats
 
Mats Samson said:
Is it possible to group specific controls from other controls of the same
type in a Userform? I cannot use a Frame because the controls are in
different locations in the form! Let's say that I want to set a specific
bunch of labels with red forecolour when a certain condition is met (pressing
a button f.i.) but all the other labels stay
with black forecolour. Another issue would be to disable a bunch of
Textboxes that
are not related to the current task in the UserForm.
I tried to manipulate their Name, ex. ALabel1, BLabel1, ALabel2,
BLabel2....etc.
and then refer to the names as group of controls, but I'm not certain how to
accomplish this, so I better ask you experts out there!

Using a naming convention:

I added three textboxes to a form and called them txtAName, txtAAddress and
txtBAge. Then I created the following test routine.

Private Sub CommandButton1_Click()
Dim obj As Control
Dim objT As MSForms.TextBox
For Each obj In Me.Controls
If TypeOf obj Is MSForms.TextBox Then
If Left(obj.Name, 4) = "txtA" Then
Set objT = obj
objT.BackColor = vbYellow
End If
End If
Next
End Sub

Works fine for me.

Robert
 
Thanks a lot Robert,
it did exactly what I wanted!
And I even cut it to selecting a group of different controls with a "coded"
namelabel:
Dim obj As Control
For Each obj In Me.Controls
If Left(obj.Name, 2) = "Pu" Then
obj.Enabled = True
End If
Next
By coding the Label Name I can get any control to belong to several groups
(selections) by using Left(obj.Name, n) = "?", i.e. if n=2, PuRLabel10 and
PuSLabel19 belongs to the same selection but if n=3, they are in different
groups! Very neat, but needs of course a careful planning when naming the
labels.
Best regards
Mats
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top