ToolboxItem Osztály

Definíció

Egy eszközkészletelem alapszintű implementációját biztosítja.

public ref class ToolboxItem : System::Runtime::Serialization::ISerializable
[System.Serializable]
public class ToolboxItem : System.Runtime.Serialization.ISerializable
public class ToolboxItem : System.Runtime.Serialization.ISerializable
[<System.Serializable>]
type ToolboxItem = class
    interface ISerializable
type ToolboxItem = class
    interface ISerializable
Public Class ToolboxItem
Implements ISerializable
Öröklődés
ToolboxItem
Származtatott
Attribútumok
Megvalósítás

Példák

Az alábbi példakód egy olyan összetevőt mutat be, amely az IToolboxService interfészt használja egy szöveges adatformátum-kezelő vagy ToolboxItemCreatorCallbackaz eszközkészlethez való hozzáadásához. Az adat létrehozója visszahívási meghatalmazottja átadja az eszközkészletbe beillesztett szöveges adatokat, és egy űrlapra húzva egy olyan egyénire ToolboxItem , amely létrehozza TextBox a szöveget.

#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#using <System.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Drawing;
using namespace System::Drawing::Design;
using namespace System::Windows::Forms;
using namespace System::Security::Permissions;

namespace TextDataTextBoxComponent
{
   // Custom toolbox item creates a TextBox and sets its Text property
   // to the constructor-specified text.
   [PermissionSetAttribute(SecurityAction::Demand, Name="FullTrust")]
   public ref class TextToolboxItem: public ToolboxItem
   {
   private:
      String^ text;
      delegate void SetTextMethodHandler( Control^ c, String^ text );

   public:
      TextToolboxItem( String^ text )
         : ToolboxItem()
      {
         this->text = text;
      }

   protected:

      // ToolboxItem::CreateComponentsCore  to create the TextBox
      // and link a method to set its Text property.

      virtual array<IComponent^>^ CreateComponentsCore( IDesignerHost^ host ) override
      {
         TextBox^ textbox = dynamic_cast<TextBox^>(host->CreateComponent( TextBox::typeid ));
         
         // Because the designer resets the text of the textbox, use
         // a SetTextMethodHandler to set the text to the value of
         // the text data.
         Control^ c = dynamic_cast<Control^>(host->RootComponent);
         array<Object^>^temp0 = {textbox,text};
         c->BeginInvoke( gcnew SetTextMethodHandler( this, &TextToolboxItem::OnSetText ), temp0 );
         array<IComponent^>^temp1 = {textbox};
         return temp1;
      }

   private:

      // Method to set the text property of a TextBox after it is initialized.
      void OnSetText( Control^ c, String^ text )
      {
         c->Text = text;
      }

   };


