Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Can you get to the open project or solution files while in a Designer in Design mode
 

Can you get to the open project or solution files while in a Designer in Design mode

Hello all,

While running code inside of a component designer, is there any way to be able to reference the solution and/or project that is active for the current session? I have a form component and I would like to be able to reference the associated Xml Schema file which is in the project. Is there any way to do this? Thanks in advance for any ideas and/or suggestions!

Gary Winey  Wednesday, October 25, 2006 12:05 PM
i think you need to interact with EnvDTE
joeycalisay  Wednesday, October 25, 2006 1:15 PM

Here is a partial solution to maybe help someone else. When I tried to use the sample code from MSDN Referencing the DTE Object I got err 429, Operation unavailable.

The work arounds suggested by a Microsoft support article: PRB: Visual Basic .NET Error Using GetObject or GetActiveObject for Running Instance of Office did not work. While the article referenced VB.Net, its focus was on Office Automation. Here is the link: http://support.microsoft.com/kb/308409.

Because I am interacting with the DesignTime system, I need to use Services to get my envDTE reference. The following code works and is as far as I have gotten:

Private Sub GetVisualStudioDTE(ByVal component As System.ComponentModel.IComponent)
Try
Dim visualStudioIDE As EnvDTE.DTE = CType(GetService(GetType(EnvDTE.DTE)), EnvDTE.DTE)
If visualStudioIDE Is Nothing Then
Throw New InvalidOperationException("DTE not found.")
End If
Catch ex As Exception
Throw
End Try
End Sub 'GetVisualStudioDTE

Gary Winey  Thursday, November 02, 2006 12:51 AM

Just in case someone finds this thread and needs the RunTime equivalents, see below. Note that I get an error when trying to use Marshall.GetActiveObject with a generic ProgID.

VB

' Adapted from:
' HOWTO: Automating Visual Studio .NET from outside the IDE
' http://www.mztools.com/articles/2005/MZ005.htm

Imports EnvDTE
Imports System.Runtime.InteropServices

Private Const m_VISUAL_STUDIO_2002_PROG_ID As String = "VisualStudio.DTE.7"
Private Const m_VISUAL_STUDIO_2003_PROG_ID As String = "VisualStudio.DTE.7.1"
Private Const m_VISUAL_STUDIO_2005_PROG_ID As String = "VisualStudio.DTE.8.0"
Private Const m_VISUAL_STUDIO_GENERIC_PROG_ID As String = "VisualStudio.DTE"

' Code throws an error if generic prog id is used here.
Dim visualStudioIDE1 As EnvDTE.DTE = _
    Marshal.GetActiveObject(m_VISUAL_STUDIO_GENERIC_PROG_ID)

' Create an instance of the latest Visual Studio .NET IDE.
' Using a type and activator allows any version to be created.
Dim type As System.Type = _
    System.Type.GetTypeFromProgID(m_VISUAL_STUDIO_GENERIC_PROG_ID)
Dim visualStudioIDE2 = _
    DirectCast(System.Activator.CreateInstance(type), EnvDTE.DTE)

C#

using EnvDTE;
using System.Runtime.InteropServices;

// Adapted from:
// HOWTO: Automating Visual Studio .NET from outside the IDE
// http://www.mztools.com/articles/2005/MZ005.htm

private const string m_VISUAL_STUDIO_2002_PROG_ID = "VisualStudio.DTE.7";
private const string m_VISUAL_STUDIO_2003_PROG_ID = "VisualStudio.DTE.7.1";
private const string m_VISUAL_STUDIO_2005_PROG_ID = "VisualStudio.DTE.8.0";
private const string m_VISUAL_STUDIO_GENERIC_PROG_ID = "VisualStudio.DTE";

// Code throws an error if generic prog id is used here.
EnvDTE.DTE visualStudioIDE1 =
    Marshal.GetActiveObject(m_VISUAL_STUDIO_GENERIC_PROG_ID);

// Create an instance of the latest Visual Studio .NET IDE.
// Using a type and activator allows any version to be created.
System.Type type =
    System.Type.GetTypeFromProgID(m_VISUAL_STUDIO_GENERIC_PROG_ID);
object visualStudioIDE2 =
    ((EnvDTE.DTE)(System.Activator.CreateInstance(type)));

 

j2associates  Saturday, March 24, 2007 3:04 PM

You can use google to search for other answers

Custom Search

More Threads

• Correct way to "Close" a design surface?
• DataGridView Problem
• Adding images to DesignerVerbs
• panel anchor bug
• Scrolling user control
• inherit from scrollable control lets users put controls in it
• Unable to move the position of custom control during design time
• Setting the Default() value of a control property when its an Enum?
• Making the designer generate code
• Remove default ToolStripItems from ToolStrip Designer