Groups | Blog | Home
all groups > dotnet setup > october 2005 >

dotnet setup : adding Visual Studio's ToolBox item by code problem


Slawek Piotrowski
10/21/2005 5:48:06 AM
Hello,


I'm writing C# code and executing it using InstallUtilLib.dll from custom
action during an installation process. It works fine.

I've written some code to add an item (component) to Visual Studio .NET 2003
toolbox and I've got some serious problems with it.

Problem looks like this:
1) my code works if Visual Studio is already opened when my code executes.
2) my code works fine when executed outside of an installation project.
3) if my code is executed from an installation project and Visual Studio is
not opened, then ToolBox item is not added. I've added a "MessageBox" during
the process and I can confirm that devenv.exe *is* executed.



Here is a function used to get access to Visual Studio's process:

bool OpenVisualStudioHandle(out EnvDTE.DTE dteObject)
{
// sprobuj uzyskac dostep do wczesniej otwartego okna Visual Studio .NET 2003
try
{
dteObject = (EnvDTE.DTE)
System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.7.1");
}
catch
{
dteObject = null;
}
if (dteObject != null) return false;
// nie ma otwartego okna z Visual Studiem - otworz nowe
dteObject = (EnvDTE.DTE)
Microsoft.VisualBasic.Interaction.CreateObject("VisualStudio.DTE.7.1", "");
return true;
}




And here is the code:

void CreateComponentInVisualStudioToolBox()
{
EnvDTE.DTE dteObject;
bool dteObjectCreated = OpenVisualStudioHandle(out dteObject);
if (dteObject == null) return;
try
{
EnvDTE.Window ToolBoxWnd =
dteObject.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
EnvDTE.ToolBoxTab tlbTab = GetToolBoxTabByName(ToolBoxWnd, "Components");
if (tlbTab != null)
{
// pokaz okno ToolBox (mozna dokonywac zmian tylko, jesli okno jest
widoczne)
ToolBoxWnd.Visible = true;
// wybierz zakladke "Components"
tlbTab.Activate();
EnvDTE.ToolBoxItems tlbItems = tlbTab.ToolBoxItems;
// dopisz siebie do toolboxa
string DllFullPath =
System.Reflection.Assembly.GetExecutingAssembly().Location;
tlbItems.Add("CodeCityReaderPort", DllFullPath,
EnvDTE.vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);
}
}
finally
{
// zamknij Visual Studio, jesli na poczatku je otworzyles
if (dteObjectCreated)
{
dteObject.Quit();
}
}
}



--
Sławomir Piotrowski / Telsat GP
Rejestracja Czasu Pracy i Kontrola Dostępu
http://www.ewidencja-czasu-pracy.pl
Marc
11/11/2005 9:34:17 AM

Hello Slawek,

Before the line:
tlbTab.Activate();

add:
EnvDTE.DTE.ExecuteCommand("View.PropertiesWindow", "");

There's no hugely rational reason for that but it has worked for me an
looking around it is generally acknowledged as a quirky step required b
the tooltab code in NET.

Regards,
Mar

--
Mar
-----------------------------------------------------------------------
Marc's Profile: http://www.hightechtalks.com/m24
View this thread: http://www.hightechtalks.com/t226666
AddThis Social Bookmark Button