设置组合框中的条目匹配Set Entry Matching in a Combo Box

下面的示例使用**MatchFound** 和**MatchRequired** 属性演示**ComboBox** 的其他字符匹配。The following example uses the MatchFound and MatchRequired properties to demonstrate additional character matching for ComboBox. 匹配验证发生在 Change 事件中。The matching verification occurs in the Change event.

在此示例中,用户指定 ComboBox 的文本部分是否必须与 ComboBox 中的列出项之一相匹配。In this example, the user specifies whether the text portion of a ComboBox must match one of the listed items in the ComboBox. 用户可以指定是否需要使用**复选框** 进行匹配,然后在ComboBox中键入以指定其列表中的项。The user can specify whether matching is required by using a CheckBox and then type into the ComboBox to specify an item from its list.

若要使用本示例,请将此示例代码复制到窗体的"脚本编辑器"中。To use this example, copy this sample code to the Script Editor of a form. 若要运行本代码,需要打开该窗体,以便激活 Open 事件。To run the code you need to open the form so the Open event will activate. 确保该窗体包含:Make sure that the form contains:

  • 一个名为"ComboBox1"的 ComboBox ,绑定到"主题"字段。A ComboBox named ComboBox1 that is bound to the Subject field.

  • 一个名为"CheckBox1"的 CheckBoxA CheckBox named CheckBox1.

Sub CheckBox1_Click() 
 Set ComboBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ComboBox1") 
 Set CheckBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("CheckBox1") 
 
 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 
 
Sub Item_PropertyChange(byval pname) 
 if pname = "Subject" then 
 Set ComboBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ComboBox1") 
 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 if 
End Sub 
 
Sub Item_Open() 
 Set ComboBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ComboBox1") 
 Set CheckBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("CheckBox1") 
 
 For i = 1 To 9 
 ComboBox1.AddItem "Choice " & i 
 Next 
 ComboBox1.AddItem "Chocoholic" 
 
 CheckBox1.Caption = "MatchRequired" 
 CheckBox1.Value = True 
End Sub

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.