Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set button back color via code 3

Status
Not open for further replies.

LonnieJohnson

Programmer
Apr 16, 2001
2,628
US
I want to write code to change the backcolor of my buttons. I have already got the part that cycle's through the contols on the form and determines if if it's a command button. I can't figure out how to tell it the color.

I noticed that in the button's back color property there is a system, custom and web color tab. That have different values. For instance the web has values like MediumPurple but the Custom tab has values like 192, 192, 255. The system did not like me using the 192, 192, 255 in or out of quoatation marks.

Finally, my question. How do I assign a value to the backcolor of a button with code? Also, how do I choose if I want custom or web?

Thanks in advance.

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
The intellisense will help you.

Button1.BackColor = Color.Red 'For the "Web" red color
Button1.BackColor = Color.FromArgb(192,192,255) 'For a custom color
 
Thanks RiverGuy,

Apperently I am going about this the wrong way. This is how I use to do it in Access and it is not changing the button color. It doesn't error or anything. Just don't do anything. I even stuck a message box in there to give me the control names just to see if it was firing and it is not.

Can you help?

Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Name Like "btn*" Then
ctl.BackColor = Color.Aqua
End If
Next


ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
Code:
Dim i2 As Image
Dim ctl As Control
For Each ctl In Me.Controls
  If ctl.Name.StartsWith("btn") Then
     ctl.BackColor = Color.Aqua
   End If
Next

this works better

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
chrissie1, I really like that StartsWith thing. I am like a little kid in a candy store. It's all good to me.

Actually my problem was with the names of my buttons. Blatant error on my part. I had a prefix of bnt vs btn. The first way works fine. But I still like the StartsWith thing.

RiverGuy you very first response was vey helpful.

Thank you both.

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
Thanks again guys. Is there a universal way of doing this so that I can put it in a global module and call it from any form? I know this probably has something to do with the inheriting thing.


ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
Not really to do with inheritance.

You need to do three things to make this a universal sub

1. Pass a reference to the form to the function.
2. Change the starts with control name to TypeOf - it's a much better way.
3. Use recursion in case some of the controls on the form are nested inside other controls (for example, panels).

So, overall,
Code:
Public sub ChangeColors (byref ctnr as Control , byref c as color)
For Each ctl as control In ctnr.Controls
  If typeof ctl is Button then
     ctl.BackColor = c
else if ctl.hasControls then 
  ChangeColors(ctl) 'recursion - function calls itself
End If
Next
end sub

I've only just thrown this together - it might not compile but you should get the idea.

Mark [openup]
 
Oh I forgot to say that to call it from the form, you just say

ChangeColors(Me)

since Form inherits Control, this will be fine.

Mark [openup]
 
Thanks Custom24. That was great. However, I couldn't get it to compile with the....

else if ctl.hasControls then
ChangeColors(ctl) 'recursion - function calls itself

part. I commented this out and it ran fine. I even added another color parm to change the fore color.



ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
Sorry - I guess I should have checked that it compiled.

Here you go

Code:
    Public Sub ChangeColors(ByRef ctnr As Control, ByRef c As Color)
        For Each ctl As Control In ctnr.Controls
            If TypeOf ctl Is Button Then
                ctl.BackColor = c
            ElseIf ctl.Controls.Count <> 0 Then
                ChangeColors(ctl, c) 'recursion - function calls itself
            End If
        Next
    End Sub

Mark [openup]
 
Cool. There is some much more to this than VB 6.0 and Access.

Thanks a bunch.

You guys will definately be hearing more from me.

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top