问题
Back in the old days of VBA, we used to be able to directly access controls on a form by name. I'm a little perplexed by how to do this in VBA 2010 (or if it's even possible any longer).
Say I have a ribbon control with a dropdown list called "channelList", and elsewhere on the ribbon I have a textbox called "labelText". Each of these items has a unique callback function - when I type something into labelText, its callback fires, and when I select an item from the channelList listbox, its callback fires with the list item passed as an argument.
Where I'm stumped by is how I would update the labelText textbox contents with 'something' from within the channelList callback.
Is there a way to directly access the textbox control from within the listbox callback, or must I generate some sort of event? What method or function would I use to set the text value for the control? (Do I need to cast the IRibbonControl object to something?)
回答1:
The solution was a combination of an answer and the comments, so here goes:
- Add a "getText" callback subroutine name to the Ribbon XML, specific to the "labelText" editbox.
editBox id="labelText" label="Text:" sizeString="xxxxxxxxxx" onChange="TextboxCallback" getText="UpdateTextBoxText" screentip="Channel label" supertip="Limited to 10 characters. Press Enter once finished typing."
- In the combo box callback function, set the desired editbox text to a global and call Ribbon.InvalidateControl with "labelText" as the argument.
MyRibbon.InvalidateControl "labelText"
- Implement the editbox callback, passing a handle to the ribbon and another variable ByRef to contain the 'stuff' to be updated. Use the global to update the control's text via the ByRef argument.
Sub UpdateTextBoxText(control As IRibbonControl, ByRef returnedVal) Select Case (control.id) Case "labelText" returnedVal = LabelText End Select End Sub
I guess this is "progress". labelText.Value = "blah" is far too simple.
回答2:
Take a look at the Ribbon.Invalidate
method. When you "invalidate" the ribbon you basically reset it, and in that moment you can set the properties of controls on the ribbon based on whatever you can track in that moment. In order to do so, you need to have Control.GetEnabled
, GetVisible
, etc., subroutines in your VBA.
You can also invalidate individual controls with Ribbon.InvalidateControl
.
Here's a link that might help: http://sourcedaddy.com/ms-excel/resetting-controls.html
来源:https://stackoverflow.com/questions/18004053/updating-contents-of-an-iribbon-control-from-another-controls-callback