   // Component that adds a "Text" data format ToolboxItemCreatorCallback 
    // to the Toolbox. This component uses a custom ToolboxItem that 
    // creates a TextBox containing the text data.
   public ref class TextDataTextBoxComponent: public Component
   {
   private:
      bool creatorAdded;
      IToolboxService^ ts;

   public:
      TextDataTextBoxComponent()
      {
         creatorAdded = false;
      }


      property System::ComponentModel::ISite^ Site 
      {
         // ISite to register TextBox creator
         virtual System::ComponentModel::ISite^ get() override
         {
            return __super::Site;
         }

         virtual void set( System::ComponentModel::ISite^ value ) override
         {
            if ( value != nullptr )
            {
               __super::Site = value;
               if (  !creatorAdded )
                              AddTextTextBoxCreator();
            }
            else
            {
               if ( creatorAdded )
                              RemoveTextTextBoxCreator();
               __super::Site = value;
            }
         }
      }

   private:

      // Adds a "Text" data format creator to the toolbox that creates
      // a textbox from a text fragment pasted to the toolbox.
      void AddTextTextBoxCreator()
      {
         ts = dynamic_cast<IToolboxService^>(GetService( IToolboxService::typeid ));
         if ( ts != nullptr )
         {
            ToolboxItemCreatorCallback^ textCreator = 
                gcnew ToolboxItemCreatorCallback( 
                this, 
                &TextDataTextBoxComponent::CreateTextBoxForText );
            try
            {
               ts->AddCreator( 
                   textCreator, 
                   "Text", 
                   dynamic_cast<IDesignerHost^>(GetService( IDesignerHost::typeid )) );
               creatorAdded = true;
            }
            catch ( Exception^ ex ) 
            {
               MessageBox::Show( ex->ToString(), "Exception Information" );
            }
         }
      }


      // Removes any "Text" data format creator from the toolbox.
      void RemoveTextTextBoxCreator()
      {
         if ( ts != nullptr )
         {
            ts->RemoveCreator( 
                "Text", 
                dynamic_cast<IDesignerHost^>(GetService( IDesignerHost::typeid )) );
            creatorAdded = false;
         }
      }


      // ToolboxItemCreatorCallback delegate format method to create
      // the toolbox item.
      ToolboxItem^ CreateTextBoxForText( Object^ serializedObject, String^ format )
      {
        IDataObject^ o = gcnew DataObject(dynamic_cast<IDataObject^>(serializedObject));

        if( o->GetDataPresent("System::String", true) )
        {
            String^ toolboxText = dynamic_cast<String^>(o->GetData( "System::String", true ));
           return( gcnew TextToolboxItem( toolboxText ));
        }

        return nullptr;
      }

   public:
      ~TextDataTextBoxComponent()
      {
         if ( creatorAdded )
                  RemoveTextTextBoxCreator();
      }
   };
}
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;
using System.Security.Permissions;
using System.Windows.Forms;

namespace TextDataTextBoxComponent
{
    // Component that adds a "Text" data format ToolboxItemCreatorCallback 
    // to the Toolbox. This component uses a custom ToolboxItem that 
    // creates a TextBox containing the text data.
    public class TextDataTextBoxComponent : Component
    {
        private bool creatorAdded = false;
        private IToolboxService ts;

        public TextDataTextBoxComponent()
        {                     
        }

        // ISite override to register TextBox creator
        public override System.ComponentModel.ISite Site
        {
            get
            {
                return base.Site;
            }
            set
            {
                if( value != null )
                {                    
                    base.Site = value;

                    if (!creatorAdded)
                    {
                        AddTextTextBoxCreator();
                    }
                }
                else
                {
                    if (creatorAdded)
                    {
                        RemoveTextTextBoxCreator();
                    }

                    base.Site = value;             
                }
            }
        }

        // Adds a "Text" data format creator to the toolbox that creates 
        // a textbox from a text fragment pasted to the toolbox.
        private void AddTextTextBoxCreator()
        {
            ts = (IToolboxService)GetService(typeof(IToolboxService));

            if (ts != null) 
            {
                ToolboxItemCreatorCallback textCreator = 
                    new ToolboxItemCreatorCallback(this.CreateTextBoxForText);

                try
                {
                    ts.AddCreator(
                        textCreator, 
                        "Text", 
                        (IDesignerHost)GetService(typeof(IDesignerHost)));

                    creatorAdded = true;
                }
                catch(Exception ex)
                {
                    MessageBox.Show(
                        ex.ToString(), 
                        "Exception Information");
                }                
            }
        }

        // Removes any "Text" data format creator from the toolbox.
        private void RemoveTextTextBoxCreator()
        {
            if (ts != null)             
            {
                ts.RemoveCreator(
                    "Text", 
                    (IDesignerHost)GetService(typeof(IDesignerHost)));            

                creatorAdded = false;
            }
        }

        // ToolboxItemCreatorCallback delegate format method to create 
        // the toolbox item.
        private ToolboxItem CreateTextBoxForText(
            object serializedObject, 
            string format)
        {
            DataObject o = new DataObject((IDataObject)serializedObject);

            string[] formats = o.GetFormats();

            if (o.GetDataPresent("System.String", true))
            {
                string toolboxText = (string)(o.GetData("System.String", true));
                return (new TextToolboxItem(toolboxText));
            }

            return null;
        }

