none
How to create a custom control with multiple values in its property

    Question

  • I want to create a custom control that has multiple value properties. For example the Size property of any toolbox control, it takes two parameters a width and a height.

    So how can i create a property of a control that will take two or more values as inputs to its Set method, or by any other method, and we can use them in a single property?

    Tuesday, April 18, 2017 9:08 AM

Answers

  • Hi James,

    As Viorel mentioned, this behavior is controlled with a TypeConverter attribute.  In this case you want to apply the ExpandableObjectConverter to your custom class:

    <System.ComponentModel.TypeConverter(GetType(System.ComponentModel.ExpandableObjectConverter))>

    So for example, if we had a "GPS Coordinate" class, it might look like:

    <System.ComponentModel.TypeConverter(GetType(System.ComponentModel.ExpandableObjectConverter))>
    Public Class GPSCoord
        Public Property Lat As Double
        Public Property Lon As Double
        Public Property Alt As Double
    
        Public Overrides Function ToString() As String
            Return $"GPS Coord: {Lat}/{Lon} {Alt}"
        End Function
    End Class

    An example of a control with this class as a property value might then be:

    Imports System.ComponentModel
    
    Public Class UserControl1
        Public Property Coordinates As New GPSCoord
    
        Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    End Class

    You can create a custom type converter if you want more functionality, such as parsing from a string, but to just get the expansion you can use the default ExpandableObjectConverter.

    Note that when the object is a reference type, you must create a new instance or the property grid will not be able to expand the property value (there is no + shown if the property value is set to Nothing).


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

    Wednesday, April 19, 2017 11:48 AM
    Moderator

All replies

  • So how can i create a property of a control that will take two or more values as inputs to its Set method, or by any other method, and we can use them in a single property?

    If the Size property is defined as a Size object it will have the two fields that you require.
    https://msdn.microsoft.com/en-us/library/system.drawing.size%28v=vs.110%29.aspx

    Tuesday, April 18, 2017 9:30 AM
  • I apologize for not making my question clear 

    i want to create something similar to the Size property that it should take more than one arguments when i call the property of the custom control.

    For example: if i create a custom control named "CustomComboBox" and then define a property named "addMovies"

    now when i access this property in my code :

    customComboBox.addMovies("value1", "value2", "value3")


    By default if i create a property in my custom control it wold be something like this:

    Private multipleMovies As String
        Public Property AddMovies() As String
            Get
                Return multipleMovies 
            End Get
            Set(ByVal value As String)
                multipleMovies = value
            End Set
        End Property

    So i want to change this property such that, it takes more than one value  

    • Edited by James Kadra Tuesday, April 18, 2017 10:14 AM typo problem, it was String instead of Boolean
    Tuesday, April 18, 2017 9:40 AM
  • So i want to change this property such that, it takes more than one value  

    A property cannot represent more than one value.  But it can represent an object that has multiple properties.  For instance, it can represent a Size object which has two properties - X and Y.

    So to get multiple values for your MultipleMovies property, create a class that has the properties (value1, value2, value3) that you want, and define your property as that Type.

    If you create the appropriate constructor for your class you can pass all the values in a list.
    CustomComboBox.AddMovies = New MultipleMovies("Mad Max", "The Patriot", "Braveheart")

    The example code you posted is not valid for a property, whatever its Type. A property is 'set' using an assignment. 

    You could, of course, create an add method that took a number of arguments and created a value for the separate properties. 

    Public Class Movies
        Public Property Value1 As String
        Public Property Value2 As String
        Public Property Value3 As String
    
        Public Sub AddMovies(V1 As String, V2 As String, V3 As String)
            Value1 = V1
            Value2 = V2
            Value3 = V3
        End Sub
    
    End Class
     So your code would be

        CustomComboBox.AddMovies("Mad Max", "The Patriot", "Braveheart")

    If the number of values will vary, then define your property as a Collection. That way, you can add as many items to the collection as you want.



    • Edited by AcamarMVP Tuesday, April 18, 2017 11:14 AM sp
    Tuesday, April 18, 2017 11:11 AM
  • Hi James,

    According to your description, you want to create a custom control, and it has one property like: customComboBox,addMovies("Value1","Value2","Value3"). I find the similar case that you can refer to:

    https://social.msdn.microsoft.com/Forums/en-US/0d082e0f-ad6e-4d42-b2b3-da22915281c3/create-custom-control-with-multiple-value-properties?forum=vbgeneral

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 19, 2017 6:06 AM
    Moderator
  • Seems that the behaviour of properties in Property grid can be based on special classes derived from TypeConverter, which in case of Size is SizeConverter: http://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/SizeConverter.cs.

    See also the MSDN documentation for TypeConverter.

    Try experimenting with a structure similar to Size, for example:

    [TypeConverter( typeof( MyTypeConverter ) )]
    public class MyStruct
    {
       public int First { get; set; }
       public int Second{ get; set; }
    }

    The source of SizeConverter will help.


    • Edited by Viorel_MVP Wednesday, April 19, 2017 8:37 AM
    Wednesday, April 19, 2017 8:37 AM
  • Hi James,

    As Viorel mentioned, this behavior is controlled with a TypeConverter attribute.  In this case you want to apply the ExpandableObjectConverter to your custom class:

    <System.ComponentModel.TypeConverter(GetType(System.ComponentModel.ExpandableObjectConverter))>

    So for example, if we had a "GPS Coordinate" class, it might look like:

    <System.ComponentModel.TypeConverter(GetType(System.ComponentModel.ExpandableObjectConverter))>
    Public Class GPSCoord
        Public Property Lat As Double
        Public Property Lon As Double
        Public Property Alt As Double
    
        Public Overrides Function ToString() As String
            Return $"GPS Coord: {Lat}/{Lon} {Alt}"
        End Function
    End Class

    An example of a control with this class as a property value might then be:

    Imports System.ComponentModel
    
    Public Class UserControl1
        Public Property Coordinates As New GPSCoord
    
        Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    End Class

    You can create a custom type converter if you want more functionality, such as parsing from a string, but to just get the expansion you can use the default ExpandableObjectConverter.

    Note that when the object is a reference type, you must create a new instance or the property grid will not be able to expand the property value (there is no + shown if the property value is set to Nothing).


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

    Wednesday, April 19, 2017 11:48 AM
    Moderator