none
How can I integrate usercontrol's properties into form's properties? RRS feed

  • Question

  • I have create a custom form and a UserControl which acts like custom form's title bar. What I want to achieve, is to integrate UserControl's properties into custom form's properties. Something like nested properties or something like this...



    In this case we have Button's properties integrated into TextBox's properties. Any idea how can I do that?

    Thank you in advance!!!

    Mediocrity is my greatest enemy!!!

    Friday, May 11, 2018 1:09 PM

All replies

  • I don't have an example but the right direction is to use MergableProperty attribute 

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, May 11, 2018 2:10 PM
    Moderator
  • I think you'll want an implementation of an ExtenderProvider.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, May 11, 2018 2:14 PM
    Moderator
  • I don't know if this will help or not Design-Time Attributes for Components

    La vida loca

    Friday, May 11, 2018 4:17 PM
  • Removed due to bugs....

     However,  you may also want to look into using the ExpandableObjectConverter which I mentioned in This Thread of yours.  You would need a class which holds any properties you want shown to the user and add a Property to the usercontrol which is of that class type.  In that thread I just linked to,  your class would be implemented like the SidePadding class which is an expandable property in that example.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Friday, May 11, 2018 10:14 PM
    Friday, May 11, 2018 8:59 PM
  • Okay, I had sometime to play with this and came up with a decent solution where we begin with constituent controls within a user control. see How to: Expose Properties of Constituent Controls.

    With that knowledge it all seems (and it is) all too easy to expose controls on a user control. Here I added a ComboBox and Button to the user control and set access to them in the property window after dropping the user control on a form.

    Public Class UserControl1
        Public Property ComboBox() As ComboBox
            Get
                Return (ComboBox1)
            End Get
            Set(ByVal value As ComboBox)
                ComboBox1 = value
            End Set
        End Property
        Public Property MyButton() As Button
            Get
                Return (Button1)
            End Get
            Set(ByVal value As Button)
                Button1 = value
            End Set
        End Property
    End Class
    
      

    Sadly after setting properties, build/run the settings are gone so with a little magic we can fix this by with several attributes.

    The key is DesignerSerializationVisibility, setup with DesignerSerializationVisibility.Content so properties persist in the form you placed the user control on.

    So we end up with the following.

    Imports System.ComponentModel
    Imports System.Drawing.Design
    
    ''' <summary>
    ''' Note Category is AAA because it brings the category to the top
    ''' in the property window
    ''' </summary>
    Public Class UserControl1
        Public Sub New()
            InitializeComponent()
        End Sub
        <Category("AAAA"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
            Editor("DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                   GetType(UITypeEditor))>
        Public Property ComboBox() As ComboBox
            Get
                Return (ComboBox1)
            End Get
            Set(ByVal value As ComboBox)
                ComboBox1 = value
            End Set
        End Property
    
        <Category("AAAA"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
            Editor("DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                   GetType(UITypeEditor))>
        Public Property MyButton() As Button
            Get
                Return (Button1)
            End Get
            Set(ByVal value As Button)
                Button1 = value
            End Set
        End Property
    End Class
    
    Hopefully this suits your needs.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by IronRazerz Friday, May 11, 2018 10:13 PM
    Friday, May 11, 2018 9:26 PM
    Moderator