        protected override void Dispose(bool disposing)
        {
            if (creatorAdded)
            {
                RemoveTextTextBoxCreator();
            }
        }        
    }

    // Custom toolbox item creates a TextBox and sets its Text property
    // to the constructor-specified text.
    public class TextToolboxItem : ToolboxItem
    {
        private string text;
        private delegate void SetTextMethodHandler(Control c, string text);

        public TextToolboxItem(string text) : base()
        {
            this.text = text;
        }

        // ToolboxItem.CreateComponentsCore override to create the TextBox 
        // and link a method to set its Text property.
        protected override IComponent[] CreateComponentsCore(IDesignerHost host)
        {
            System.Windows.Forms.TextBox textbox = 
                (TextBox)host.CreateComponent(typeof(TextBox));
                
            // Because the designer resets the text of the textbox, use 
            // a SetTextMethodHandler to set the text to the value of 
            // the text data.
            Control c = host.RootComponent as Control;
            c.BeginInvoke(
                new SetTextMethodHandler(OnSetText), 
                new object[] {textbox, text});
           
            return new System.ComponentModel.IComponent[] { textbox };
        }        

        // Method to set the text property of a TextBox after it is initialized.
        private void OnSetText(Control c, string text) 
        {
            c.Text = text;
        }
    }
}
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Security.Permissions
Imports System.Windows.Forms

' Component that adds a "Text" data format ToolboxItemCreatorCallback 
' to the Toolbox. This component uses a custom ToolboxItem that 
' creates a TextBox containing the text data.
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Public Class TextDataTextBoxComponent
    Inherits System.ComponentModel.Component

    Private creatorAdded As Boolean = False
    Private ts As IToolboxService

    Public Sub New()
    End Sub

    ' ISite override to register TextBox creator
    Public Overrides Property Site() As System.ComponentModel.ISite
        Get
            Return MyBase.Site
        End Get
        Set(ByVal Value As System.ComponentModel.ISite)
            If (Value IsNot Nothing) Then
                MyBase.Site = Value
                If Not creatorAdded Then
                    AddTextTextBoxCreator()
                End If
            Else
                If creatorAdded Then
                    RemoveTextTextBoxCreator()
                End If
                MyBase.Site = Value
            End If
        End Set
    End Property

    ' Adds a "Text" data format creator to the toolbox that creates 
    ' a textbox from a text fragment pasted to the toolbox.
    Private Sub AddTextTextBoxCreator()
        ts = CType(GetService(GetType(IToolboxService)), IToolboxService)
        If (ts IsNot Nothing) Then
            Dim textCreator As _
            New ToolboxItemCreatorCallback(AddressOf Me.CreateTextBoxForText)
            Try
                ts.AddCreator( _
                textCreator, _
                "Text", _
                CType(GetService(GetType(IDesignerHost)), IDesignerHost))

                creatorAdded = True
            Catch ex As Exception
                MessageBox.Show(ex.ToString(), "Exception Information")
            End Try
        End If
    End Sub

    ' Removes any "Text" data format creator from the toolbox.
    Private Sub RemoveTextTextBoxCreator()
        If (ts IsNot Nothing) Then
            ts.RemoveCreator( _
            "Text", _
            CType(GetService(GetType(IDesignerHost)), IDesignerHost))
            creatorAdded = False
        End If
    End Sub

    ' ToolboxItemCreatorCallback delegate format method to create 
    ' the toolbox item.
    Private Function CreateTextBoxForText( _
    ByVal serializedObject As Object, _
    ByVal format As String) As ToolboxItem

        Dim o As New DataObject(CType(serializedObject, IDataObject))

        Dim formats As String() = o.GetFormats()

        If o.GetDataPresent("System.String", True) Then

            Return New TextToolboxItem(CStr(o.GetData("System.String", True)))

        End If

        Return Nothing
    End Function

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If creatorAdded Then
            RemoveTextTextBoxCreator()
        End If
    End Sub

End Class

