Results 1 to 6 of 6

Thread: Hiding/showing a label when a listbox is hidden/shown

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    152

    Hiding/showing a label when a listbox is hidden/shown

    I've got some listboxes with labels on them and I want the label above each listbox to be hidden when the listbox is hidden. Right now I'm giving them matching tags and then just looping to search for the correct one:

    Code:
        'this subroutine covers concealing all listboxes
        Private Sub DisableListboxes(sender As Object, e As EventArgs) Handles Listbox1.EnabledChanged, _
            Listbox2.EnabledChanged, Listbox3.EnabledChanged
    
            'the name of the listbox that raised the sub
            Dim CurrentList As ListBox = DirectCast(sender, ListBox)
    
            'finds the label associated with that listbox
            Dim CurrentLabel As Label = Nothing
            For Each lb As Control In CurrentList.Parent.Controls
                If TypeOf lb Is Label Then
                    If CStr(lb.Tag) = CStr(CurrentList.Tag) Then
                        'it's the correct one
                        CurrentLabel = DirectCast(lb, Label)
                    End If
                End If
            Next lb
    
            If CurrentList.Enabled = False Then 
    
                'conceals the label
                CurrentLabel.Enabled = False
    
            Else
                'reveals the label
                CurrentLabel.Enabled = True
            End If
        End Sub
    There has to be a better way.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,375

    Re: Hiding/showing a label when a listbox is hidden/shown

    Just set the visibility based on the listbox's visibility:
    Code:
    <label>.Visibility = <listbox>.Visibility
    no need for loops.

    Edit - I just realized you're using the .Enabled property:
    Code:
    <label>.Enabled = <listbox>.Enabled
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Hiding/showing a label when a listbox is hidden/shown

    Several but the easiest is to put each listbox with its label on a separate panel and change the visibility of the panel thusly,

    vb.net Code:
    1. Private Sub DisableListboxes(sender As Object, e As EventArgs) Handles ListBox1.EnabledChanged, ListBox2.EnabledChanged 'etc.
    2.         If CType(sender, ListBox).Enabled = False Then
    3.             CType(sender, ListBox).Parent.Visible = False
    4.         Else
    5.             CType(sender, ListBox).Parent.Visible = True
    6.         End If
    7.     End Sub
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    152

    Re: Hiding/showing a label when a listbox is hidden/shown

    dday: The problem is that the subroutine is handling multiple listboxes and I need to find the label that goes with that particular listbox.

    dunfiddlin: That'll work. I thought about putting them in a groupbox but it would have been to cluttered; a panel will work well though.
    Last edited by colleen.boye; May 20th, 2013 at 03:12 PM.

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Hiding/showing a label when a listbox is hidden/shown

    Another way could be to use a dictionary.

    Some thing like

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private m_dictionary As New Dictionary(Of ListBox, Label) From
    4.                                           {{Me.ListBox1, Me.Label1},
    5.                                            {Me.ListBox2, Me.Label2},
    6.                                            {Me.ListBox3, Me.Label3}}
    7.  
    8.     Private Sub ListBox1_EnabledChanged(ByVal sender As System.Object,
    9.                                               ByVal e As System.EventArgs) Handles _
    10.                                               ListBox1.EnabledChanged,
    11.                                               ListBox2.EnabledChanged,
    12.                                               ListBox3.EnabledChanged
    13.         Dim lb As ListBox = DirectCast(sender, ListBox)
    14.         Dim l As Label = Me.m_dictionary(lb)
    15.  
    16.         l.Enabled = lb.Enabled
    17.     End Sub
    18.  
    19.  
    20. End Class

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Hiding/showing a label when a listbox is hidden/shown

    Say Label1 is paired with ListBox1

    In form load
    Code:
    Label1.DataBindings.Add("Visible", ListBox1, "Visible")
    Test it say in a button click event
    Code:
    ListBox1.Visible = Not ListBox1.Visible

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