Office VBA教程:Change事件

该事件发生于最终用户改变命令栏组合框中的选项时。

Private Sub CommandBarComboBox_Change

    (ByVal Ctrl As CommandBarComboBox)

说明

Change事件由 CommandBarComboBox对象识别。若要返回某个 CommandBarComboBox 控件的 Change事件,请用关键词 WithEvents 声明一个变量,并将其值设置为该 CommandBarComboBox 控件。当触发 Change事件时,将执行该控件的 OnAction属性所指定的宏或代码。

VBA示例

以下示例创建一个包含 CommandBarComboBox 控件的命令栏,该控件包含四个选项。该组合框通过 CommandBarComboBox_Change事件响应用户操作。


Private ctlComboBoxHandler As New ComboBoxHandler
Sub AddComboBox()
    Set HostApp = Application
            Dim newBar As Office.CommandBar
    Set newBar = HostApp.CommandBars.Add(Name:="Test CommandBar", Temporary:=True)
    Dim newCombo As Office.CommandBarComboBox
    Set newCombo = newBar.Controls.Add(msoControlComboBox)
    With newCombo
        .AddItem "First Class", 1
        .AddItem "Business Class", 2
        .AddItem "Coach Class", 3
        .AddItem "Standby", 4
        .DropDownLines = 5
        .DropDownWidth = 75
        .ListHeaderCount = 0
    End With
    ctlComboBoxHandler.SyncBox newCombo
    newBar.Visible = True
End Sub

上述示例使用以下代码,此段代码保存在 VBA 工程的类模块中。


Private WithEvents ComboBoxEvent As Office.CommandBarComboBox
Public Sub SyncBox(box As Office.CommandBarComboBox)
    Set ComboBoxEvent = box
    If Not box Is Nothing Then
        MsgBox "Synced " & box.Caption & " ComboBox events."
    End If
End Sub
Private Sub Class_Terminate()
    Set ComboBoxEvent = Nothing
End Sub
Private Sub ComboBoxEvent_Change(ByVal Ctrl As Office.CommandBarComboBox)
    Dim stComboText As String
    stComboText = Ctrl.Text
        Select Case stComboText
        Case "First Class"
            FirstClass
        Case "Business Class"
            BusinessClass
        Case "Coach Class"
            CoachClass
        Case "Standby"
            Standby
    End Select
End Sub
Private Sub FirstClass()
    MsgBox "You selected First Class reservations"
End Sub
Private Sub BusinessClass()
    MsgBox "You selected Business Class reservations"
End Sub
Private Sub CoachClass()
    MsgBox "You selected Coach Class reservations"
End Sub
Private Sub Standby()
    MsgBox "You chose to fly standby"
End Sub

上页:Office VBA教程:为 Microsoft Office 开发人员提供的新增功能 下页:Office VBA教程:Click事件

Office VBA教程:Change事件

Office VBA教程:Click事件 Office VBA教程:EnvelopeHide事件
Office VBA教程:EnvelopeShow事件 Office VBA教程:OnUpdate事件
Office VBA教程:新增事件 Office VBA教程:隐藏语言元素
Office VBA教程:创建和修改气球 Office VBA教程:获得有关 Macintosh 关键词的帮助
Office VBA教程:添加和管理菜单栏和菜单项 Office VBA教程:在运行时间修改气球
Office VBA教程:“Office 助手”概述 Office VBA教程:命令栏概述
Office VBA教程:返回集合中的对象 Office VBA教程:添加和显示快捷菜单
Office VBA教程:添加和修改工具栏 Office VBA教程:使用“Office 助手”
Office VBA教程:使用命令栏 Office VBA教程:无法使用帮助主题
Office VBA教程:OLE 编程标识符(ActiveX 控件) Office VBA教程:ActivateWizard方法
版权所有 © 中山市飞娥软件工作室 证书:粤ICP备09170368号