' Custom toolbox item creates a TextBox and sets its Text property
' to the constructor-specified text.
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Public Class TextToolboxItem
    Inherits System.Drawing.Design.ToolboxItem

    Private [text] As String

    Delegate Sub SetTextMethodHandler( _
    ByVal c As Control, _
    ByVal [text] As String)

    Public Sub New(ByVal [text] As String)
        Me.text = [text]
    End Sub

    ' ToolboxItem.CreateComponentsCore override to create the TextBox 
    ' and link a method to set its Text property.
    <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
    Protected Overrides Function CreateComponentsCore(ByVal host As IDesignerHost) As IComponent()
        Dim textbox As TextBox = CType(host.CreateComponent(GetType(TextBox)), TextBox)

        ' Because the designer resets the text of the textbox, use 
        ' a SetTextMethodHandler to set the text to the value of 
        ' the text data.
        Dim c As Control = host.RootComponent

        c.BeginInvoke( _
        New SetTextMethodHandler(AddressOf OnSetText), _
        New Object() {textbox, [text]})

        Return New System.ComponentModel.IComponent() {textbox}
    End Function

    ' Method to set the text property of a TextBox after it is initialized.
    Private Sub OnSetText(ByVal c As Control, ByVal [text] As String)
        c.Text = [text]
    End Sub

End Class

Az alábbi példakód bemutatja az osztály alaposztályként való ToolboxItem használatát egy egyéni eszközkészletelem-implementációhoz.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Runtime.Serialization;
using System.Windows.Forms;

namespace CustomToolboxItem;

public class Form1 : Form
{
    readonly IContainer _components;
    UserControl1 userControl11;
    Label label1;

    public Form1() => InitializeComponent();

    void InitializeComponent()
    {
        label1 = new Label();
        userControl11 = new UserControl1();
        SuspendLayout();
        // 
        // label1
        // 
        label1.Location = new Point(15, 16);
        label1.Name = "label1";
        label1.Size = new Size(265, 62);
        label1.TabIndex = 0;
        label1.Text = "Build the project and drop UserControl1 from the toolbox onto the form.";
        // 
        // userControl11
        // 
        userControl11.LabelText = "This is a custom user control.  The text you see here is provided by a custom too" +
            "lbox item when the user control is dropped on the form.";
        userControl11.Location = new Point(74, 81);
        userControl11.Name = "userControl11";
        userControl11.Padding = new Padding(6);
        userControl11.Size = new Size(150, 150);
        userControl11.TabIndex = 1;
        // 
        // Form1
        // 
        ClientSize = new Size(292, 266);
        Controls.Add(userControl11);
        Controls.Add(label1);
        Name = "Form1";
        Text = "Form1";
        ResumeLayout(false);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (_components != null))
        {
            _components.Dispose();
        }
        base.Dispose(disposing);
    }

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}

// Configure this user control to have a custom toolbox item.
[ToolboxItem(typeof(MyToolboxItem))]
public class UserControl1 : UserControl
{
    readonly IContainer _components;
    Label label1;

    public UserControl1() => InitializeComponent();

    public string LabelText
    {
        get => label1.Text; set => label1.Text = value;
    }

    void InitializeComponent()
    {
        label1 = new Label();
        SuspendLayout();
        // 
        // label1
        // 
        label1.Dock = DockStyle.Fill;
        label1.Location = new Point(6, 6);
        label1.Name = "label1";
        label1.Size = new Size(138, 138);
        label1.TabIndex = 0;
        label1.Text = "This is a custom user control.  " +
            "The text you see here is provided by a custom toolbox" +
            " item when the user control is dropped on the form.\r\n";
        label1.TextAlign =
            ContentAlignment.MiddleCenter;
        // 
        // UserControl1
        // 
        Controls.Add(label1);
        Name = "UserControl1";
        Padding = new Padding(6);
        ResumeLayout(false);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (_components != null))
        {
            _components.Dispose();
        }
        base.Dispose(disposing);
    }
}

// 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())
        {
            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;
            }
        }
    }
}

public class ToolboxItemDialog : Form
{
    readonly IContainer _components;
    Label label1;
    TextBox textBox1;
    Button button1;
    Button button2;

    public ToolboxItemDialog() => InitializeComponent();

