vba 独立启用/禁用功能区控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41166091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable/Disable Ribbon Controls Independently
提问by Jean-Marc Flamand
I've been searching many times for a solution to this and the closest solution is Ron de Bruin examplebut it does not cover what I need.
我一直在寻找解决方案很多次,最接近的解决方案是 Ron de Bruin示例,但它没有涵盖我需要的内容。
I am trying to do is essentially 2 things
我想做的基本上是两件事
Example: I have 4 buttons.
示例:我有 4 个按钮。
Button1 on Group1 and tab1
Group1 和 tab1 上的 Button1
Button2 on Group1 and tab1
Group1 和 tab1 上的 Button2
Button3 on Group2 and tab2
Group2 和 tab2 上的 Button3
Button4 on Group2 and tab2
Group2 和 tab2 上的 Button4
When the ribbon control "Button1" is clicked it will run some code and disable the "Button1" at the end.
When the ribbon control "Button2" is clicked it will run some code and the check the "Button3" and "button4" and set the opposite properties.
单击功能区控件“Button1”时,它将运行一些代码并在最后禁用“Button1”。
单击功能区控件“Button2”时,它将运行一些代码并检查“Button3”和“button4”并设置相反的属性。
If button3 was true, it becomes false.
如果 button3 为真,则变为假。
If button4 was false, it becomes true.
如果 button4 为假,则变为真。
Can you provide some guidance to solve this
你能提供一些指导来解决这个问题吗
回答by CommonSense
Ok, all buttons have GetEnabled
events, so when ribbon activates/updates - event fired! (simple).
好的,所有按钮都有GetEnabled
事件,所以当功能区激活/更新时 - 事件触发!(简单的)。
Callback function to this event looks like this:
此事件的回调函数如下所示:
Sub Button_GetEnabled(control As IRibbonControl, ByRef enabled)
'(enabled = true to enable)
enabled = EnableButtons
End Sub
So lets start! In your module with callbacks functions you need a global (global to callbacks) boolean smth like EnableButtons
.
那么让我们开始吧!在具有回调函数的模块中,您需要一个全局(全局到回调)布尔值,例如EnableButtons
.
And when ribbon loads this code example fires setting flag to True
:
当功能区加载此代码示例时,将设置标志设置为True
:
Private Sub OnRib_Load(ribbonUI As IRibbonUI)
Set MyRibbonUI = ribbonUI
EnableButtons = True
End Sub
And on each button you need callback for GetEnabled
event described above.
在每个按钮上,您都需要对GetEnabled
上述事件进行回调。
After that - all buttons are enabled! So what we can make here? Lets look on OnAction callback to your desired button:
之后 - 所有按钮都已启用!那么我们可以在这里做什么?让我们看看你想要的按钮的 OnAction 回调:
Sub Button_Click(control As IRibbonControl)
EnableButtons = False
MyRibbonUI.Invalidate
'do some stuff - buttons disabled
EnableButtons = True
MyRibbonUI.Invalidate
End Sub
So Invalidate
method "updates" all controls. You can try to InvalidateControl
desired control (which is a more preferable way, than Invalidate
, due to performance), but I think that more elegant way is to place callbacks and events only on buttons you want!
所以Invalidate
方法“更新”所有控件。您可以尝试进行InvalidateControl
所需的控制(这是一种更可取的方式,而不是Invalidate
,由于性能原因),但我认为更优雅的方式是仅将回调和事件放置在您想要的按钮上!
So, finally, you need reference to ribbon, boolean flag, and _GetEnabled
events.
因此,最后,您需要引用功能区、布尔标志和_GetEnabled
事件。
回答by Jean-Marc Flamand
Explantion provided by Commonsensehelp me to build that final solution for the revised question asked. Thanks
Explantion提供由常识帮我建立一个最终的解决方案,修订后的问题问。谢谢
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