RemoveItem 方法

从指定的列表框控件或组合框控件显示的值列表中,删除一个项。

expression.RemoveItem(Index)

expression     必需。返回“应用于”列表中的一个对象的表达式。

Index     必需 Variant 型。即将从列表中删除的项,用项目号或列表项文本表示。

说明

该方法仅对窗体上的列表框或组合框控件有效。而且,控件的 RowSourceType 属性必须设为“值列表”。

列表项编号从零开始。如果 Index 参数的值与现有项目编号或现有项的文本不对应,则会出错。

使用 AddItem 方法可以将项目添加到值列表中。

示例

该示例从列表框控件的列表中删除指定项。为使函数可以工作,必须为其传递一个代表窗体上列表框控件的 ListBox 对象及一个代表要删除的项的 Variant 值。

Function RemoveListItem(ctrlListBox As ListBox, _

        ByVal varItem As Variant) As Boolean

    ' Trap for errors.

    On Error GoTo ERROR_HANDLER

    ' Remove the list box item and set the return value

    ' to True, indicating success.

    ctrlListBox.RemoveItem Index:=varItem

    RemoveListItem = True

    ' Reset the error trap and exit the function.

    On Error GoTo 0

    Exit Function

' Return False if an error occurs.

ERROR_HANDLER:

    RemoveListItem = False

End Function