I found the solution and could add my custom tab to the Visual Studio toolbox and add my custom controls to my custom tab with Windows Forms Application project and not from Visual Studio Add-in project. I describe it as follows:
In Windows Forms Application project, first, we must create an instance of Visual Studio IDE. Then, must create a temporary Windows Forms Application project and next, must add our custom tab to the Visual Studio toolbox and add our custom controls to our custom tab.
My codes are as following for Class1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
using System.IO;
namespace InstallToolboxControls
{
[ComImport(), Guid("00000016-0000-0000-C000-000000000046"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
interface IOleMessageFilter
{
[PreserveSig]
int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);
[PreserveSig]
int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);
[PreserveSig]
int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
}
class Program : IOleMessageFilter
{
[DllImport("ole32.dll")]
private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter, out IOleMessageFilter oldFilter);
static string ctrlPath = "WindowsFormsControlLibrary2.FloorsGrouping, WindowsFormsControlLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=197889249da45bfc";
[STAThread]
public static void Toolbox(string arg)
{
Program program = new Program();
program.Register();
if (arg.Equals("Install", StringComparison.CurrentCultureIgnoreCase))
{
program.InstallControl();
}
else if (arg.Equals("UnInstall", StringComparison.CurrentCultureIgnoreCase))
{
program.UninstallControl();
}
program.Revoke();
GC.Collect();
GC.WaitForPendingFinalizers();
}
void InstallControl()
{
Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
DTE dte = (DTE)System.Activator.CreateInstance(type, true);
string tmpFile = Path.GetFileNameWithoutExtension(Path.GetTempFileName());
string tmpDir = string.Format("{0}{1}", Path.GetTempPath(), tmpFile);
Solution2 solution = dte.Solution as Solution2;
string templatePath = solution.GetProjectTemplate("WindowsApplication.zip", "CSharp");
Project proj = solution.AddFromTemplate(templatePath, tmpDir, "dummyproj", false);
EnvDTE.Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
EnvDTE.ToolBox toolbox = (EnvDTE.ToolBox)window.Object;
EnvDTE.ToolBoxTab myTab = toolbox.ToolBoxTabs.Add("Saino");
myTab.Activate();
myTab.ToolBoxItems.Add("MyUserControl", ctrlPath, vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);
dte.Solution.Close(false);
Marshal.ReleaseComObject(dte);
}
void UninstallControl()
{
Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
DTE dte = (DTE)System.Activator.CreateInstance(type, true);
EnvDTE.Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
EnvDTE.ToolBox toolbox = (EnvDTE.ToolBox)window.Object;
EnvDTE.ToolBoxTab myTab = toolbox.ToolBoxTabs.Item("Saino");
myTab.Activate();
myTab.Delete();
Marshal.ReleaseComObject(dte);
}
void Register()
{
IOleMessageFilter oldFilter;
CoRegisterMessageFilter(this, out oldFilter);
}
void Revoke()
{
IOleMessageFilter oldFilter;
CoRegisterMessageFilter(null, out oldFilter);
}
#region IOleMessageFilter Members
public int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
{
return 0;
}
public int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
{
if (dwRejectType == 2)
{
return 200;
}
return -1;
}
public int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
{
return 2;
}
#endregion
}
}
And my codes are as following for Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InstallToolboxControls;
namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
InstallToolboxControls.Program prg = new InstallToolboxControls.Program();
InstallToolboxControls.Program.Toolbox("Install");
}
}
}