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

Change colors on other forms

Status
Not open for further replies.

VbAche

Programmer
Nov 28, 1999
8
0
0
US
Hello,
Working on a little homework project.

I have three forms. After the user selects which colors he would like to see the forms in, he than closes that form where he has chosen his color preferences from option buttons and the three forms are to be in the color preferences he has chosen. I am working with forecolor and backcolor - for the color preferences.

I am using a For Each Loop - but it don't work.
Dim frm as form
For each frm in forms
For each ctrl in frm.controls
ctrl.backcolor = (color) vbRed, vbYellow, etc.
Next
Next any help would be appreciated !!!
 
The Controls collection is not a member of the Visual Basic Collection class. It has a smaller set of properties and methods than a Collection object ...

These are not my words: they come straight from the source: Microsoft VB6 Language Reference pp192-193. In normal speak: The For ...Each loop may not provide you the expected results when used in connection with the Controls collection. Here's how to solve your problem:

Create a form frmOne with a textbox array (two textboxes), a label and a command button. Add a module to your project, and paste the following code into the module:


Dim frm1 As frmOne, frm2 As frmOne, frm3 As frmOne

Set frm1 = New frmOne
Set frm2 = New frmOne
Set frm3 = New frmOne

Load frm1
frm1.Show
Load frm2
frm2.Show
Load frm3
frm3.Show

ChangeFormColor

Unload frm1
Set frm1 = Nothing
Unload frm2
Set frm2 = Nothing
Unload frm3
Set frm3 = Nothing

End Sub

Private Sub ChangeFormColor()

Dim frm As Form
For Each frm In Forms

frm.BackColor = vbBlue

Dim ctrl As Control
Dim intIndex As Integer

For intIndex = 0 To frm.Controls.Count - 1

Set ctrl = frm.Controls(intIndex)
Select Case (TypeOf ctrl Is TextBox) Or (TypeOf ctrl Is Label)
Case True
ctrl.BackColor = vbRed
Case Else

'do nothing

End Select

Next intIndex

frm.Refresh

Next

'Flush object variable

Set ctrl = Nothing

End Sub


Make sure that the Project Properties set the Public Main() module as Startup Module.

The Main module loads and shows three forms: all are "copies" of the frmOne form. The ChangeFormColor procedure changes the Background color of the forms to blue and the Background color of the TextBoxes and Labels to red. The Background of the Command button remains unchanged

The colors are hardcoded into the procedure, but they can easily be selected through e.g. the Common Dialog Box or other means. The procedure is just meant to show the general outline.

WARNING:
This procedure works to my general satisfaction. However it should not ????? Use it at your discretion???
The same Microsoft Language Reference pp502 hints as a tip: the TypeOf objectname Is objecttype clause can't be used with the Select Case statement.

As you may have guessed, English (neither UK nor US) is NOT my native language and therefore I surely have missed something: the above sample code works perfectly!

Perhaps somebody out there can provide a sensible (and surely) simple explanation.
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
hello

Just read the reply from rvbasic and although I think it will work there does seem to be a lot of code. Here's another option:

Start by adding a module to your project (this is used to store variables used by more than one form) - click on Project | Add Module | Open to do this and then enter the following lines of code to the module:

Public form1backcolor As Long
Public form1forecolor As Long

Public form2backcolor As Long
Public form2forecolor As Long

Public form3backcolor As Long
Public form3forecolor As Long

Now return to your main form (the one with the option buttons to select colours) and add the code the variables just created in the module to the relevant colours using vbcolor, qbcolor or hex value. For example:

form1backcolor = vbred 'or whatever colour
form1forecolor = vbgreen

form2backcolor = &hff 'or whatever colour
form2forecolor = qbcolor(8)

form3backcolor = qbcolor(12)'or whatever colour
form3forecolor = rgb(100,100,100)

Then add the following code to the Form_Load() event of form1:

Dim ctl As Control

Me.BackColor = form1backcolor
Me.ForeColor = form1forecolor

For Each ctl In Me.Controls
On Error Resume Next
ctl.ForeColor = form1forecolor
Next

You will have to repeat this code for form2 and form3 - obviously you refer to form2backcolor/form2forecolor and form3backcolor/form3forecolor in each case!

Now if you show any of the forms (form1.show) the colours will be set as required.

I wasn't sure from your original post if by forecolour you just meant the forecolor of the form OR the forecolor of controls on the form. If you don't want to change the forecolor of controls on the form then take out the code from "For Each ctl In Me.Controls" to "Next"

Hope that helps
Kate
 
Kate is right: the code can be simplified as below:

Private Sub ChangeFormColor()

Dim frm As Form
For Each frm In Forms
frm.BackColor = vbBlue

Dim ctrl As Control
For Each ctrl In frm.Controls
On Error Resume Next
ctrl.BackColor = vbRed
Next

frm.Refresh
Next

End Sub


I wanted to show the Itemized use of the Controls Collection and at the same time use TypeOf to select specific Controls and that clobbered the skeleton code, for which my apologies.

By the way, if you want to change the BackColor of a Command Button, you should first set (at design or run time) its Style property to 1 - Graphical.

Nevertheless, I still do have problems understanding the comments about the Select Case and TypeOf combination.

PS: You surely observed that the first line of the code
Public Sub Main()
was accidentally dropped.
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top