Reputation: 6510
How to add my right click menus in SSMS 2008R2\2012 Object Explorer? I researched this topic.
I do this way:
private void Provider_SelectionChanged(object sender, NodesChangedEventArgs args)
{
INodeInformation[] nodes;
int nodeCount;
objectExplorer.GetSelectedNodes(out nodeCount, out nodes);
INodeInformation node = (nodeCount > 0 ? nodes[0] : null);
if (_databaseMenu == null &&
_databaseRegex.IsMatch(node.Context))
{
_databaseMenu = (HierarchyObject)node.GetService(typeof(IMenuHandler));
_databaseMenu.AddChild(string.Empty, new MenuItem());
}
}
BUT the problem is: if I do left click on database and then right click - I see my menu, ok. If I expand the object tree via (+) and then immediately right click on database - I do not see my menu. I understand why it is but how to solve this problem?
Upvotes: 1
Views: 515
Reputation: 6112
I spent a considerable amount of time working on this same issue for my own SSMS add-in. What I came up with is a dirty hack, but it was the only way I could find to get it working reliably.
You use SendKeys.SendWait
to issue SHIFT + F10, which is the shortcut to open the context menu, and you do it twice, since a single issuance will toggle the menu's state (visible to not or vice versa). The UI will stop responding and eventually throw if you use Send
, so be sure to use SendWait
.
There will be a slight delay on left click or flicker of the menu on right click. And, of course, this won't work if the user has altered that shortcut (or has defined external, superseding macros), but a quick glance through the SSMS options doesn't reveal any way to change the context menu shortcut.
private void Provider_SelectionChanged(object sender, NodesChangedEventArgs args)
{
INodeInformation[] nodes;
int nodeCount;
objectExplorer.GetSelectedNodes(out nodeCount, out nodes);
INodeInformation node = (nodeCount > 0 ? nodes[0] : null);
if (_databaseMenu == null &&
_databaseRegex.IsMatch(node.Context))
{
_databaseMenu = (HierarchyObject)node.GetService(typeof(IMenuHandler));
_databaseMenu.AddChild(string.Empty, new MenuItem());
SendKeys.SendWait("+({F10})")
SendKeys.SendWait("+({F10})")
}
}
Upvotes: 1