Userform: Set image to visible based on selected listbox item

bluesummers

New Member
Joined
Sep 25, 2013
Messages
4
Hello all,

I have a small UI where I would like to display an image based on what item is selected in a listbox.

The listbox is multi-coulumn, and the data I want is from the 2nd Column, so I pull the value from the 2nd column and save it in a variable.

The idea is that I have each image pulled in and laid on top of each other. When the ListBox1 is clicked, then it will change the corresponding image's visibility to true, while changing all others to false.

The problem i have is that I cannot think of a way to change the image visibility back to false when a different item is selected from ListBox1.

The code is:

Code:
Private Sub ListBox1_Click()
    Dim i As Integer
    Dim grabbedListItem As String
    
'    Grabs the selected item from ListBox1's 2nd column and stores it in grabbedListItem
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) Then
            grabbedListItem = ListBox1.Column(1, Me.ListBox1.ListIndex)
        End If
    Next i
    
'    Not static enough. Need images packaged with code, keeping for reference
'    imgDisplay.Picture = LoadPicture("C:\TEST\" & grabbedList & ".jpg")
    
   
    If grabbedListItem = "HCR21" Then
       imgHCR21.Visible = True
       imgNO_IMAGE.Visible = False
    ElseIf grabbedListItem = "HCR22" Then
       imgHCR22.Visible = True
    ElseIf grabbedListItem = "HA2S3" Then
        imgHA2S3.Visible = True
    ElseIf grabbedListItem = "HPB11" Then
        imgHPB11.Visible = True
    ElseIf grabbedListItem = "HPB12" Then
        imgHPB12.Visible = True
    ElseIf grabbedListItem = "HPB11_BOX" Then
        imgHPB11_BOX.Visible = True
    Else
        imgNO_IMAGE.Visible = True
    End If


End Sub

Does anyone have a suggestion or know of a good way to do this?

Thanks a bunch!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Does this work for you?

Code:
Private Sub ListBox1_Click()
    Dim i As Integer
    Dim grabbedListItem As String
    Dim Ctrl As Control
'   Grabs the selected item from ListBox1's 2nd column and stores it in grabbedListItem
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) Then
            grabbedListItem = ListBox1.Column(1, Me.ListBox1.ListIndex)
        End If
    Next i
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "Image" Then
            Ctrl.Visible = Ctrl.Name = "img" & grabbedListItem
        End If
    Next Ctrl
End Sub
 
Upvote 0
Andrew,

That worked perfectly! Being new to excel and VB, I knew there was something that I just didn't know existed that would help. The code is working, and I will read up on the "Control" variable type more!

Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top