Toggle Visible Property

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Toggle Visible Property

Post by D Willett »

Hi. I have a series of Boxes (Shapes):
Box34
Box35
Box36
etc through to Box42.

They each sit under a label. I would like to set each visible property to true when the corresponding label is clicked and all others in the group to false.
This is to show the user which label he has clicked.
I know I could do this using vba for each label but I'm sure there is a quicker way using far less code.
Can any of you guys help?

Regards
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78378
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Toggle Visible Property

Post by HansV »

Make the names of the labels correspond to those of the boxes: Box34 > Label34, Box35 > Label35, ..., Box42 > Label42.

Create a single function in the form's code module:

Code: Select all

Private Function LabelClick(n As Long)
    Dim i As Long
    For i = 34 To 42
        Me.Controls("Box" & i).Visible = (i = n)
    Next i
End Function
In the On Click event of Label34, enter =LabelClick(34)
In the On Click event of Label35, enter =LabelClick(35)
etc.
S27.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Toggle Visible Property

Post by D Willett »

Hi Hans. Thanks for this, just a little issue here. I'm already using the "Click" event for some other code (as below):

Code: Select all

Private Sub Label24_Click()
Me.Box34.Visible = True

If Right(Me.OrderBy, 4) = "Desc" Then
    Me.OrderBy = "JobID"
Else
    Me.OrderBy = "JobID Desc"
End If
Me.OrderByOn = True
End Sub
So I can't use that event unless there is another way to achieve this?

Kind Regards
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78378
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Toggle Visible Property

Post by HansV »

Change the event procedure to

Code: Select all

Private Sub Label24_Click()
    Call LabelClick(34) ' use the number of the box associated with the label

    If Right(Me.OrderBy, 4) = "Desc" Then
    Me.OrderBy = "JobID"
    Else
        Me.OrderBy = "JobID Desc"
    End If
    Me.OrderByOn = True
End Sub
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Toggle Visible Property

Post by D Willett »

Works perfectly Hans.

Thanks again.
Kind Regards
Cheers ...

Dave.