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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To Theme a Form 2

Status
Not open for further replies.
Apr 27, 2006
126
GB
Hi,

I hoped this was simple but I guess the answer isn't as obvious as I thought. Basically, I have a form (or forms) I want to be able to change the appearance of, basically change the theme.

So say the form has 3 labels and 3 buttons, rather than saying
Code:
form1.label1.font.name = "courier"
form1.label1.font.color = "magenta"
...

I hoped I could perform something along the lines of (although I know this isn't right):

Code:
Dim stuff as object? (tried msforms.things too)

for each stuff in form1
  stuff.font.name = "courier"
  stuff.font.color = "magenta"
next

see what I mean? Would be lovely to be able to just re-jig a whole form in one shot and have all the buttons, labels and whatnot all uniform :)

________
clueless
 
did you try
for each control on form

ck1999

 
i believe I did but I'll double check once I boot into windows again.

________
clueless
 
It would also help when you post, to mention things like what application, is it a userform...stuff like that.

I reason I ask is that

stuff.font.color = "magenta"

may not be an acceptable syntax for a control on a userform.

faq219-2884

Gerry
My paintings and sculpture
 
You might want to think about using AutoFormat. It allows you to turn a currently existing form into an AutoFormat and then use the same formatting for any form you want. When you decide you need something new, create a new "dummy" form and simply place one control of each type you wish on it and format them. Now follow the short little tutorial below.

****************************************​

You can turn any form you have into a custom AutoForm. From Design View for the form, goto Format - AutoFormat and click on "Customize." Now check the radio button for "Create a new AutoFormat based on YourFormName." You'll be asked to give the AutoFormat a name, then you're done.

Now, anytime you want to use this same formatting for a form, from the Form Wizard select the AutoFormat, or from form Design View goto Format - AutoFormat and select the AutoFormat you want to use. And here's the best part, you can do this to a form you've already created using another formatting, and it will change that form 's formatting to the newly chosen AutoFormat!



The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
The mention of msforms in the original post strongly suggests that we are looking at a UserForm. AutoForm only works in Access (and even there only against Access formsr rather than userforms).

Given the info we've got so far, it looks like ck1999's suggestion of stepping through each control on the form is the most apprpriate idea.
 
sorry, I usually state what I'm using. This is a userform in Excel (2003)

with regards to ck1999's idea, I used:

Code:
Dim obj As Control

For Each obj In UserForm1
 obj.????
Next

I don't know what to put after "obj" to control the style of the buttons/labels/whatever. Nothing seems obvious in the list which will perform what I'm trying to achieve. Any ideas?

________
clueless
 
... in UserForm1.Controls
See ColorConstants for anailable colour names (there are only few, vbMagenta including), for non-named colours you need RGB values.
The code will work only in run-time.

combo
 
It may also be worth putting in TypeOf check so that if you have any controls that don't support the properties you're changing your code doesn't error out. E.g.
Code:
Dim obj As Control

For Each obj In UserForm1.Controls
 If TypeOf obj Is CommandButton Or TypeOf obj Is MSForms.TextBox Then
    'set properties
 End If
Next
Hope this helps

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Sorry, in my last post, MSForms.TextBox should, of course, have been MSForms.Label [blush]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Try something like this. I used combo suggestion about RGB

Code:
  Dim c As Control
  
  For Each c In Me
  Select Case c.ControlType
    Case acTextBox, acComboBox, acListBox, acCheckBox, acLabel, acToggleButton
                    c.FontName = "COurier New"
                    c.ForeColor = RGB(0, 80, 0)
    End Select
    
  Next c

I used select statement in case you wanted to change fonts and colors based on control type

ck1999
 
Code:
  Dim c As Control
  
  For Each c In Me
  Select Case c.ControlType
    Case acTextBox, acComboBox, acListBox, acCheckBox, acLabel, acToggleButton
                    c.FontName = "COurier New"
                    c.ForeColor = RGB(0, 80, 0)
    End Select
    
  Next c

This bugs out on "For Each c In Me" (or if Me is replaced with userform1) with "Object does not support this property or method"

________
clueless
 
WOO! figured it out!

Code:
Private Sub CommandButton2_Click()
Dim frmctl As Control
Dim r As Integer
For Each frmctl In UserForm1.Controls
    r = r + 1
    UserForm1.Controls.Item(r - 1).Font.Bold = True
    UserForm1.Controls.Item(r - 1).Font.Name = "Courier New"
Next
End Sub

Couldn't have got there without you guys though, so thanks for the help! :)

________
clueless
 
incidentally, I will still look at implementing the select case check in there so thanks ck1999

________
clueless
 
Why not simply this ?
Code:
Private Sub CommandButton2_Click()
Dim frmctl As Control
For Each frmctl In UserForm1.Controls
    frmctl.Font.Bold = True
    frmctl.Font.Name = "Courier New"
Next
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
haha, why not indeed?

Thanks PHV, I did feel that the solution I had was a bit of an un-ideal way of doing it but as I hadn't managed to get anything else working I just went with it.

You're the man, thanks :)

________
clueless
 
It worked fine in Access 2007. Not sure why it would not work with you.

ck1999
 
The TypeOf (as suggested in my previous post) will work fine for checking the controls type... [wink]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
ck1999, your code is for Access.Form objects, not MSForms.UserForm

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
To have custom theme for userform in design time:
- add various controls onto the userform and format them (font, colour, size, text),
- add a new page to the toolbox (just for some order),
- drag formatted controls onto the toolbox (could be more than one in one drag).

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top