Share via


MatchFound、MatchRequired 屬性、Change 事件、CheckBox 控制項範例

下列範例會使用 MatchFoundMatchRequired 屬性來示範 ComboBox的其他字元比對。 比對驗證發生在 Change 事件中。

在本範例中,使用者指定 ComboBox 的文字框架是否必須符合 ComboBox 中列出的項目之一。 使用者可以使用 CheckBox指定是否需要比對,然後輸入 ComboBox 以從其清單中指定專案。

若要使用本範例,請將此範例程式碼複製到表單的宣告部分中。 請確定該表單包含:

  • 一個名為 ComboBox1 的 ComboBox
  • 一個名為 CheckBox1 的 CheckBox
Private Sub CheckBox1_Click() 
 If CheckBox1.Value = True Then 
 ComboBox1.MatchRequired = True 
 MsgBox "To move the focus from the " _ 
 & "ComboBox, you must match an entry in " _ 
 & "the list or press ESC." 
 Else 
 ComboBox1.MatchRequired = False 
 MsgBox " To move the focus from the " _ 
 & "ComboBox, just tab to or click " _ 
 & "another control. Matching is optional." 
 End If 
End Sub 
 
Private Sub ComboBox1_Change() 
 If ComboBox1.MatchRequired = True Then 
 'MSForms handles this case automatically 
 Else 
 If ComboBox1.MatchFound = True Then 
 MsgBox "Match Found; matching optional." 
 Else 
 MsgBox "Match not Found; matching " _ 
 & "optional." 
 End If 
 End If 
End Sub 
 
Private Sub UserForm_Initialize() 
Dim i As Integer 
 
For i = 1 To 9 
 ComboBox1.AddItem "Choice " & i 
Next i 
ComboBox1.AddItem "Chocoholic" 
 
CheckBox1.Caption = "MatchRequired" 
CheckBox1.Value = True 
End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應