Creating a property with viewable, extensible fields

To clarify what I ask, look at the Font property in the designer. When you click the drop-down menu, you get the options ForeColor, ImeMode, Language, etc.

I need the same kind of drop-down list that allows me to enter several values ​​for the property that I myself add to the designer (this will be for the database property in my custom control and will give you the opportunity to select "Version", "Type", and so on .d.).

How can I do it? I looked through everything and I can not find a solution.

Thanks.

+4
source share
2 answers

, , TypeConverter. , ExpandableObjectConverter - "" . - :

Public Class Widget
    Public Property Name As String
    Public Property FooValue As Integer

    Public Property XYOffset As Offset
     ...

Offset , :

Public Class Offset
    Public Property Name As String
    Public Property X As Int32
    Public Property Y As Int32
    ...
    Public Overrides Function ToString() As String
        Return String.Format("{0}: ({1}, {2})", Name, X.ToString, Y.ToString)
    End Function

, Widget , XYOffset , . ToString() , (WindowsApplication14.SomeType) . PropertyGrid:

enter image description here

, NET , Offset. TypeConverters:

Public Class XYOffsetConverter
    Inherits ExpandableObjectConverter

    ' more to come 
End Class

:

Public Class Offset
     ...
    <TypeConverter(GetType(XYOffsetConverter))>
    Public Property XYOffset As Offset

Offset : <TypeConverter(GetType(ExpandableObjectConverter))>, , CustomControl, TypeConverter (. ). :

enter image description here

NET , String Int32, . :
a) summary Offset (X )
b) "summary", .

. Offset:

Public Class Offset
    <NotifyParentProperty(True)>
    Public Property Name As String
    <NotifyParentProperty(True)>
    Public Property X As Int32
    <NotifyParentProperty(True)>
    Public Property Y As Int32

:

enter image description here

B - , , ToString() . , , .

, , , , (). TypeConverters , , .

, VS.


, . VS , , Offset, . TypeConverter. , .

+6

TypeConverterAttribute System.ComponentModel.ExpandableObjectConverter .

, "drop down".

:

Imports System.ComponentModel

<TypeConverterAttribute(GetType(System.ComponentModel.ExpandableObjectConverter))>
Public Class MyPropertyClass
    Public Property Name As String
    Public Property Value As Integer
End Class

, :

<Browsable(True)> _
Public Property MyProperty As New MyPropertyClass

:

Dropdown property.

+1

Source: https://habr.com/ru/post/1605808/


All Articles