案例: :我有一个CommandButton和一个窗体中的未绑定ComboBox(RowSourceType:值列表-使用VBA从另一窗体填充)。
目标: :当用户单击CommandButton时,将从此ComboBox中删除ComboBox中的选定项目。
尝试: :我在CommandButton的Click事件中使用了ComboBox的RemoveItem方法,该事件需要要删除的项目的索引。 为了获取所选项目的索引,我尝试使用ComboBox的Selected属性,循环遍历所有ComboBox项目,但无论选择如何,Selected属性始终返回0。
Private Sub bDelete_Click()
Dim i As Integer
For i = 0 To Me.cAnswered.ListCount - 1
If Me.cAnswered.Selected(i) = True Then
'MsgBox i
'Stop
Me.cAnswered.RemoveItem i
Exit For
End If
Next
Me.bDelete.Visible = (Me.cAnswered.ListCount > 0)
End Sub
能否请您告诉我如何实现此目标?
答案 0 :(得分:2)
尝试一下
Private Sub bDelete_Click()
Dim i As Integer
For i = 0 To Me.cAnswered.ListCount - 1
If Me.cAnswered.ItemData(i) = cAnswered.Value Then
Me.cAnswered.RemoveItem i
Exit For
End If
Next
cAnswered = Null
End Sub