次の方法で共有


ToolboxItem コンストラクタ (Type)

指定した型のコンポーネントを作成する ToolboxItem クラスの新しいインスタンスを初期化します。

名前空間: System.Drawing.Design
アセンブリ: System.Drawing (system.drawing.dll 内)

構文

'宣言
Public Sub New ( _
    toolType As Type _
)
'使用
Dim toolType As Type

Dim instance As New ToolboxItem(toolType)
public ToolboxItem (
    Type toolType
)
public:
ToolboxItem (
    Type^ toolType
)
public ToolboxItem (
    Type toolType
)
public function ToolboxItem (
    toolType : Type
)

パラメータ

  • toolType
    ツールボックス項目が作成する IComponent の型。

例外

例外の種類 条件

InvalidOperationException

ToolboxItem がロックされています。

使用例

ToolboxItem から派生したクラスに ToolboxItem コンストラクタを使用して、カスタム ツールボックス項目を実装するコード例を次に示します。このコード例は、ToolboxItem クラスのトピックで取り上げているコード例の一部分です。

<Serializable()> _
Class MyToolboxItem
    Inherits ToolboxItem

    ' The add components dialog in Visual Studio looks for a public
    ' constructor that takes a type.
    Public Sub New(ByVal toolType As Type)
        MyBase.New(toolType)
    End Sub


    ' And you must provide this special constructor for serialization.
    ' If you add additional data to MyToolboxItem that you
    ' want to serialize, you may override Deserialize and
    ' Serialize methods to add that data.  
    Sub New(ByVal info As SerializationInfo, _
        ByVal context As StreamingContext)
        Deserialize(info, context)
    End Sub


    ' Let's override the creation code and pop up a dialog.
    Protected Overrides Function CreateComponentsCore( _
        ByVal host As System.ComponentModel.Design.IDesignerHost, _
        ByVal defaultValues As System.Collections.IDictionary) _
        As IComponent()
        ' Get the string we want to fill in the custom
        ' user control.  If the user cancels out of the dialog,
        ' return null or an empty array to signify that the 
        ' tool creation was canceled.
        Dim d As New ToolboxItemDialog()
        If d.ShowDialog() = DialogResult.OK Then
            Dim [text] As String = d.CreationText
            Dim comps As IComponent() = _
                MyBase.CreateComponentsCore(host, defaultValues)
            ' comps will have a single component: our data type.
            CType(comps(0), UserControl1).LabelText = [text]
            Return comps
        Else
            Return Nothing
        End If
    End Function
End Class
// Toolbox items must be serializable.
[Serializable]
class MyToolboxItem : ToolboxItem
{
    // The add components dialog in Visual Studio looks for a public
    // constructor that takes a type.
    public MyToolboxItem(Type toolType)
        : base(toolType)
    {
    }

    // And you must provide this special constructor for serialization.
    // If you add additional data to MyToolboxItem that you
    // want to serialize, you may override Deserialize and
    // Serialize methods to add that data.  
    MyToolboxItem(SerializationInfo info, StreamingContext context)
    {
        Deserialize(info, context);
    }

    // Let's override the creation code and pop up a dialog.
    protected override IComponent[] CreateComponentsCore(
        System.ComponentModel.Design.IDesignerHost host, 
        System.Collections.IDictionary defaultValues)
    {
        // Get the string we want to fill in the custom
        // user control.  If the user cancels out of the dialog,
        // return null or an empty array to signify that the 
        // tool creation was canceled.
        ToolboxItemDialog d = new ToolboxItemDialog();
        if (d.ShowDialog() == DialogResult.OK)
        {
            string text = d.CreationText;

            IComponent[] comps = 
                base.CreateComponentsCore(host, defaultValues);
            // comps will have a single component: our data type.
            ((UserControl1)comps[0]).LabelText = text;
            return comps;
        }
        else
        {
            return null;
        }
    }
}

プラットフォーム

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

参照

関連項目

ToolboxItem クラス
ToolboxItem メンバ
System.Drawing.Design 名前空間