Frustration sets in today. I’ve finally decided I’m going to hook in Help Builder into VS.Net to provide the same functionality I’ve had in VFP for years to automatically populate Help Ids from a running instance of Help Builder. The idea is pretty simple: You select a control, you click a hotkey and Help Builder pops up you select your topic, click a button and the help Id (the HTML file) gets inserted.
Bonk – wake up call. Hooking things into the VS.Net IDE is a freaking nightmare of a mess of COM interface pointers that are barely documented. I spent the better half of a day today trying to hunt down some information on how to retrieve a selected control and although I found some code to do this I was actually unable to get it to work from a Macro at least.
First issue I’m still undecided on – what should the UI for this sort of thing be? The main feature should be a hotkeyable function so it’s easily accessible. Ideally a right click menu on the control would also be nice – Get Help Topic ID, but I shudder to think on what’s involved in making that happen in a context sensitive way. There are macros, there are add-ins, Wizards and designers. Hmmm... the latter two are pretty much out as I have no control over the control type, so i think I have to go either with a macro or add-in.
So far I’ve been using Macros to test this, mainly because it’s easier to work with. So as mentioned I found some code that is supposed to do this but fails at least in Macro operation:
Sub SelectedControl()
Dim WinForm As EnvDTE.SelectedItem
MessageBox.Show(DTE.SelectedItems.Count.ToString())
WinForm = DTE.SelectedItems.Item(1)
Dim hostWin As IDesignerHost
hostWin = CType(DTE.ActiveWindow.Object, IDesignerHost)
' get the right source file
MessageBox.Show(DTE.ActiveWindow.Document.Name)
Dim ServiceProvider As System.IServiceProvider
ServiceProvider = CType(hostWin, IServiceProvider)
' *** THIS FAILS ***
Dim sel As ISelectionService
sel = CType(hostWin.GetService(Type.GetType("System.ComponentModel.Design.ISelectionService")), _
System.ComponentModel.Design.ISelectionService)
Dim SelectionService As ISelectionService
SelectionService = _
CType(hostWin.GetService(GetType(ISelectionService)), _
ISelectionService)
Dim ctrl As Control
ctrl = SelectionService.PrimarySelection
MessageBox.Show(ctrl.Name)
End Sub
The above fails with an error that says QueryInterface failed on the IServiceProvider interface. Ok, that doesn’t help me much. I’m not sure where things go wrong because everything up to that point returns a COM pointer. I suspect the problem might be that hostWin is not actually pointing at the form’s designer correctly, but who knows? Au, my head hurts <g>. I thought I was done with COM interface programming, but now I remember why it’s taken me so long to come back to this.
Other Posts you might also like