在组合框中设置条目匹配

以下示例使用 MatchFoundMatchRequired 属性来演示 ComboBox 的其他字符匹配。 匹配验证发生在 Change 事件中。

在此示例中,用户指定 ComboBox 的文本部分是否必须与 ComboBox 中的列出项之一相匹配。 用户可以使用 CheckBox 指定是否需要匹配,然后键入 ComboBox 以从其列表中指定项目。

若要使用本示例,请将此示例代码复制到窗体的"脚本编辑器"中。 若要运行本代码,需要打开该窗体,以便激活 Open 事件。 确保该窗体包含:

  • 一个名为"ComboBox1"的 ComboBox ,绑定到"主题"字段。

  • 一个名为"CheckBox1"的 CheckBox

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

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。