adding Visual Studio's ToolBox item by code

G

Guest

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();
}
}
}
 
T

TerryFei

Hi,
Welcome to MSDN Newsgroup!

Based on the current situation, please help me to confirm some information
on your side:
1.What about your program, VS.NET Add-In or something else?

2. >>" My code works fine when executed outside of an installation project."
In this scenario, whether or not the VS2003 IDE is opened? If not, I want
to confirm how you run this code.

If you have any questions or concerns, please feel free to let me know.
I'm looing forward to you. :)

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Hi,
Welcome to MSDN Newsgroup!

Thank you for the reply, answers inline.


Based on the current situation, please help me to confirm some information
on your side:
1.What about your program, VS.NET Add-In or something else?

My program (in fact it's a class library) is a .NET component. It can be
used with or without visual designer. I want to add it to Visual Studio's
component tab in the ToolBox.


2. >>" My code works fine when executed outside of an installation project."
In this scenario, whether or not the VS2003 IDE is opened?

Well... I thought I've checked both ways, but now it looks like it works
only if Visual Studio .NET is opened when my code is executed.


If not, I want to confirm how you run this code.

In order to launch separately I compile it as a console application, define
Main function that creates an "Installer" object and directly executes
CreateComponentInVisualStudioToolBox.



In case it matters - I'm testing the code under Polish version of Windows XP
Pro with Service Pack 2 and all current patches installed. I'm using an
account with local admin privs.
 
T

TerryFei

Hi,

Based on your code, it will add items to an existed VS .Net Instance, so
it's necessary to make VS .NET opened for the code working. (This behavior
is as expected.)
If we want to add the items to the VS.NET 2003 IDE persistently after
installing once, the general convention is using a VS.NET add-in to achieve
the goal.

I browse some articles and hope it's helpful for you.
Title: Visual Studio .NET 2003 Automation Samples (including some add-in
samples)
URL:http://www.microsoft.com/downloads/details.aspx?familyid=3FF9C915-30E5-4
30E-95B3-621DCCD25150&displaylang=en

Title: Deploying Controls to VS.NET Toolbox Programatically
URL: http://www.codeproject.com/dotnet/AddItemToToolBar.asp
If you have any questions or concerns, please feel free to let me know.
Thanks for your understanding.

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Hello,


I've changed my code to follow code from codeproject and it works fine now.
Thanks, Terry Fei.
 
T

TerryFei

Hi Slawomir,

I am glad to know that the problem is resolved now. It's really been a
pleasure working
with you.

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top