Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to set the background colour for a textbox in an array. All other properties are working properly, but the background colour. I am using VB.Net 2008 on Windows XP SP3. Is there something more I have to do get this property to work?

Below is a snippet of the code I am using.

Thanks in advance.


VB
ReDim txtBoxNo(20)
       txtBoxNo(0) = Me.txtNum0
       txtBoxNo(1) = Me.txtNum1
           ...
       ...
       txtBoxNo(18) = Me.txtNum18
       txtBoxNo(19) = Me.txtNum19
       '
       Try
           For iCount = 0 To 19
               With txtBoxNo(iCount)
                   .Enabled = True
                   .BackColor = Color.LightBlue
                   .ForeColor = Red
                   .Enabled = False
                   .Text = CStr(iCount + 1)
               End With
           Next
       Catch ex As Exception
           MsgBox(ex.Message)
           End
       End Try
Posted

1 solution

That's not going to work because you don't define the type of the array. This would work MUCH easier if you used a Generic collection like this:
Dim myTextBoxes As New List(Of TextBox)
myTextBoxes.Add(txtNum0)
myTextBoxes.Add(txtNum1)
myTextBoxes.Add(txtNum2)
...
' BTW: You don't have to put "Me" in front of everything...
...
' and you don't need the exception handling code
...
For i As Integer = 0 To myTextBoxes.Count - 1
    With myTextBoxes(i)
        ' I don't know why you're setting Enabled True and False.
        ' That doesn't do anything in this code.
        .BackColor = Color.LightBlue
        .ForeColor = Red
        .Text = (i + 1).ToString
    End With
Next
 
Share this answer
 
Comments
brodec2000 27-Feb-12 5:34am    
Dave, Thanks for you help, I tried your code, but I get the same result. All other properties work except the BackColor. Any other ideas?
Dave Kreskowiak 27-Feb-12 8:19am    
Are the Enabled properties on these set to True?? Are the ReadOnly properties set to False??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900