Resolved How can I change the bg color of a text box with code

Mixelplik

Member
Joined
Dec 12, 2009
Messages
5
Programming Experience
Beginner
I'm studying chemistry and wanted to make a quick app for studying the periodic table.

What I have is 36 text boxes. I have a structure that can hold each of the 36 element's info, and an array with 36 instances of the structure.

Example:
VB.NET:
   Dim Elements(37) As Element
 Structure Element
        Dim strSymbol As String
        Dim strElementName As String
        Dim strNumber As String
        Dim strSymbolAnswer As String
        Dim box As TextBox
    End Structure
        Elements(1).strNumber = "1"
        Elements(1).strSymbol = "H"
        Elements(1).strElementName = "Hydrogen"
        Elements(1).strSymbolAnswer = Me.txtH.Text
        Elements(1).box = Me.txtH

I want to run the app, and then fill in each text box with the appropriate info, I want the text boxes correctly filled in to turn green, and more importantly the incorrect ones to turn blue.

(A) I get this error, (SOMTIMES ARRG!)
Null Reference Exception Was Unhandled
I have no clue why I'm getting this error sometimes, I'm referencing the box from my form

Here's my logic code
VB.NET:
   Dim getResults(1) As String

        Dim i As Integer = 1


        While i < 37
            If Elements(i).strSymbolAnswer = Elements(1).strSymbol Then


                i = i + 1
                ReDim Preserve getResults(i + 1)
                Elements(i).box.BackColor = Color.CadetBlue
            Else

                getResults(i) = Elements(i).strSymbolAnswer
                Elements(i).box.BackColor = Color.Chartreuse
                ReDim Preserve getResults(i + 1)
                i = i + 1
            End If

        End While

    End Sub

note when I mess with it, sometime the text boxes will fill in, but randomly and the comparison between the input text and the info stored in the data structure never compare correctly. 90% of the time the structure data is showing a null. It's all driving me nuts.

I about to scrap this simple project, as I'm spending more time making a study aid then actually studying.
 
Last edited:
If you fixed the problem yourself, please post your solution as it might help someone else with the same or similiar problem.

Thanks.
 
My problem

This was actually a simple fix. If you look at my error you'll see "unhandled" usually when your getting this in conjuction with an array and a loop it means something is out of synch. In this case I have 36 elements and since arrays start at 0 I wanted each array member to reflect the element number it contained. Basically, it was creating one too many .box objects than the array could hold.
I think I was increnting the array loop before I was creating it's associated box

You'll see the 1=1+1 was too early in the if then body.
 
Back
Top