|
-
Aug 10th, 2012, 04:13 PM
#1
Thread Starter
Lively Member
Hide everything on a form?
Hi,
I have a form with 37 labels, 36 of which are currently visible. I want to turn the visibility of all the labels to False apart from the label that is currently invisible, which I want to make visible.
Instead of typing "lbl.Visible = False" 36 times, is there a quick way to do it?
Or a simple loop?
Thanks
-
Aug 10th, 2012, 04:23 PM
#2
Re: Hide everything on a form?
which version of vb are you using?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 10th, 2012, 04:25 PM
#3
Thread Starter
Lively Member
Re: Hide everything on a form?
-
Aug 10th, 2012, 04:27 PM
#4
Re: Hide everything on a form?
in vb 2010:
vb.net Code:
Array.ForEach(Me.Controls.OfType(Of Label).ToArray, Sub(lbl) lbl.Visible = Not lbl.Visible)
vb2008:
vb.net Code:
For Each lbl as label in Me.Controls.OfType(Of Label).ToArray lbl.Visible = Not lbl.Visible next
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 10th, 2012, 04:28 PM
#5
Re: Hide everything on a form?
I know where .Paul is going, and I won't jump in on that one. One question I would ask is this: Would it be correct to assume that the one that you want to make visible is going to change? In other words, the 36 that will toggle will not always be the same 36? If it is always the same 36, I would suggest a different solution from what .Paul. is going to suggest.
By the way, he was asking which version of VB.NET. Since you posted in this forum, .NET is pretty much assumed. However, there are things you can do if you are targeting at least framework 3.5, which are not available in earlier versions of the framework.
EDIT: I wasn't fast enough with this reply.
My usual boring signature: Nothing
 
-
Aug 10th, 2012, 04:35 PM
#6
Re: Hide everything on a form?
Try this:
vb Code:
Array.ForEach(Me.Controls.OfType(Of Label).ToArray, Sub(lbl) lbl.Visible = If(lbl.Visible, False, True))
EDIT: Looks as though I was not fast enough either for this reply, but .NET version is what determines the availability of LINQ @OP.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 10th, 2012, 04:45 PM
#7
Thread Starter
Lively Member
Re: Hide everything on a form?
Sorry, I though VB.net was just VB.net. I am using vb 2010 with .net framework 4.
I didn't think it would matter to start with but I have 37 labels, between 30 and 36 labels will be visible at the time and I want to make them all invisible apart from label 37.
Shaggy Hiker - Label 37 will be the the one made visible every time as it will just say "You won!" (I am making a hangman game to improve my skills).
So .Paul your method is correct for what I originally stated but for the 6 labels that could be visible or could be invisible, if they are invisible then it makes them visible which I do not want.
I guess I can use your method and just hard code the last 6 labels (it will be the same six labels each time) to be invisible?
-
Aug 10th, 2012, 04:48 PM
#8
Re: Hide everything on a form?
to exclude Label1, Label2, Label3:
vb.net Code:
Array.ForEach(Me.Controls.OfType(Of Label).Except(new Label(){Label1, Label2, Label3}).ToArray, Sub(lbl) lbl.Visible = Not lbl.Visible)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 10th, 2012, 05:17 PM
#9
Thread Starter
Lively Member
Re: Hide everything on a form?
-
Aug 10th, 2012, 05:36 PM
#10
Re: Hide everything on a form?
If you have two sets of labels, and one set should be visible while the other is not, or vice versa, then there is a much simpler solution: Put all the labels on each set onto a panel or other container. Then you just need to toggle the visibility of the container rather than toggling the visibility of the individual controls. Of course, this is only going to work really well if the controls of each set are in some rectangular extent, whereas toggling the visibility of the individual controls will work for any other type of arrangement.
My usual boring signature: Nothing
 
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|