    void InitializeComponent()
    {
        label1 = new Label();
        textBox1 = new TextBox();
        button1 = new Button();
        button2 = new Button();
        SuspendLayout();
        // 
        // label1
        // 
        label1.Location = new Point(10, 9);
        label1.Name = "label1";
        label1.Size = new Size(273, 43);
        label1.TabIndex = 0;
        label1.Text = "Enter the text you would like" +
            " to have the user control populated with:";
        // 
        // textBox1
        // 
        textBox1.AutoSize = false;
        textBox1.Location = new Point(10, 58);
        textBox1.Multiline = true;
        textBox1.Name = "textBox1";
        textBox1.Size = new Size(270, 67);
        textBox1.TabIndex = 1;
        textBox1.Text = "This is a custom user control.  " +
            "The text you see here is provided by a custom toolbox" +
            " item when the user control is dropped on the form.";
        // 
        // button1
        // 
        button1.DialogResult = DialogResult.OK;
        button1.Location = new Point(124, 131);
        button1.Name = "button1";
        button1.TabIndex = 2;
        button1.Text = "OK";
        // 
        // button2
        // 
        button2.DialogResult = DialogResult.Cancel;
        button2.Location = new Point(205, 131);
        button2.Name = "button2";
        button2.TabIndex = 3;
        button2.Text = "Cancel";
        // 
        // ToolboxItemDialog
        // 
        AcceptButton = button1;
        CancelButton = button2;
        ClientSize = new Size(292, 162);
        ControlBox = false;
        Controls.Add(button2);
        Controls.Add(button1);
        Controls.Add(textBox1);
        Controls.Add(label1);
        FormBorderStyle =
            FormBorderStyle.FixedDialog;
        Name = "ToolboxItemDialog";
        Text = "ToolboxItemDialog";
        ResumeLayout(false);
    }

    public string CreationText => textBox1.Text;

    protected override void Dispose(bool disposing)
    {
        if (disposing && (_components != null))
        {
            _components.Dispose();
        }
        base.Dispose(disposing);
    }
}
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Runtime.Serialization
Imports System.Text
Imports System.Windows.Forms



