Setting color of controls on runtime

RGS

Member
Licensed User
This fragement of code:

Controls() = GetControls("frmMain")
For i = 0 To ArrayLen(Controls())-1
If ControlType(Controls(i)) = "Label" Then
Control(Controls(i)).Color = cWhite
End If
Next

does not seem to work in my application. During execution the error "If ControlType(Controls(i)) = "Label" Then Error Description: Object reference not set to an instance of an object" is raised.

Types of controls on my form: TabControl, Labels, TextBox, Images and Listbox

Am I overlooking something? Thanks in advance.
 

moster67

Expert
Licensed User
This works (it is just like your code - the only difference is that I declared the array in the Global-part).

rgds,
moster67


B4X:
Sub Globals
   'Declare the global variables here.
   Dim controls(0)
End Sub

Sub App_Start
   'frmMain.Show
   Controls() = GetControls("frmMain")
For i = 0 To ArrayLen(Controls())-1
If ControlType(Controls(i)) = "Label" Then
Control(Controls(i)).Color = cWHite
End If
Next

frmMain.Show
End Sub
 

specci48

Well-Known Member
Licensed User
The problem is the TabControl!
If I only used controls added by the designer, everything works fine (thats what moster67 surely did).
But the ControlType statement doesn't work for the TabControl. I'll get an "Unable to evaluate expression." in the debug mode.

I can't explain this behaviour because I'm not so familiar with the .Net and basic4ppc basics. Anyone? :sign0085:

specci48
 

RGS

Member
Licensed User
Ah, that's it. That only leaves setting the color manually for each component. Thanks.
 
Top