Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I asked the question and Mr Ralf Meier suggested to use Enum and it works perfectly. My happiness doesn't last long as later on I found that enum is fixed type. I can not resize it. So, i tried using list but I am undone. Actually I want bring some data from database and set as property list. Can Anyone please help me. Thanks a lot in advance.

What I have tried:

VB
Private _objList As List(Of String)

Property ShahObjList As List(Of String)
        Get
            Return _objList
        End Get
        Set(value As List(Of String))
            _objList = value
        End Set
    End Property


Using this I could set the property as a collection but I can not select from this collection. As for example i have a, b, c ,d and I will select c. Then the custom control do the job accordingly. But for me when I select any of these (a/b/c/d), again it goes back to a, that means index 0. Thanks.
Posted
Updated 18-Feb-21 23:55pm
v3

This is not so easy to do ... but I have a Solution for you - but this Solution has it's Rules.
At first your Property it must be like this :
VB
<TypeConverter(GetType(DropDownConverter))>
Property ProjektDatei As String
    Get
        Return my_ProjektDatei
    End Get
    Set(value As String)
        my_ProjektDatei = value
    End Set
End Property
Private my_ProjektDatei As String = ""

<Browsable(False), EditorBrowsable(EditorBrowsableState.Never)>
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Property ProjektDateiArray As String() = {"1", "2", "3", "4"}


As you see my Property is a single String. The Array with your Data is the following Property. To make it work you need a special TypeConverter. This TypeConverter is written by me and you have to add it to your Application :
VB.NET
Imports System.ComponentModel

Public Class DropDownConverter
    Inherits StringConverter

    ' Example for using :
    '=============================

    ' <TypeConverter(GetType(DropDownConverter))>
    ' Property ProjektDatei As String
    '    Get
    '        Return my_ProjektDatei
    '    End Get
    '    Set(value As String)
    '        my_ProjektDatei = value
    '    End Set
    ' End Property
    ' Private my_ProjektDatei As String = ""

    ' <Browsable(False), EditorBrowsable(EditorBrowsableState.Never)>
    ' <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    ' Property ProjektDateiArray As String()
    ' !!! thgis Property must allways have the name of the Property to which it points and also the name-Extention 'Array'
    ' !!! this Property contains the List of chossable Options and could be of Type String() , List(of String) or Collection 

 

    Public Overloads Overrides Function GetStandardValuesSupported(ByVal context As ITypeDescriptorContext) As Boolean
        Return True 'True teilt dem PropertyGrid mit, dass es eine Combobox anzeigen soll
    End Function

    Public Overloads Overrides Function GetStandardValuesExclusive(ByVal context As ITypeDescriptorContext) As Boolean
        Return True
        ' True makes the Combobox only selectable
        ' False also allows free input
    End Function

    Private EntriesArray(-1) As String  'stores the chooseable entries for the DropDown

    Public Overrides Function CanConvertFrom(context As System.ComponentModel.ITypeDescriptorContext, sourceType As System.Type) As Boolean
        If sourceType Is GetType(String) Then Return True 'allow konverting of strings
        Return MyBase.CanConvertFrom(context, sourceType)
    End Function

    Public Overloads Overrides Function GetStandardValues(ByVal context As ITypeDescriptorContext) As StandardValuesCollection
         Dim myPD As PropertyDescriptor = Nothing

        Dim myAttribute As DropDownConverterDataAttribute = context.PropertyDescriptor.Attributes(GetType(DropDownConverterDataAttribute))
        If myAttribute IsNot Nothing Then  ' is the name of the array given as Attribute ...?
            myPD = TypeDescriptor.GetProperties(context.Instance)(myAttribute.DataArray)

        Else  ' comes the name of the selected array from the name of the Property ...?
            ' get Host-Property and Options 
            Dim HostPropertyName As String = context.PropertyDescriptor.Name
            Dim HostPropertyArrayName As String = HostPropertyName + "Array"
            myPD = TypeDescriptor.GetProperties(context.Instance)(HostPropertyArrayName)

        End If

        If myPD IsNot Nothing Then
            If myPD.PropertyType Is GetType(String()) Then
                EntriesArray = myPD.GetValue(context.Instance)
            ElseIf myPD.PropertyType Is GetType(List(Of String)) Then
                Dim myList As List(Of String) = myPD.GetValue(context.Instance)
                ReDim EntriesArray(myList.Count - 1)
                For i As Integer = 0 To myList.Count - 1
                    EntriesArray(i) = myList(i)
                Next
            ElseIf myPD.PropertyType Is GetType(Collection) Then
                Dim myCollection As Collection = myPD.GetValue(context.Instance)
                ReDim EntriesArray(myCollection.Count - 1)
                For i As Integer = 0 To myCollection.Count - 1
                    EntriesArray(i) = myCollection.Item(i + 1)
                Next
            End If
        End If

        Return New StandardValuesCollection(EntriesArray) 'Exports the selected options
    End Function

End Class

' =================================================================================================

<AttributeUsage(AttributeTargets.[Property] Or AttributeTargets.Method)> _
Public Class DropDownConverterDataAttribute
    Inherits Attribute

    Public Sub New(DataArray_PropertyName As String)
        my_DataArray = DataArray_PropertyName
    End Sub

    Public ReadOnly Property DataArray() As String
        Get
            Return my_DataArray
        End Get
    End Property
    Private my_DataArray As String

End Class


I suggest you use the code as given ...
Perhaps you have to import also the used Namespaces to your Application.
 
Share this answer
 
Comments
Shah Samiur Rashid 20-Feb-21 22:46pm    
Thanks a lot Mr Ralf. Let me see your kind/valuable suggestions in work and get back to you soon. Thanks again.
Ralf Meier 21-Feb-21 3:52am    
You are welcome.
I think there could be some questions - feel free to ask them ...
Shah Samiur Rashid 22-Feb-21 1:23am    
Ha ha. You are so kind Mr Ralf. I am a bit busy. I will get back soon to irritate all of you. ha ha ha.....
Ralf Meier 24-Feb-21 2:52am    
Hi ... what's the success until now ?
Does it work like you want ?
Shah Samiur Rashid 24-Feb-21 22:19pm    
Thanks Mr Ralf. I will start with this one, next week. Obviously I will let you know. Thanks a lot. Stay safe.
Your problem is not quite clear, especially this:
Quote:
when I select any of these (a/b/c/d), again it goes back to a, that means index 0


Assuming that you want to return index of selected object in List(Of String)

VB
Dim objList As List(Of String) = New List(Of String)() From {"A", "E", "I", "J", "O", "Q"}
Dim selectedObj As String = "J"
Dim selectedIndex As Integer = objList.IndexOf(selectedObj) 'returns 3 as arrays are zero-based indexed


If you want to achieve somethig else, you have to be more specific.
 
Share this answer
 
v2
Comments
Shah Samiur Rashid 20-Feb-21 22:56pm    
Thanks a lot for your time. Normally in the customize control class array can not be used as property type, so far I have seen. But I need to use array type of property as I want user can select his option from the property panel during design time and the customized control can work accordingly. Please let me know whether I could state it clearly. Thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900