Click here to Skip to main content
15,065,044 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.
   
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.
Shah Samiur Rashid 1-Mar-21 12:39pm
   
Hi Ralf. Hope you are doing fine. I have started to digest your suggestions and coding as I am new in handling this kind of classes. I am coming back to you soon.
Ralf Meier 1-Mar-21 14:50pm
   
I'm fine thanks ...
I'm wondering that I haven't heard anything from you until now. In my opinion this must be the Solution for you.
Shah Samiur Rashid 2-Mar-21 1:50am
   
I am sorry Mr Ralf. Very very sorry. I told you before that I was busy with other issues. Anyway, Today I could apply your codes to customize a combobox control having list of data from database. It works for me 100%. Though I could not understand many of the codes but successfully I could implement your codes to customize a control having dynamic array coming from database as a selection option for a property of the control. Thanks a lot Mr Ralf. And apologize again. Bye. Stay safe.
Shah Samiur Rashid 2-Mar-21 1:53am
   
Mr Ralf, is it possible to get your contact e-mail. If you don't like it, i would be sorry. If you want I can give my email. Thanks a lot.
Ralf Meier 2-Mar-21 3:40am
   
I'm glad to hear that my Solution works for you ...
If you have further questions you can ask it here ...
I don't want to provide my EMail-adress in this Forum - but of course you can give me yours ...

Shah Samiur Rashid 2-Mar-21 8:57am
   
Its OK Mr Ralf that you don't want to pass your email here. However, thanks a lot for your time to help me sorting out my problem. Anyway, I am working on it and trying to implement in my project on my way. Anyway, Thanks a lot again. Stay safe.
Ralf Meier 2-Mar-21 11:57am
   
You are welcome ... and if there are further question ... no problem ... feel free to contact me ...
Shah Samiur Rashid 4-Mar-21 9:48am
   
Hi Mr Ralf. Hope you are doing fine. Now I can include data from mysql database into property with your help. But now I am facing another challenge. I have two properties such as A and B. A property is fixed but the other property B is dependent on the property A value. I am talking about a custom control. So list of the property B should be changed during design time based on the selection of property A option. Then option from property B will be selected. I search online a lot but could not get any example like this. Maybe my searching was not correct. Can you please help me. Thanks a lot.
Ralf Meier 4-Mar-21 10:03am
   
Perhaps I could help you ... but in the Moment I don't understand what you mean.
Could you give me an example what you do and what should happen ?
Customized Controls are a Speciality of me - so I think I would find a Solution ...

Another thing : what is your first name ? Is it Shah ... or is it Rashid ? My first name is Ralf and you need not to say Mister ... ;-)
Shah Samiur Rashid 4-Mar-21 10:30am
   
Thanks a lot. I am trying to customize a combobox control with some customize properties for my project. So, there will be two properties one is A) DataSourceOption and another one is B) DataSourceName. So the first one is fixed using enum type. In this property user will select (Table/View/Procedure/SQL). Based on this the other property (B) will be changed. If user choose Table on property A, all the tables will be listed in the property B during Design Time. Then user will choose which table and combobox data will be generated during runtime and filtering option will be there as well. Please let me know whether it is clear. Thanks Ralf. You can find my mail below.
Ralf Meier 4-Mar-21 10:38am
   
I have allready send you an EMail ...
and ...
It's still not clear for me ...
Shah Samiur Rashid 4-Mar-21 12:12pm
   
Please find my response in your email. Thanks a lot.
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.
   
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