Public Class Form1
    Inherits Form
    Private components As System.ComponentModel.IContainer = Nothing
    Friend WithEvents UserControl11 As UserControl1
    Friend WithEvents label1 As System.Windows.Forms.Label

    Public Sub New() 
        InitializeComponent()
    End Sub
    
    
    Private Sub InitializeComponent() 
        Me.label1 = New System.Windows.Forms.Label
        Me.UserControl11 = New UserControl1
        Me.SuspendLayout()
        '
        'label1
        '
        Me.label1.Location = New System.Drawing.Point(15, 16)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(265, 62)
        Me.label1.TabIndex = 0
        Me.label1.Text = "Build the project and drop UserControl1" + _
            " from the toolbox onto the form."
        '
        'UserControl11
        '
        Me.UserControl11.LabelText = "This is a custom user control.  " + _
            "The text you see here is provided by a custom too" + _
            "lbox item when the user control is dropped on the form."
        Me.UserControl11.Location = New System.Drawing.Point(74, 85)
        Me.UserControl11.Name = "UserControl11"
        Me.UserControl11.Padding = New System.Windows.Forms.Padding(6)
        Me.UserControl11.TabIndex = 1
        '
        'Form1
        '
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.UserControl11)
        Me.Controls.Add(Me.label1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub
     
    
    Protected Overrides Sub Dispose(ByVal disposing As Boolean) 
        If disposing AndAlso (components IsNot Nothing) Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    
    
    Shared Sub Main() 
        Application.EnableVisualStyles()
        Application.Run(New Form1())
    End Sub
End Class

' Configure this user control to have a custom toolbox item.
<ToolboxItem(GetType(MyToolboxItem))> _
Public Class UserControl1
    Inherits UserControl
    Private components As System.ComponentModel.IContainer = Nothing
    Private label1 As System.Windows.Forms.Label


    Public Sub New()
        InitializeComponent()
    End Sub


    Public Property LabelText() As String
        Get
            Return label1.Text
        End Get
        Set(ByVal value As String)
            label1.Text = Value
        End Set
    End Property


    Private Sub InitializeComponent()
        Me.label1 = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        ' 
        ' label1
        ' 
        Me.label1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.label1.Location = New System.Drawing.Point(6, 6)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(138, 138)
        Me.label1.TabIndex = 0
        Me.label1.Text = "This is a custom user control.  " + _
            "The text you see here is provided by a custom toolbox" + _
            " item when the user control is dropped on the form." + _
            vbCr + vbLf
        Me.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        ' 
        ' UserControl1
        ' 
        Me.Controls.Add(label1)
        Me.Name = "UserControl1"
        Me.Padding = New System.Windows.Forms.Padding(6)
        Me.ResumeLayout(False)
    End Sub


    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso (components IsNot Nothing) Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
End Class

' Toolbox items must be serializable.
<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


Public Class ToolboxItemDialog
    Inherits Form
    Private components As System.ComponentModel.IContainer = Nothing
    Private label1 As System.Windows.Forms.Label
    Private textBox1 As System.Windows.Forms.TextBox
    Private button1 As System.Windows.Forms.Button
    Private button2 As System.Windows.Forms.Button
    
    
    Public Sub New() 
        InitializeComponent()
    End Sub
    
    Private Sub InitializeComponent() 
        Me.label1 = New System.Windows.Forms.Label()
        Me.textBox1 = New System.Windows.Forms.TextBox()
        Me.button1 = New System.Windows.Forms.Button()
        Me.button2 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        ' 
        ' label1
        ' 
        Me.label1.Location = New System.Drawing.Point(10, 9)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(273, 43)
        Me.label1.TabIndex = 0
        Me.label1.Text = "Enter the text you would like" + _
            " to have the user control populated with:"
        ' 
        ' textBox1
        ' 
        Me.textBox1.AutoSize = False
        Me.textBox1.Location = New System.Drawing.Point(10, 58)
        Me.textBox1.Multiline = True
        Me.textBox1.Name = "textBox1"
        Me.textBox1.Size = New System.Drawing.Size(270, 67)
        Me.textBox1.TabIndex = 1
        Me.textBox1.Text = "This is a custom user control.  " + _
            "The text you see here is provided by a custom toolbox" + _
            " item when the user control is dropped on the form."
        ' 
        ' button1
        ' 
        Me.button1.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.button1.Location = New System.Drawing.Point(124, 131)
        Me.button1.Name = "button1"
        Me.button1.TabIndex = 2
        Me.button1.Text = "OK"
        ' 
        ' button2
        ' 
        Me.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.button2.Location = New System.Drawing.Point(205, 131)
        Me.button2.Name = "button2"
        Me.button2.TabIndex = 3
        Me.button2.Text = "Cancel"
        ' 
        ' ToolboxItemDialog
        ' 
        Me.AcceptButton = Me.button1
        Me.CancelButton = Me.button2
        Me.ClientSize = New System.Drawing.Size(292, 162)
        Me.ControlBox = False
        Me.Controls.Add(button2)
        Me.Controls.Add(button1)
        Me.Controls.Add(textBox1)
        Me.Controls.Add(label1)
        Me.FormBorderStyle = _
            System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.Name = "ToolboxItemDialog"
        Me.Text = "ToolboxItemDialog"
        Me.ResumeLayout(False)
    
    End Sub
    
    Public ReadOnly Property CreationText() As String 
        Get
            Return textBox1.Text
        End Get
    End Property
    
    
    Protected Overrides Sub Dispose(ByVal disposing As Boolean) 
        If disposing AndAlso (components IsNot Nothing) Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    
    End Sub
End Class

Megjegyzések

ToolboxItem az eszközkészletelemek alaposztálya, amely egy tervezési időalapú környezet eszközkészletében jeleníthető meg. Az eszközkészletelemek általában egy tervezőmódú dokumentumon való meghíváskor létrehozandó összetevőt jelölnek. Az ToolboxItem osztály tartalmazza azokat a metódusokat és tulajdonságokat, amelyek szükségesek ahhoz, hogy az eszközkészlet megjelenítse az eszközkészlet elemének megjelenítési tulajdonságait, használat esetén hozzon létre egy összetevőt vagy összetevőt, és szerializálja és deszerializálja magát az eszközkészlet-adatbázisban való megőrzés érdekében.

Az osztály egy példánya ToolboxItem konfigurálható névvel, bitképpel és típussal a létrehozáshoz, anélkül, hogy létrehoz egy osztályt, amelyből ToolboxItemszármazik. Az ToolboxItem osztály emellett egy alaposztályt is biztosít az egyéni eszközkészletelemek implementációihoz. Az egyéni elemek ToolboxItem több összetevőt is létrehozhatnak. Egyéni eszközkészletelem implementálásához a , ToolboxItemés a metódusokból CreateComponentsCore kell származnia és Serialize felül kell bírálnia.Deserialize

A következő tulajdonságokat és metódusokat kell konfigurálni a ToolboxItem megfelelő működéshez:

  • A DisplayName tulajdonság az eszközkészletelem címkéjét adja meg, amikor egy eszközkészletben jelenik meg.

  • A TypeName tulajdonság megadja az elem által létrehozott összetevő típusának teljes nevét. Ha egy származtatott osztály több összetevőt hoz létre, a TypeName tulajdonság használható vagy nem használható, attól függően, hogy a CreateComponentsCore metódus felülbírálása függ-e a tulajdonság értékétől.

  • A AssemblyName tulajdonság megadja azt a szerelvényt, amely az elem által létrehozott összetevő típusát tartalmazza.

  • A Bitmap tulajdonság opcionálisan egy bitképképet ad meg, amely az eszközkészlet elemének megjelenítendő neve mellett jelenik meg az eszközkészletben.

  • A Filter tulajdonság opcionálisan tartalmaz minden ToolboxItemFilterAttribute olyan objektumot, amely meghatározza, hogy az elem használható-e egy adott összetevőn.

  • A CreateComponentsCore metódus visszaadja az összetevőpéldányt vagy -példányokat, hogy beszúrják az eszközt.

  • A Serialize metódus az eszközkészlet-elemet egy megadott SerializationInfofájlba menti.

  • A Deserialize metódus a megadott SerializationInfoállapotinformációkból konfigurálja az eszközkészlet-elemet.

  • A Initialize metódus úgy konfigurálja az eszközkészlet-elemet, hogy a megadott típusú összetevőt hozza létre, ha a CreateComponentsCore metódust nem bírálták felül, hogy másképp viselkedjen.

  • A Locked tulajdonság azt jelzi, hogy az eszközkészletelem tulajdonságai módosíthatók-e. Az eszközkészlet-elemek általában zárolva vannak, miután hozzáadták egy eszközkészlethez.

  • A Lock metódus zárol egy elemkészletet.

  • A CheckUnlocked metódus kivételt eredményez, ha a Locked tulajdonság .true

Konstruktorok

Name Description
ToolboxItem()

Inicializálja a ToolboxItem osztály új példányát.

ToolboxItem(Type)

Inicializálja a ToolboxItem megadott típusú összetevőt létrehozó osztály új példányát.

Tulajdonságok

Name Description
AssemblyName

Lekéri vagy beállítja az eszközkészletelem által létrehozott típust vagy típusokat tartalmazó szerelvény nevét.

Bitmap

Lekéri vagy beállít egy bitképet, amely az eszközkészlet elemét jelöli az eszközkészletben.

Company

Lekéri vagy beállítja ennek ToolboxItema vállalatnak a nevét.

ComponentType

Lekéri ennek az összetevőtípusnak a ToolboxItemtípusát.

DependentAssemblies

Lekéri vagy beállítja az AssemblyName eszközkészlet-elemet.

Description

Lekéri vagy beállítja ennek ToolboxItema leírásnak a leírását.

DisplayName

Lekéri vagy beállítja az eszközkészletelem megjelenítendő nevét.

Filter

Lekéri vagy beállítja azt a szűrőt, amely meghatározza, hogy az eszközkészlet-elem használható-e a célösszetevőn.

IsTransient

Egy értéket kap, amely jelzi, hogy az eszközkészletelem átmeneti-e.

Locked

Beolvas egy értéket, amely jelzi, hogy az ToolboxItem aktuálisan zárolt-e.

OriginalBitmap

Lekéri vagy beállítja az elem eszközkészletében használni kívánt eredeti bitképet.

Properties

Lekéri a tulajdonságok szótárát.

TypeName

Lekéri vagy beállítja annak a típusnak IComponent a teljes nevét, amelyet az eszközkészletelem a meghíváskor hoz létre.

Version

Lekéri ennek a verziónak a ToolboxItemverzióját.

Metódusok

Name Description
CheckUnlocked()

Kivételt jelez, ha az eszközkészlet-elem jelenleg zárolva van.

CreateComponents()

Létrehozza azokat az összetevőket, amelyek létrehozásához az eszközkészlet-elem konfigurálva van.

CreateComponents(IDesignerHost, IDictionary)

Létrehozza azokat az összetevőket, amelyeket az eszközkészletelem úgy van konfigurálva, hogy a megadott tervezői gazdagépet és alapértelmezett értékeket használja.

CreateComponents(IDesignerHost)

Létrehozza azokat az összetevőket, amelyeket az eszközkészletelem konfigurálva van a létrehozáshoz a megadott tervezői gazdagép használatával.

CreateComponentsCore(IDesignerHost, IDictionary)

Összetevők tömbjét hozza létre az eszközkészlet-elem meghívásakor.

CreateComponentsCore(IDesignerHost)

Összetevőt vagy összetevőtömböt hoz létre az eszközkészletelem meghívásakor.

Deserialize(SerializationInfo, StreamingContext)

Betölti az eszközkészletelem állapotát a megadott szerializálási információs objektumból.

Equals(Object)

Meghatározza, hogy két ToolboxItem példány egyenlő-e.

FilterPropertyValue(String, Object)

A visszaadása előtt szűr egy tulajdonságértéket.

GetHashCode()

A példány kivonatkódját adja vissza.

GetType()

Lekéri az Type aktuális példányt.

(Öröklődés forrása Object)
GetType(IDesignerHost, AssemblyName, String, Boolean)

Létrehoz egy példányt a megadott típusból, opcionálisan egy megadott tervező gazdagép és szerelvénynév használatával.

GetType(IDesignerHost)

Engedélyezi az eszközkészlet-elemhez társított típus elérését.

Initialize(Type)

Inicializálja az aktuális elemkészletet a létrehozandó megadott típussal.

Lock()

Zárolja az eszközkészlet-elemet, és megakadályozza a tulajdonságainak módosítását.

MemberwiseClone()

Az aktuális Objectpéldány sekély másolatát hozza létre.

(Öröklődés forrása Object)
OnComponentsCreated(ToolboxComponentsCreatedEventArgs)

Az eseményt emeli ComponentsCreated ki.

OnComponentsCreating(ToolboxComponentsCreatingEventArgs)

Az eseményt emeli ComponentsCreating ki.

Serialize(SerializationInfo, StreamingContext)

Menti az eszközkészletelem állapotát a megadott szerializálási információs objektumba.

ToString()

Az aktuális Stringértéket képviselő értéket ToolboxItem ad vissza.

ValidatePropertyType(String, Object, Type, Boolean)

Ellenőrzi, hogy egy objektum egy adott típusú-e.

ValidatePropertyValue(String, Object)

Ellenőrzi a tulajdonságot, mielőtt hozzá lett rendelve a tulajdonságszótárhoz.

esemény

Name Description
ComponentsCreated

Közvetlenül az összetevők létrehozása után következik be.

ComponentsCreating

Az összetevők létrehozásakor fordul elő.

Explicit interfész-implementációk

Name Description
ISerializable.GetObjectData(SerializationInfo, StreamingContext)

Ennek a tagnak a leírását a GetObjectData(SerializationInfo, StreamingContext) metódus ismerteti.

A következőre érvényes:

Lásd még