Page 1 of 1

For loop help

Posted: Mon Jul 23, 2012 4:18 am
by Bogoh67
So i have an array of controls on the form
Code: Select all
Dim thectrls() As Control = {RegionBTN, FSBTN, FollowBTN, browseBTN, WBrowseBTN, Label1, Label2, Label3, SavetoTXT, WorkingTXT}
and i want to disable all of them in one code section i use this
Code: Select all
 For Each i As Control In thectrls
            i.Visible = False
        Next
but when i run this happens

For Each i As Control In thectrls
i.Visible = False
Next
Object reference not set to an instance of an object.
any help?

Re: For loop help

Posted: Mon Jul 23, 2012 7:33 am
by M1z23R
I used that but with same control type (label only) and it worked, i only changed it to For each L as Label. Not sure how to solve this :/

Edit:

If you are using threading (sockets for example) you'd have to use a callback thread. Maybe the error here is cross-thread operation. Not sure, but you can try with safe multi-thread.

Yet another edit: xD

I also remembered something, if you are doing this on form load event, try putting it in a form shown. Or in a simple button click to see if it works.

Re: For loop help

Posted: Mon Jul 23, 2012 9:06 am
by MrAksel
Its because one of the controls is 'Nothing'.
Code: Select all
For Each i As Control In thectrls
            If i IsNot Nothing Then i.Visible = False 'i.Enabled = false   ? You said you wanted to disable it'
        Next

Re: For loop help

Posted: Mon Jul 23, 2012 9:02 pm
by mandai
Depending on which event you are using this code in the cause could also be because thectrls is null.

Re: For loop help

Posted: Mon Jul 23, 2012 11:34 pm
by Bogoh67
MrAksel wrote:
Its because one of the controls is 'Nothing'.
Code: Select all
For Each i As Control In thectrls
            If i IsNot Nothing Then i.Visible = False 'i.Enabled = false   ? You said you wanted to disable it'
        Next
mandai wrote:
Depending on which event you are using this code in the cause could also be because thectrls is null.

Well you guys are right i think. I declared the thectrls control array in the form class and the for loop in the button click event.

new code:
Code: Select all
  Dim thectrls() As Control = {RegionBTN, FSBTN, FollowBTN, browseBTN, WBrowseBTN, Label1, Label2, Label3, SavetoTXT, WorkingTXT}
        FullScreen.Show()
        For Each i As Control In thectrls
            If i IsNot Nothing Then i.Visible = False 'i.Enabled = false   ? You said you wanted to disable it'
        Next
Thanks guys