How to use an Items Collection Editor on a custom UserControl (.Net 4, Winforms)?

寵の児 提交于 2021-01-21 05:44:35

问题


I created a UserControl that contains a custom ToolStrip Control. While using the UserControl throughout an application, the ToolStrip control cannot be accessed directly, as it is logically embedded in the UserControl.

So, to access the items of the ToolStrip I defined a readonly property in the UserControl class that returns the items of the ToolStrip.

Now, programmatically the items of the ToolStrip can be edited, by using the UserControl.Items property, but I cannot do the same in the design mode.

Like someone drags the UserControl from the ToolBox to the form, goes to the propertyGrid, chooses the Item's property and manipulates the items of the ingrained ToolStrip accordingly; just as we do with any standard ToolStrip control's items collection with the help of Items Collection Editor.

Herein, though the Items property of the UserControl shows in the propertyGrid, it is not usable and everytime I click, it shows an error message

Value cannot be null. Parameter name: value

I'm sure this is because the property is declared just as readonly and I need to hike more so that, when it is clicked in the propertyGrid, the Items Collection Editor pops-up with the items of the ToolStrip and can be handled consequently.

So how should I be doing this?


回答1:


You can do that by exposing the ToolStrip itself, but it is not a good idea. A UserControl shouldn't expose its internal controls.

If your user control has just a ToolStrip you should consider using a CustomControl which is a class that inherits ToolStrip and you get all its functionality including design capabilities for free.




回答2:


I have been working on the issue for a while, and the two most widely held ideas I got are: 1) Exposing the ToolStrip as a property. 2) Use a ControlDesigner for the UserControl and set EnableDesignMode for the ToolStrip.

But somehow none of them actually served my purpose, as exposing the ToolStrip would let the user have full control on it, which was not my objective. The second option would even put the user up with deleting the ToolStrip. Hence I had to figure out a different approach. That's when I tripped upon the CollectionBase class.

As it says, the CollectionBase class is the abstract base class for a strongly typed collection. So how about maintaining the ToolStrip item collection through it? I created an instance of the CollectionBase class and regulated the addition, modification, fetching and deletion of the ToolStrip items through it. Each item in the collection hails from a class that is inherited from the ToolStripButton class. In that way, individual item can be handled as any standard ToolStripButton and you can hide or show the properties to the user accordingly. Lastly, the ToolStrip in question is set as the parent of the CollectionBase which eventually becomes the parent of each ToolStripItem too. Here's the concept:

Imports System.ComponentModel
Imports System.Windows.Forms
Imports Office2003
Imports System.Drawing
Imports System.Windows.Forms.Design

Public Class MyUserControl
    Private _Buttons As MyUserControlButtons
    Public Event MyUserControlButtonClicked(ByVal Sender As MyUserControlButton)

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        _Buttons = New MyUserControlButtons(MyToolStrip)
    End Sub

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public ReadOnly Property Buttons As MyUserControlButtons
        Get
            Return _Buttons
        End Get
    End Property

    Private Sub MyToolStrip_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MyToolStrip.ItemClicked
     'Check if the ToolStrip item clicked is a ToolStripButton...
        If TypeOf e.ClickedItem Is ToolStripButton Then
            'Insert your code here...

         'Raise the custom event.
            RaiseEvent MyUserControlButtonClicked(Item)
        End If
    End Sub
End Class

#Region "Manager classes for MyUserControl buttons."

