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.
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
So i have an array of controls on the form
For Each i As Control In thectrls
i.Visible = False
Next
Code: Select all
and i want to disable all of them in one code section i use this
Dim thectrls() As Control = {RegionBTN, FSBTN, FollowBTN, browseBTN, WBrowseBTN, Label1, Label2, Label3, SavetoTXT, WorkingTXT}
Code: Select all
but when i run this happens For Each i As Control In thectrls
i.Visible = False
Next
For Each i As Control In thectrls
i.Visible = False
Next
Object reference not set to an instance of an object.any help?
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.
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.
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]()
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!

Depending on which event you are using this code in the cause could also be because thectrls is null.
MrAksel wrote:Its because one of the controls is 'Nothing'.Code: Select allFor 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
Thanks guys 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
5 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023