Enable/Disable 功能区独立控制
Enable/Disable Ribbon Controls Independently
我一直在寻找解决方案很多次,最接近的解决方案是 Ron de Bruin example,但它没有涵盖我所需要的。
我想做的基本上是两件事
示例:我有 4 个按钮。
Group1 和 tab1 上的 Button1
Group1 和 tab1 上的按钮 2
Group2 和 tab2 上的按钮 3
Group2 和 tab2 上的 Button4
单击功能区控件 "Button1" 时,它将 运行 一些代码并在最后禁用 "Button1"。
当单击功能区控件 "Button2" 时,它将 运行 一些代码并检查 "Button3" 和 "button4" 并设置相反的属性.
如果 button3 为真,则变为假。
如果 button4 为假,则变为真。
你能提供一些解决这个问题的指导吗
好的,所有按钮都有 GetEnabled
事件,所以当功能区 activates/updates - 事件被触发! (简单)。
此事件的回调函数如下所示:
Sub Button_GetEnabled(control As IRibbonControl, ByRef enabled)
'(enabled = true to enable)
enabled = EnableButtons
End Sub
那么让我们开始吧!在带有回调函数的模块中,您需要一个全局(全局到回调)布尔值,如 EnableButtons
.
当功能区加载时,此代码示例触发设置标志为 True
:
Private Sub OnRib_Load(ribbonUI As IRibbonUI)
Set MyRibbonUI = ribbonUI
EnableButtons = True
End Sub
并且在每个按钮上,您都需要为上述 GetEnabled
事件回调。
之后 - 所有按钮都已启用!那么我们可以在这里做什么?让我们看看您想要的按钮的 OnAction 回调:
Sub Button_Click(control As IRibbonControl)
EnableButtons = False
MyRibbonUI.Invalidate
'do some stuff - buttons disabled
EnableButtons = True
MyRibbonUI.Invalidate
End Sub
所以Invalidate
方法"updates"所有控件。您可以尝试 InvalidateControl
所需的控制(由于性能,这是比 Invalidate
更可取的方式),但我认为更优雅的方式是仅将回调和事件放置在您想要的按钮上!
所以,最后,您需要参考功能区、布尔标志和 _GetEnabled
个事件。
Commonsense 提供的解释帮助我为修改后的问题构建最终解决方案。谢谢
Option Explicit
Public MyRibbonUI As IRibbonUI
Public EnableButton1 As Boolean
Public EnableButton2 As Boolean
Public EnableButton3 As Boolean
Public EnableButton4 As Boolean
Public Sub OnRib_Load(ribbon As IRibbonUI)
'
' Code for onLoad callback. Ribbon control customUI
'
Set MyRibbonUI = ribbon
EnableButton1 = True
EnableButton2 = True
EnableButton3 = True
EnableButton4 = False
End Sub
Public Sub Button_GetEnabled(control As IRibbonControl, ByRef Enabled)
'
' Code for getEnabled callback. Ribbon control button
'
Select Case control.ID
Case "Button1"
Enabled = EnableButton1
Case "Button2"
Enabled = EnableButton2
Case "Button3"
Enabled = EnableButton3
Case "Button4"
Enabled = EnableButton4
End Select
End Sub
Public Sub Button1_onAction(control As IRibbonControl)
'
' Code for onAction callback. Ribbon control button
'
EnableButton1 = False
MyRibbonUI.InvalidateControl ("Button1")
End Sub
Public Sub Button2_onAction(control As IRibbonControl)
'
' Code for onAction callback. Ribbon control button
'
If EnableButton3 = False Then
EnableButton3 = True
Else
EnableButton3 = False
End If
MyRibbonUI.InvalidateControl ("Button3")
If EnableButton4 = False Then
EnableButton4 = True
Else
EnableButton4 = False
End If
MyRibbonUI.InvalidateControl ("Button4")
End Sub
Public Sub Button3_onAction(control As IRibbonControl)
'
' Code for onAction callback. Ribbon control button
'
End Sub
Public Sub Button4_onAction(control As IRibbonControl)
'
' Code for onAction callback. Ribbon control button
'
End Sub
我一直在寻找解决方案很多次,最接近的解决方案是 Ron de Bruin example,但它没有涵盖我所需要的。
我想做的基本上是两件事
示例:我有 4 个按钮。
Group1 和 tab1 上的 Button1
Group1 和 tab1 上的按钮 2
Group2 和 tab2 上的按钮 3
Group2 和 tab2 上的 Button4
单击功能区控件 "Button1" 时,它将 运行 一些代码并在最后禁用 "Button1"。
当单击功能区控件 "Button2" 时,它将 运行 一些代码并检查 "Button3" 和 "button4" 并设置相反的属性.
如果 button3 为真,则变为假。
如果 button4 为假,则变为真。
你能提供一些解决这个问题的指导吗
好的,所有按钮都有 GetEnabled
事件,所以当功能区 activates/updates - 事件被触发! (简单)。
此事件的回调函数如下所示:
Sub Button_GetEnabled(control As IRibbonControl, ByRef enabled)
'(enabled = true to enable)
enabled = EnableButtons
End Sub
那么让我们开始吧!在带有回调函数的模块中,您需要一个全局(全局到回调)布尔值,如 EnableButtons
.
当功能区加载时,此代码示例触发设置标志为 True
:
Private Sub OnRib_Load(ribbonUI As IRibbonUI)
Set MyRibbonUI = ribbonUI
EnableButtons = True
End Sub
并且在每个按钮上,您都需要为上述 GetEnabled
事件回调。
之后 - 所有按钮都已启用!那么我们可以在这里做什么?让我们看看您想要的按钮的 OnAction 回调:
Sub Button_Click(control As IRibbonControl)
EnableButtons = False
MyRibbonUI.Invalidate
'do some stuff - buttons disabled
EnableButtons = True
MyRibbonUI.Invalidate
End Sub
所以Invalidate
方法"updates"所有控件。您可以尝试 InvalidateControl
所需的控制(由于性能,这是比 Invalidate
更可取的方式),但我认为更优雅的方式是仅将回调和事件放置在您想要的按钮上!
所以,最后,您需要参考功能区、布尔标志和 _GetEnabled
个事件。
Commonsense 提供的解释帮助我为修改后的问题构建最终解决方案。谢谢
Option Explicit
Public MyRibbonUI As IRibbonUI
Public EnableButton1 As Boolean
Public EnableButton2 As Boolean
Public EnableButton3 As Boolean
Public EnableButton4 As Boolean
Public Sub OnRib_Load(ribbon As IRibbonUI)
'
' Code for onLoad callback. Ribbon control customUI
'
Set MyRibbonUI = ribbon
EnableButton1 = True
EnableButton2 = True
EnableButton3 = True
EnableButton4 = False
End Sub
Public Sub Button_GetEnabled(control As IRibbonControl, ByRef Enabled)
'
' Code for getEnabled callback. Ribbon control button
'
Select Case control.ID
Case "Button1"
Enabled = EnableButton1
Case "Button2"
Enabled = EnableButton2
Case "Button3"
Enabled = EnableButton3
Case "Button4"
Enabled = EnableButton4
End Select
End Sub
Public Sub Button1_onAction(control As IRibbonControl)
'
' Code for onAction callback. Ribbon control button
'
EnableButton1 = False
MyRibbonUI.InvalidateControl ("Button1")
End Sub
Public Sub Button2_onAction(control As IRibbonControl)
'
' Code for onAction callback. Ribbon control button
'
If EnableButton3 = False Then
EnableButton3 = True
Else
EnableButton3 = False
End If
MyRibbonUI.InvalidateControl ("Button3")
If EnableButton4 = False Then
EnableButton4 = True
Else
EnableButton4 = False
End If
MyRibbonUI.InvalidateControl ("Button4")
End Sub
Public Sub Button3_onAction(control As IRibbonControl)
'
' Code for onAction callback. Ribbon control button
'
End Sub
Public Sub Button4_onAction(control As IRibbonControl)
'
' Code for onAction callback. Ribbon control button
'
End Sub