For loop help

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
5 posts Page 1 of 1
Contributors
User avatar
Bogoh67
VIP - Site Partner
VIP - Site Partner
Posts: 656
Joined: Sun Apr 18, 2010 8:20 pm

For loop help
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?
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: For loop help
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.
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: For loop help
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
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: For loop help
mandai
Depending on which event you are using this code in the cause could also be because thectrls is null.
User avatar
Bogoh67
VIP - Site Partner
VIP - Site Partner
Posts: 656
Joined: Sun Apr 18, 2010 8:20 pm

Re: For loop help
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
5 posts Page 1 of 1
Return to “Coding Help & Support”