ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Grouping controls (https://www.excelbanter.com/excel-programming/352611-grouping-controls.html)

Mats Samson

Grouping controls
 
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

Robert ap Rhys

Grouping controls
 

"Mats Samson" wrote in message
...
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



Mats Samson

Grouping controls
 
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

"Robert ap Rhys" wrote:


"Mats Samson" wrote in message
...
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





All times are GMT +1. The time now is 02:51 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com