Question

  • Thread starter Thread starter Jack
  • Start date Start date
J

Jack

why does this stament generate an error if
Checkbox_1 through 5 : exist

Dim i As Integer
For i = 1 To 5
Controls("Checkbox_" & CStr(i)), Object).BackColor = Color.Black
Next


The error are
Property access must assign to the property or use its value.

and

End of statement expected.
 
Try:

Dim i As Integer

For i = 1 To 5
Controls("Checkbox_" & CStr(i)).BackColor = Color.Black
Next



The ",Object" is not valid syntax.
 
Robinson said:
Try:

Dim i As Integer

For i = 1 To 5
Controls("Checkbox_" & CStr(i)).BackColor = Color.Black
Next



The ",Object" is not valid syntax.
From the ", Object" syntax, could it supposed to have been...

CType(Control("Checkbox_" & CStr(i), Object)).BackColor = Color.Black

Or better still;

DirectCast(Control("Checkbox_" & CStr(i), Checkbox)).BackColor = Color.Black

.... ? Alternate ways of doing it anyway...!
________________________________
Grim Reaper
 
I had done that previously and get the following error

Run-time exception thrown : System.NullReferenceException - The
pointer for this method was null.

It givers the following Troubleshooting tips:

Use the "new" keyword to create an object instance.
5
Check to determine if the object is null before calling the method.

Why would it give a null exception if Checkbox_1 through five are on
the form
 
It will return null if the item in question is not in the controls
collection, for example:




Dim theControl as Control = Controls ( "thenameofthecontrol" )

If theControl IsNot Nothing Then

' I can use the control

theControl.BackColor = .....

Else

' The control with that name does not exist in the collection

End If
 
why does this stament generate an error if
Checkbox_1 through 5 : exist

Dim i As Integer
For i = 1 To 5
Controls("Checkbox_" & CStr(i)), Object).BackColor = Color.Black
Next

Personally, I'd prefer to create an array of Checkboxes and loop through
that:

Private setChecks as CheckBox() _
= { Me.CheckBox_1 _
, Me.CheckBox_2 _
... _
, Me.CheckBox_5 _
}

For Each eCB as CheckBox in setChecks
eCB.BackColor = Color.Black
Next

Regards,
Phill W.
 
Phill W. said:
Personally, I'd prefer to create an array of Checkboxes and loop through
that:

Private setChecks as CheckBox() _
= { Me.CheckBox_1 _
, Me.CheckBox_2 _
... _
, Me.CheckBox_5 _
}

For Each eCB as CheckBox in setChecks
eCB.BackColor = Color.Black
Next

Regards,
Phill W.

Or, if he's changing every checkbox on the form, he could
iterate through all the controls on the form and if it's
a checkbox, change the backcolor. Then he can add and remove
checkboxes willy-nilly without having to change the code.

Robin S.
 
Sorry for a late reply but could you not use DataBinds

CheckBox2.DataBindings.Add("BackColor", CheckBox1, "BackColor")
CheckBox3.DataBindings.Add("BackColor", CheckBox1, "BackColor")
CheckBox4.DataBindings.Add("BackColor", CheckBox1, "BackColor")
CheckBox5.DataBindings.Add("BackColor", CheckBox1, "BackColor")

Then later in your code

CheckBox1.BackColor = Color.Black

Which makes all five Checkboxes have the same color?
 
Back
Top