Check control types

G

Geoff Edwards

I know it might be a bit OTT, but many users of my templates get upset
when they find a space in a dialog which is both white but not
enabled.

I have written this code to check whether a control is enabled and
turn the white bit of the relevant controls (textbox, combobox,
listbox etc) grey if it is not:

Public Sub SetBackgroundColours(ctrl As Control)
With ctrl
If .Enabled Then
.BackColor = &H80000005
Else
.BackColor = &H8000000F
End If
End With
End Sub

This works perfectly well, but I haven't found a way of checking the
controls on a form to see if they are of a type of control which I
want to change. I do want to change the backgrounds of a textbox,
listbox or combobox to grey if it is disabled, but I don't want to
change the background of an entire frame to white if it enabled!
Hence

For Each ctrl In Me.Controls
SetBackgroundColours ctrl
Next

doesn't work.

At the moment, I am setting the tag of each "colourable" control to
(wait for it :) ) "Colourable" and using:

Dim ctrl As Control
For Each ctrl In Me.Controls
If ctrl.Tag = "Colourable" Then SetBackgroundColours ctrl
Next

but there must be a better method which will, presumably, involved
testing each control for its actual control type. How do I do that?

Or is there a better way of doing this altogether?

TIA

Geoff Edwards
Leeds, UK

e-mail: (e-mail address removed)
(replace the stopspambot bit with some
version of my name)
 
G

Geoff Edwards

To answer my own question, I have done it as below. Is that OK or am
I laying traps for myself? I cannot think of any control (that I use
at the moment anyway!) which has a .text property of which I won't
want to change the colour.

Public Sub SetBackgroundColours(frm As MSForms.UserForm)

Dim ctrl As Control
For Each ctrl In frm.Controls
Dim c As String
Dim e As Integer
Dim d As String

On Error Resume Next
c = ctrl.text
e = Err.Number
d = Err.Description
Err.Clear
On Error GoTo 0

' The control does not have text property
If e = 438 Then
Rem Do nothing
Else
With ctrl
If .Enabled Then
.BackColor = &H80000005
Else
.BackColor = &H8000000F
End If
End With

End If
Next
End Sub






I know it might be a bit OTT, but many users of my templates get upset
when they find a space in a dialog which is both white but not
enabled.

I have written this code to check whether a control is enabled and
turn the white bit of the relevant controls (textbox, combobox,
listbox etc) grey if it is not:

Public Sub SetBackgroundColours(ctrl As Control)
With ctrl
If .Enabled Then
.BackColor = &H80000005
Else
.BackColor = &H8000000F
End If
End With
End Sub

This works perfectly well, but I haven't found a way of checking the
controls on a form to see if they are of a type of control which I
want to change. I do want to change the backgrounds of a textbox,
listbox or combobox to grey if it is disabled, but I don't want to
change the background of an entire frame to white if it enabled!
Hence

For Each ctrl In Me.Controls
SetBackgroundColours ctrl
Next

doesn't work.

At the moment, I am setting the tag of each "colourable" control to
(wait for it :) ) "Colourable" and using:

Dim ctrl As Control
For Each ctrl In Me.Controls
If ctrl.Tag = "Colourable" Then SetBackgroundColours ctrl
Next

but there must be a better method which will, presumably, involved
testing each control for its actual control type. How do I do that?

Or is there a better way of doing this altogether?

TIA

Geoff Edwards
Leeds, UK

e-mail: (e-mail address removed)
(replace the stopspambot bit with some
version of my name)

Geoff Edwards
Leeds, UK

e-mail: (e-mail address removed)
(replace the stopspambot bit with some
version of my name)
 
J

Jonathan West

Hi geoff,

That would work, but you might like in future to consider using the TypeOf
operator to deal with this sort of thing, so that you have exact control
over the kinds of objects you are setting

Public Sub SetBackgroundColours(ctrl As Control)
If TypeOf ctrl Is MSForms.TextBox Or _
TypeOf ctrl Is MSForms.ListBox Or _
TypeOf ctrl Is MSForms.ComboBox Then
With ctrl
If .Enabled Then
.BackColor = &H80000005
Else
.BackColor = &H8000000F
End If
End With
End If
End Sub

In addition, if you wanted even finer control ove this, then you could put
some value into the Tag property of individual controls, and then read
ctrl.Tag value for each control and act accordingly.

Lots of ways to skin this particular cat :)

--
Regards
Jonathan West - Word MVP
http://www.multilinker.com
Please reply to the newsgroup
 
G

Geoff Edwards

Many many thanks.

I am never comfortable with using errors as a programming technique
(presumably from my QBasic and MBasic days :) )

I hadn't found TypeOf

Amazing how many things the help file doesn't have!

Thanks again.

Geoff



Hi geoff,

That would work, but you might like in future to consider using the TypeOf
operator to deal with this sort of thing, so that you have exact control
over the kinds of objects you are setting

Public Sub SetBackgroundColours(ctrl As Control)
If TypeOf ctrl Is MSForms.TextBox Or _
TypeOf ctrl Is MSForms.ListBox Or _
TypeOf ctrl Is MSForms.ComboBox Then
With ctrl
If .Enabled Then
.BackColor = &H80000005
Else
.BackColor = &H8000000F
End If
End With
End If
End Sub

In addition, if you wanted even finer control ove this, then you could put
some value into the Tag property of individual controls, and then read
ctrl.Tag value for each control and act accordingly.

Lots of ways to skin this particular cat :)

Geoff Edwards
Leeds, UK

e-mail: (e-mail address removed)
(replace the stopspambot bit with some
version of my name)
 

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

Top