如何获得列表框所选项目的名称?

问题描述

如何在列表框上获得商品名称

我有代码

Dim x2 As Long
Dim OriginalCount2 As Long
'Store original ListBox count
  OriginalCount2 = ListBox1.ListCount
'Temporarily hide ListBox (runs faster)
  ListBox1.Visible = False
'Delete selected line items
  For x2 = OriginalCount2 - 1 To 0 Step -1
    If ListBox1.Selected(x2) = True Then MsgBox x2
  Next x2
'Unhide ListBox
  ListBox1.Visible = True

但是它只能获取商品索引。

解决方法

将有助于了解什么是事件或操作触发了代码,但这应该使您朝正确的方向入手:

Private Sub ListBox1_Click()
    Dim LBItem As Long
    For LBItem = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(LBItem) = True Then
            MsgBox (ListBox1.List(LBItem))
        End If
    Next
End Sub