Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

VS.Net Automation and Selected Controls - no fun at all!


:P
On this page:

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.

 

 


The Voices of Reason


 

Bob Archer
June 28, 2004

# re: VS.Net Automation and Selected Controls - no fun at all!

Rick,

I highly recommend Inside Visual Studio.Net... 2/3 of the book is dedicated to extending the IDE through macros and add-ins.

BOb

Rick Strahl
June 30, 2004

# re: VS.Net Automation and Selected Controls - no fun at all!

As it turns out the problem here is that macros don't work for this, but if I build a full blown add-in for it, it does. I'll have more on this in a few days - this process is definitely very tedious and requires an overview understanding of the full process.

anonymouse
July 20, 2004

# re: VS.Net Automation and Selected Controls - no fun at all!

Hi Rick,

My experience is the same as yours - you can do behind the scenes stuff with Macros, even show an inputbox dialog, but as soon as you get anywhere near forms then forget it. I think it might have something to do with how Visual Studio macros work - they sort of act on Visual Studio from the outside so to speak; whereas, when you create an add-in, one of the first things you do is hook into the process/event models. Plus that shim business...

All I wanted to do was quickly create a form which would iterate through a class and allow me to quickly jump between regions and functions, but regions are only exposed through the editor - not through reflection which would have been a nice way to do this kind of thing.

Still, I got to build an Add-In, show off my RegEx skills, and code the thing in C#! Not that I'm being dismissive of VB here - I just find that when I'm mixing it up a bit, I get told off by the Env for writing stuff like:

Dim newObj As Object;

(When everyone knows it should be:

Dim newObj As Object';

:-)
)

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024