Public Class MyUserControlButtons
    Inherits CollectionBase
    Private _Parent As ToolStrip

    ''' <summary>
    ''' Initializes a new instance of the MyUserControlButtons class.
    ''' </summary>
    ''' <param name="Holder">The ToolStrip that contains the items of the MyUserControlButtons class.</param>
    Public Sub New(ByVal Holder As ToolStrip)
        MyBase.New()
        _Parent = Holder
    End Sub

    ''' <summary>
    ''' The ToolStrip that contains this item.
    ''' </summary>
    Public ReadOnly Property Parent() As ToolStrip
        Get
            Return _Parent
        End Get
    End Property

    ''' <summary>
    ''' Gets the element at the specified index.
    ''' </summary>
    ''' <param name="Index">The zero-based index of the element to get.</param>
    Default Public ReadOnly Property Item(ByVal Index As Integer) As MyUserControlButton
        Get
            Return CType(List(Index), MyUserControlButton)
        End Get
    End Property

    ''' <summary>
    ''' Adds an item to the collection.
    ''' </summary>
    Public Function Add() As MyUserControlButton
        Return Me.Add
    End Function

    ''' <summary>
    ''' Adds an item to the collection.
    ''' </summary>
    ''' <param name="Value">The object to add to the collection.</param>
    Public Sub Add(ByVal Value As MyUserControlButton)
        List.Add(Value)
        Value.Parent = Me.Parent
    End Sub

    ''' <summary>
    ''' Adds an item to the collection.
    ''' </summary>
    ''' <param name="Name">The name of the object to add to the collection.</param>
    ''' <param name="Image">The image of the object to add to the collection.</param>
    Public Function Add(ByVal Name As String, ByVal Image As Image) As MyUserControlButton
        Dim b As MyUserControlButton = New MyUserControlButton(Me.Parent)
        b.Name = Name
        b.Text = Name
        b.Image = Image
        Me.Add(b)
        Return b
    End Function

    ''' <summary>
    ''' Adds an item to the collection.
    ''' </summary>
    ''' <param name="Name">The name of the object to add to the collection.</param>
    ''' <param name="Text">The text of the object to add to the collection.</param>
    ''' <param name="Image">The image of the object to add to the collection.</param>
    Public Function Add(ByVal Name As String, ByVal Text As String, ByVal Image As Image) As MyUserControlButton
        Dim b As MyUserControlButton = New MyUserControlButton(Me.Parent)
        b.Name = Name
        b.Text = Text
        b.Image = Image
        Me.Add(b)
        Return b
    End Function

    ''' <summary>
    ''' Removes the first occurence of a specific object from the collection.
    ''' </summary>
    ''' <param name="Value">The object to be removed.</param>
    Public Sub Remove(ByVal Value As MyUserControlButton)
        List.Remove(Value)
    End Sub

    ''' <summary>
    ''' Determines the index of a specific item in the collection.
    ''' </summary>
    ''' <param name="Value">The object to locate in the collection.</param>
    Public Function IndexOf(ByVal Value As Object) As Integer
        Return List.IndexOf(Value)
    End Function

    ''' <summary>
    ''' Determines whether the collection contains a specific value.
    ''' </summary>
    ''' <param name="Value">The object to locate in the collection.</param>
    Public Function Contains(ByVal Value As MyUserControlButton) As Boolean
        Return List.Contains(Value)
    End Function

    Protected Overrides Sub OnInsertComplete(ByVal index As Integer, ByVal value As Object)
        Dim b As MyUserControlButton = CType(value, MyUserControlButton)
        b.Parent = Me.Parent
        Me.Parent.Items.Insert(index, CType(value, ToolStripButton))
        MyBase.OnInsertComplete(index, value)
    End Sub

    Protected Overrides Sub OnSetComplete(ByVal index As Integer, ByVal oldValue As Object, ByVal newValue As Object)
        Dim b As MyUserControlButton = CType(newValue, MyUserControlButton)
        b.Parent = Me.Parent
        MyBase.OnSetComplete(index, oldValue, newValue)
    End Sub

    Protected Overrides Sub OnClearComplete()
        MyBase.OnClearComplete()
    End Sub
End Class

Public Class MyUserControlButton
    Inherits ToolStripButton

    ''' <summary>
    ''' Initializes a new instance of the MyUserControlButton class.
    ''' </summary>
    Sub New()
        MyBase.New()
        Init()
    End Sub

    ''' <summary>
    ''' Initializes a new instance of the MyUserControlButton class.
    ''' </summary>
    ''' <param name="Holder">The ToolStrip that contains the StackView button.</param>
    Sub New(ByVal Holder As ToolStrip)
        MyBase.New()
        Init()
        Me.Parent = Holder
    End Sub

    Private Sub Init()
        Me.AutoToolTip = False
        Me.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
        Me.ImageAlign = ContentAlignment.MiddleLeft
        Me.ImageIndex = -1
        Me.ImageKey = ""
        Me.ImageTransparentColor = Color.Magenta
        Me.TextAlign = ContentAlignment.MiddleLeft
        Me.TextDirection = ToolStripTextDirection.Horizontal
    End Sub

    ''' <remarks>
    ''' Hide this property from the user in both design and code editor.
    ''' </remarks>
    <Browsable(False),
    EditorBrowsable(False)> _
    Public Shadows Property AccessibleDefaultActionDescription As String
        Get
            Return MyBase.AccessibleDefaultActionDescription
        End Get
        Set(ByVal value As String)
            MyBase.AccessibleDefaultActionDescription = value
        End Set
    End Property

    ''' <remarks>
    ''' Hide this property from the user in both design and code editor.
    ''' </remarks>
    <Browsable(False),
    EditorBrowsable(False)> _
    Public Shadows Property AccessibleDescription As String
        Get
            Return MyBase.AccessibleDescription
        End Get
        Set(ByVal value As String)
            MyBase.AccessibleDescription = value
        End Set
    End Property

    ''' <remarks>
    ''' Hide this property from the user in both design and code editor.
    ''' </remarks>
    <Browsable(False),
    EditorBrowsable(False)> _
    Public Shadows Property AccessibleName As String
        Get
            Return MyBase.AccessibleName
        End Get
        Set(ByVal value As String)
            MyBase.AccessibleName = value
        End Set
    End Property


    ' Keep on hiding the irrelevant properties...


    ''' <summary>
    ''' Gets or sets a value indicating whether default or custom ToolTip text is displayed on the ToolStripButton.
    ''' </summary>
    <DefaultValue(False)> _
    Public Shadows Property AutoToolTip As Boolean
        Get
            Return MyBase.AutoToolTip
        End Get
        Set(ByVal value As Boolean)
            MyBase.AutoToolTip = value
        End Set
    End Property

    ''' <summary>
    ''' Specifies whether the image and text are rendered.
    ''' </summary>
    <DefaultValue(GetType(ToolStripItemDisplayStyle), "ImageAndText")> _
    Public Overrides Property DisplayStyle As System.Windows.Forms.ToolStripItemDisplayStyle
        Get
            Return MyBase.DisplayStyle
        End Get
        Set(ByVal value As System.Windows.Forms.ToolStripItemDisplayStyle)
            MyBase.DisplayStyle = value
        End Set
    End Property

    ''' <summary>
    ''' The image that will be displayed on the control.
    ''' </summary>
    Public Overrides Property Image() As Image
        Get
            Return MyBase.Image
        End Get
        Set(ByVal value As Image)
            MyBase.Image = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the alignment of the image on a ToolStripItem.
    ''' </summary>
    <DefaultValue(GetType(ContentAlignment), "MiddleLeft")> _
    Public Shadows Property ImageAlign As ContentAlignment
        Get
            Return MyBase.ImageAlign
        End Get
        Set(ByVal value As ContentAlignment)
            MyBase.ImageAlign = value
        End Set
    End Property

    <EditorBrowsable(False),
    DefaultValue(-1)> _
    Public Shadows Property ImageIndex As Integer
        Get
            Return MyBase.ImageIndex
        End Get
        Set(ByVal value As Integer)
            MyBase.ImageIndex = value
        End Set
    End Property

    <EditorBrowsable(False),
    DefaultValue("")> _
    Public Shadows Property ImageKey As String
        Get
            Return MyBase.ImageKey
        End Get
        Set(ByVal value As String)
            MyBase.ImageKey = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the color to treat as transparent in a ToolStripItem image.
    ''' </summary>
    Public Shadows Property ImageTransparentColor As Color
        Get
            Return MyBase.ImageTransparentColor
        End Get
        Set(ByVal value As Color)
            MyBase.ImageTransparentColor = value
        End Set
    End Property

    ''' <summary>
    ''' Specifies the name used to identify the object.
    ''' </summary>
    Public Shadows Property Name() As String
        Get
            Return MyBase.Name
        End Get
        Set(ByVal value As String)
            MyBase.Name = value
        End Set
    End Property

    ''' <summary>
    ''' The ToolStrip that contains this item.
    ''' </summary>
    <Browsable(False)> _
    Public Shadows Property Parent As ToolStrip
        Get
            Return MyBase.Parent
        End Get
        Set(ByVal value As ToolStrip)
            MyBase.Parent = value
        End Set
    End Property

    ''' <summary>
    ''' The text that will be displayed on the control.
    ''' </summary>
    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the alignment of the text on a ToolStripLabel.
    ''' </summary>
    <DefaultValue(GetType(ContentAlignment), "MiddleLeft")> _
    Public Overrides Property TextAlign As ContentAlignment
        Get
            Return MyBase.TextAlign
        End Get
        Set(ByVal value As ContentAlignment)
            MyBase.TextAlign = value
        End Set
    End Property

    <Browsable(False),
    EditorBrowsable(False),
    DefaultValue(GetType(ToolStripTextDirection), "Horizontal")> _
    Public Overrides Property TextDirection As ToolStripTextDirection
        Get
            Return MyBase.TextDirection
        End Get
        Set(ByVal value As ToolStripTextDirection)
            MyBase.TextDirection = value
        End Set
    End Property


    ' Define other properties accordingly...
