ToolboxItem Costruttori

Definizione

Inizializza una nuova istanza della classe ToolboxItem.

Overload

ToolboxItem()

Inizializza una nuova istanza della classe ToolboxItem.

ToolboxItem(Type)

Inizializza una nuova istanza della classe ToolboxItem che consente di creare il tipo di componente specificato.

Esempio

Nell'esempio ToolboxItem di codice seguente viene illustrato l'uso del costruttore in una classe derivata da ToolboxItem per un'implementazione personalizzata dell'elemento della casella degli strumenti. Questo esempio di codice fa parte di un esempio più grande fornito per la ToolboxItem classe.

// 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.
        using (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;
            }
        }
    }
}
<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.
        Using 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 Using
    End Function
End Class

ToolboxItem()

Inizializza una nuova istanza della classe ToolboxItem.

public:
 ToolboxItem();
public ToolboxItem ();
Public Sub New ()

Esempio

Nell'esempio ToolboxItem di codice seguente viene illustrato l'uso del costruttore in una classe derivata da ToolboxItem per un'implementazione personalizzata dell'elemento della casella degli strumenti. Questo esempio di codice fa parte di un esempio più grande fornito per la ToolboxItem classe.

// 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.
        using (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;
            }
        }
    }
}
<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.
        Using 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 Using
    End Function
End Class

Si applica a

ToolboxItem(Type)

Inizializza una nuova istanza della classe ToolboxItem che consente di creare il tipo di componente specificato.

public:
 ToolboxItem(Type ^ toolType);
public ToolboxItem (Type toolType);
public ToolboxItem (Type? toolType);
new System.Drawing.Design.ToolboxItem : Type -> System.Drawing.Design.ToolboxItem
Public Sub New (toolType As Type)

Parametri

toolType
Type

Il tipo di interfaccia IComponent che viene creata dall'elemento della casella di controllo.

Eccezioni

Esempio

Nell'esempio ToolboxItem di codice seguente viene illustrato l'uso del costruttore in una classe derivata da ToolboxItem per un'implementazione personalizzata dell'elemento della casella degli strumenti. Questo esempio di codice fa parte di un esempio più grande fornito per la ToolboxItem classe.

// 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.
        using (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;
            }
        }
    }
}
<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.
        Using 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 Using
    End Function
End Class

Si applica a