By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

adding Visual Studio's ToolBox item by code

P: n/a
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.GetActiveOb ject("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("Vi sualStudio.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.vsWindowKi ndToolbox);
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.vsToolBoxItemFormatDotN ETComponent);
}
}
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

Nov 1 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.)

Nov 2 '05 #2

P: n/a
> 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.

--
Sławomir Piotrowski / Telsat GP
Rejestracja Czasu Pracy i Kontrola Dostępu
http://www.ewidencja-czasu-pracy.pl

Nov 4 '05 #3

P: n/a
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/d...FF9C915-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.)

Nov 8 '05 #4

P: n/a
Hello,
I've changed my code to follow code from codeproject and it works fine now.
Thanks, Terry Fei.
--
Sławomir Piotrowski / Telsat GP
Rejestracja Czasu Pracy i Kontrola Dostępu
http://www.ewidencja-czasu-pracy.pl

Nov 9 '05 #5

P: n/a
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.)

Nov 9 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.