End Class

#End Region

MyUserControl is the custom user control containing the ToolStrip named MyToolStrip. MyUserControlButtons is the class that inherits from CollectionBase and each item of MyUserControlButtons is an object of MyUserControlButton class that inherits from ToolStripButton.

In the constructor of MyUserControl, MyUserControlButtons is instantiated with MyToolStrip as its parent. Items are added to MyUserControlButtons by creating new objects from MyUserControlButton. Properties of the items to be hidden or shown are managed in MyUserControlButton. MyUserControl returns the list of the buttons through the read only property 'Buttons'. Note the DesignerSerializationVisibility attribute of the 'Buttons' property. It specifies the type of persistence to use when serializing a property on a component at design time. The 'Content' enumeration member tells the code generator to produce code for the contents of the object, rather than for the object itself.

The drawback in this approach is that you cannot retain the items of the ToolStrip while performing copy-paste on the UserControl. It serializes the contents only when items are handled explicitly by the user but not during copy-paste. Any idea of how to deal with it is highly appreciated.

Thanks to all posters. Though I applied my own concept, your ideas paved my way.




回答3:


I have a solution but for c#

        [Editor("System.ComponentModel.Design.CollectionEditor, System.Design", typeof(UITypeEditor))]
    [MergableProperty(false)]
    [Category("Data")]
    [DescriptionAttribute("ToolStripItems")]
    public virtual ToolStripItemCollection ToolstripItems
    {
        get => toolStrip.Items;
    }


来源:https://stackoverflow.com/questions/12095899/how-to-use-an-items-collection-editor-on-a-custom-usercontrol-net-4-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!