我正在使用Excel Ribbon切换按钮。基于它的开/关状态,我在SheetSelectionChange事件的Excel单元格中进行一些处理。如果不访问我的设置文件,就无法从单元格获取toggleButton的状态,这是不必要的开销。我来介绍一下代码片段。
可以选择全局变量。但是我可以有更好的东西吗?
Private Sub mExcel_SheetSelectionChange(ByVal theSheet As Object, ByVal Target As Range)
'++
' Call the C++ Code.
'--
'If (isbtnpressed) Then ** <-- This is where i want to check the ToglgeButton status
' I don't want to read the settings file for every cell click. **
Dim flagVal As Boolean
'IsValidData goes to C++ for validation
If IsValidData(Target.Application.ActiveCell) Then
LoadMyListForm Target.Application.ActiveCell
End If
'End If
End Sub
onAction事件(两个)
Public Sub IxlRibbonGetButtonStateByTag(theControl As IRibbonControl, ByRef isbtnpressed)
'++
'Get ShowOnClick button state, callback for getPressed
'
' Arguments:
' theControl - The ribbon control that fired the OnAction request.
' IsPressed - Button status
'--
On Error Resume Next
isbtnpressed = (ReadFromSettingsFile (theControl.Tag, "0") <> "0")
End Sub
和
Public Sub IxlRibbonToggleSettingByTag(control As IRibbonControl, IsPressed As Boolean)
'++
'callback for onAction
'
' Arguments:
' theControl - The ribbon control that fired the OnAction request.
' IsPressed - Button status
'--
On Error Resume Next
If IsPressed Then
WriteToSettingsFile control.Tag, "1"
Else
WriteToSettingsFile control.Tag, "0"
End If
End Sub
UI XML看起来像
<toggleButton id="tb_IxlToggleButton"
label="My Label"
tag="MyLabel"
getPressed="IxlRibbonGetButtonStateByTag"
onAction="IxlRibbonToggleSettingByTag"
imageMso="ControlToggleButton" />
<button id="MyLabel"
tag="MyLabel"
onAction="IxlRibbonToggleSettingByTag"/>
我已经继续使用全局变量作为出路 @Pᴇʜ
在评论部分中建议
答案 0 :(得分:1)
如果不希望使用Public
变量,一种简便的方法是在注册表中保留和保留其状态。还可以根据存储的状态在应用程序启动时设置“切换”按钮:
在模块级别创建一些常量:
Public const MyApp as string = "MyApp Name"
Public const MyAppSett as string = "MyApp Settings"
Public const TglVal as string = "Toggle_Val"
将您的事件代码转换为:
Public Sub IxlRibbonToggleSettingByTag(control As IRibbonControl, IsPressed As Boolean)
SaveSetting MyApp, MyAppSett, TglVal, cStr(IsPressed)
End Sub
创建一个函数以快速返回状态(在注册表中读取):
Function TglStatus() As Boolean
Dim strToggle As String
strToggle = GetSetting(MyApp, MyAppSett, TglVal, "Nothing...")
If strToggle <> "Nothing..." Then
TglStatus = CBool(strToggle)
Else
TglStatus = False 'for the case before memorizing of something...
End If
End Function
并像这样使用它:
If TglStatus Then
'do something for true
Else
'do something else for false
End If