我创建了一个新的 Visual Studio 插件,用于在 Visual Studio 工具箱中创建和添加我的自定义选项卡,并将新项目(控件)添加到我的自定义选项卡中。代码适用于将新选项卡添加到 Visual Studio 工具箱,但不适用于将新项目(控件)添加到我的选项卡。
我的 Visual Studio 插件代码是:
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using EnvDTE90;
using EnvDTE100;
using System.Windows.Forms;
namespace MyAddin1
{
/// <summary>The object for implementing an Add-in.</summary>
/// <seealso class='IDTExtensibility2' />
public class Connect : IDTExtensibility2
{
/// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
public Connect()
{
}
/// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
/// <param term='application'>Root object of the host application.</param>
/// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
/// <param term='addInInst'>Object representing this Add-in.</param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
// Pass the applicationObject member variable to the code example.
ToolboxExample(_applicationObject);
}
public void ToolboxExample(DTE2 dte)
{
ToolBox tlBox = null;
ToolBoxTabs tbxTabs = null;
ToolBoxTab3 tbxTab = null;
try
{
tlBox = (ToolBox)(dte.Windows.Item(Constants.vsWindowKindToolbox).Object);
tbxTabs = tlBox.ToolBoxTabs;
tbxTab = (ToolBoxTab3)tbxTabs.Add("MRS");
tbxTab.Activate();
tbxTab.ToolBoxItems.Add("FloorsGrouping", @"C:\Windows\Microsoft.NET\assembly\GAC_MSIL\WindowsFormsControlLibrary2\v4.0_1.0.0.0__197889249da45bfc\WindowsFormsControlLibrary2.dll", vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);
}
catch (System.Exception ex)
{
MessageBox.Show("ERROR: " + ex.Message);
}
}
private DTE2 _applicationObject;
private AddIn _addInInstance;
}
}
以下代码行不起作用:
tbxTab.ToolBoxItems.Add("FloorsGrouping", @"C:\Windows\Microsoft.NET\assembly\GAC_MSIL\WindowsFormsControlLibrary2\v4.0_1.0.0.0__197889249da45bfc\WindowsFormsControlLibrary2.dll", vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);
我改变
tbxTab.ToolBoxItems.Add
和:
tbxTabs.Item("MRS").ToolBoxItems.Add
但是,它对我不起作用。即使我改变
@"C:\Windows\Microsoft.NET\assembly\GAC_MSIL\WindowsFormsControlLibrary2\v4.0_1.0.0.0__197889249da45bfc\WindowsFormsControlLibrary2.dll"
使用以下代码行并一一测试:
@"E:\Rostami\Saino\WindowsFormsControlLibrary2.dll"
和
"WindowsFormsControlLibrary2.FloorsGrouping, WindowsFormsControlLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=197889249da45bfc"
但是,它再次对我不起作用。
我的自定义控件主类名称是FloorsGrouping,它的显示名称是:
[DisplayName("Floors Group")]
它在 GAC 中的程序集名称是:
[Editor("WindowsFormsControlLibrary2.FloorsGrouping, WindowsFormsControlLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=197889249da45bfc", typeof(UITypeEditor))]
我在 Internet 上搜索了任何解决方案,但我只找到了几个解决方案,这些解决方案描述了向 Visual Studio 工具箱添加新选项卡以及从 Visual Studio Addin 向选项卡添加控件。