Results 1 to 10 of 10

Thread: Hide everything on a form?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    Question 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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Hide everything on a form?

    which version of vb are you using?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    Re: Hide everything on a form?

    Sorry, VB.Net

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Hide everything on a form?

    in vb 2010:

    vb.net Code:
    1. Array.ForEach(Me.Controls.OfType(Of Label).ToArray, Sub(lbl) lbl.Visible = Not lbl.Visible)

    vb2008:

    vb.net Code:
    1. For Each lbl as label in Me.Controls.OfType(Of Label).ToArray
    2.     lbl.Visible = Not lbl.Visible
    3. next

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    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

  6. #6
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Hide everything on a form?

    Try this:
    vb Code:
    1. 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.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    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?

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Hide everything on a form?

    to exclude Label1, Label2, Label3:

    vb.net Code:
    1. Array.ForEach(Me.Controls.OfType(Of Label).Except(new Label(){Label1, Label2, Label3}).ToArray, Sub(lbl) lbl.Visible = Not lbl.Visible)

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    67

    Re: Hide everything on a form?

    Thanks a lot!

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    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
  •  



Click Here to Expand Forum to Full Width