C# adding custom controls in vs toolbox
Hello,
I have developed some custom controls and now im doing the setup. I need to insert them into vs toolbar at the setup. For this i created a console application which is launched before setup is done.
I used DTE and some Reflection.
The main ideea is that i look if vs is opened then i get the toolbar object create a tab and add the controls from the assembly. If the vs is not opened or is opend but is running and you dont have acces to the toolbox object (for exemple is running the some code for debug) i create a new instance of vs and add the tab and the controls.
Well my problem is when i create a new instance. I get the toolbox object i add the tab but the controls r not added in the tab. I cant find the problem.
Heres the part of interest for this job.
1. Getting the vs instance :::
public static DTE GetDesignTimeEnvironment(DTEVersion dteVersion, ref bool alreadyCreated)
{
alreadyCreated = false;
string progID = GetEnumDescription(dteVersion);
DTE result;
try
{
//Determine if there is an open VS.Net
result = (DTE) Marshal.GetActiveObject(progID);
//Try to show properties window, if it fails VS.Net is in run-time
//you have to open a new VS.Net
try
{
result.ExecuteCommand("View.PropertiesWindow", "");
alreadyCreated = true;
}
catch //You have to open a new VS.Net
{
result = null;
}
}
catch //There is no open VS.Net
{
result = null;
}
if(result == null)
{
//Open a new VS.Net
Type type = Type.GetTypeFromProgID(progID);
if(type != null)
result = (DTE)Activator.CreateInstance(type);
}
return result;
}
2. Try to insert them :::
internal static bool RegisterControls(DTE dte, bool alreadyCreatedDTE, VSControl[] controls)
{
Window toolbox = dte.Windows.Item(EnvDTE.Constants.vsWindowKindTool box);
ToolBoxTabs tabs = ((ToolBox) toolbox.Object).ToolBoxTabs;
dte.ExecuteCommand("View.PropertiesWindow", "");
foreach(VSControl control in controls)
{
ToolBoxTab tab;
if(control.IsToolBoxTab)
{
if(GetToolBoxTab(tabs, control.TabName) == null)
tab = tabs.Add(control.TabName);
}
else
{
tab = GetToolBoxTab(tabs, control.TabName);
if(tab != null && !control.IsToolBoxTab)
{
tab.Activate();
tab.ToolBoxItems.Item(1).Select();
tab.ToolBoxItems.Add("MyControls", control.AssemblyPath, vsToolBoxItemFormat.vsToolBoxItemFormatDotNETCompo nent);
}
}
}
if(!alreadyCreatedDTE)
dte.Quit();
return true;
}
private static ToolBoxTab GetToolBoxTab(ToolBoxTabs tabs, string tabName)
{
foreach(ToolBoxTab tab in tabs)
{
if (smenglish.CompareInfo.Compare(tab.Name, tabName, CompareOptions.IgnoreCase) == 0)
return tab;
}
return null;
}
It seems that in the GetToolBoxTab(..) when vs is not opend and i create an instance each tab.Name trows execptions. I guess the toolbox object is not avaible. What can i do ?
Regards.
|