Results 1 to 3 of 3

Thread: [RESOLVED] Error calling array of controles

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2013
    Posts
    56

    Resolved [RESOLVED] Error calling array of controles

    Every time I run my program, I get the "Object Reference Not Set To An Instance Of An Object", and I dont know exactly how to fix it.

    Ill paint the picture for you:

    Lets say theres a new form, and on this new form there are 4 panels (p1, p2, p3, p4) they are made in design time, then a button that is also made in design time.

    Now, on the click event for the button, what SHOULD happen, is the panels background colors should change.

    I have an array of these panels made at runtime, and try and set the background color from this array.

    Here is what I got:
    For pn As Integer = 1 To 4
    Dim panelBG() As Panel = {p1, p2, p3, p4}

    panelBG(pn).BackColor = Color.Green
    Next

    Why I am trying to do it this way is because, in each panel there is a label, and these labels text is getting info from a server, and this information is contained within a if statement that determines the color of certain boxes at certain times

    Anyone know how I might be able to fix this error? It suggested to use "new", but I wouldnt know where to put it.
    I tried putting it when I declared my array of panels: Dim panelBG() As New Panel = {p1, p2, p3, p4}
    Didnt work.

  2. #2
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Error calling array of controles

    This is an issue that came up yesterday I think on another post.

    I guess that you are declaring that array directly on the Form Class, outside any Sub. That is fine if you want it to be available to the whole form, but the controls do not exist at that point.

    What you should do is declare the array but do not initialize the values.

    Dim PanelBG() as Panel

    Then, inside the FormLoad event assign the array elements

    PanelBG = {p1, p2, p3, p4}
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2013
    Posts
    56

    Re: Error calling array of controles

    Quote Originally Posted by kaliman79912 View Post
    This is an issue that came up yesterday I think on another post.

    I guess that you are declaring that array directly on the Form Class, outside any Sub. That is fine if you want it to be available to the whole form, but the controls do not exist at that point.

    What you should do is declare the array but do not initialize the values.

    Dim PanelBG() as Panel

    Then, inside the FormLoad event assign the array elements

    PanelBG = {p1, p2, p3, p4}

    Wow thank you, it worked. Dont know how or why that never cross my mind haha.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width