使用 Visual Basic 中的 [選取] 屬性來判斷是否已選取項目。 讀取/寫入的 Long。
語法
表達。選取 (lRow)
詞 代表 Control 物件的變數。
參數
| 名稱 | 必要/選用 | 資料類型 | 描述 |
|---|---|---|---|
| l行 | 必要 | Long | 清單方塊中的項目。 第一個項目以零 (0) 表示,第二個項目以一 (1) 表示,依此類推。 |
註解
Selected 屬性是以零為起始的陣列,包含在清單方塊中每個項目的選取的狀態。
| 設定 | 描述 |
|---|---|
| True | 已選取清單方塊項目 |
| False | 沒有選取清單方塊項目 |
此屬性只有在執行階段才能使用。
當清單方塊控制項的 MultiSelect 屬性設定為 None 時,只有一個項目可以有其 Selected 屬性設定為 True 。 當清單方塊控制項的 MultiSelect 屬性設為簡單或延伸時,任一或所有項目可以有其 Selected 屬性設定為 True 。 多重選擇清單方塊繫結至欄位永遠必須等於 Null值 屬性。 您可以使用 Selected 屬性或 ItemsSelected 集合擷取選取項目相關的資訊。
使用 [選取] 屬性,使用 Visual Basic 選取清單方塊中的專案。 例如:下列運算式選取在清單之中的第五個項目:
Me!Listbox.Selected(4) = True
範例
下列範例會使用 Selected 屬性,將 lstSource 清單方塊中的選取專案移至 lstDestination 清單方塊。 lstDestination 清單方塊的 RowSourceType 屬性會設定為 Value List,而控制項的 RowSource 屬性是從 lstSource 控制項中的所有選取專案建構。 lstSource 清單方塊的 MultiSelect 屬性會設定為 Extended。 CopySelected ( ) 程式會從 cmdCopyItem 命令按鈕呼叫。
Private Sub cmdCopyItem_Click()
CopySelected Me
End Sub
Public Sub CopySelected(ByRef frm As Form)
Dim ctlSource As Control
Dim ctlDest As Control
Dim strItems As String
Dim intCurrentRow As Integer
Set ctlSource = frm!lstSource
Set ctlDest = frm!lstDestination
For intCurrentRow = 0 To ctlSource.ListCount - 1
If ctlSource.Selected(intCurrentRow) Then
strItems = strItems & ctlSource.Column(0, _
intCurrentRow) & ";"
End If
Next intCurrentRow
' Reset destination control's RowSource property.
ctlDest.RowSource = ""
ctlDest.RowSource = strItems
Set ctlSource = Nothing
Set ctlDest = Nothing
End Sub
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。