Search code examples
c#visual-studio-2013envdtevs-extensibility

Get properties from Properties window


I'm creating a Visual Studio add-in and when I select data connection node in Server explorer window (or data table or data field) is there a way to get property values from Properties window shown in visual studio using EnvDTE?

I need to get these values from these fields: Connection string, Provider, Data type, Is Identity, etc.

thnx in advance


Solution

  • Here is some sample code that demonstrate how to access the selection in the property grid. Note there can be multiple objects selected, not only one:

    IVsMonitorSelection selection = (IVsMonitorSelection)yourSite.GetService(typeof(SVsShellMonitorSelection)); // or yourPackage.GetGlobalService
    IVsMultiItemSelect ms;
    IntPtr h;
    IntPtr pp;
    uint itemid;
    
    selection.GetCurrentSelection(out h, out itemid, out ms, out pp);
    if (pp != IntPtr.Zero)
    {
        try
        {
            ISelectionContainer container = Marshal.GetObjectForIUnknown(pp) as ISelectionContainer;
            if (container != null)
            {
                uint count;
                container.CountObjects((uint)Microsoft.VisualStudio.Shell.Interop.Constants.GETOBJS_SELECTED, out count);
                if (count == 1)
                {
                    object[] objs = new object[1];
                    container.GetObjects((uint)Microsoft.VisualStudio.Shell.Interop.Constants.GETOBJS_SELECTED, 1, objs);
                    object selection = objs[0]; // selection is here
                }
            }
        }
        finally
        {
            Marshal.Release(pp);
        }
    }