Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Something that I've seen in many user controls that for the life of me I cannot figure out:

 

Let's say you build a control that has a dropdown list in the properties at design time. Pretty easy if you use an enumeration. How, though, would you go about creating the same dropdown list for a property if you don't know what goes in the list until the control is painted on form, like say a list of devices enumerated on the system so that the designer can easily set the default device at design time? You can't really use an enum for that because you don't know what will be enumerated.

 

I know that this is possible(and probably way too simple for me to figure out) because I've used controls that do this and often wondered how. Been trying to figure it out for a while now.

 

Thanx

Posted
You need to give your property a TypeDescriptor' date=' and override its GetStandardValues method. Searching for those two keywords should get you a load of samples.[/quote']

 

Thank you divil for the advice. The technique has already come in handy a couple of times.

 

Have an example for your site...

 

   'This example demonstrates adding a dropdown list to a usercontrol that 
   'is not populated until the control is painted on a container at 
   'design time, similar to using an enumeration as a property.
   'This is useful when you do not know what will be in the list until
   'the designer paints the control, for example - Retrieving a list
   'of devices installed on the system and placing them in the PropertyGrid.
   '
   'Imports System.ComponentModel

   'Create a tank to hold the property's value
   Private m_MyProperty As String

   'Create the TypeConverter
   Private Class MyPropertyTypeConverter : Inherits StringConverter

       'The function names will always be exactly the same.
       Public Overloads Overrides Function GetStandardValuesSupported _
       (ByVal context As System.ComponentModel.ITypeDescriptorContext) _
               As Boolean
           Return True
       End Function

       'This is where you create your values.
       'Note: This function is called each time MyProperty has 
       'focus on the grid and can be updated with a list of new 
       'values at any time.
       Public Overloads Overrides Function GetStandardValues _
       (ByVal context As System.ComponentModel.ITypeDescriptorContext) _
       As System.ComponentModel.TypeConverter.StandardValuesCollection

      'Create an arraylist of strings that you want to see in the property's list.
           Dim AnArrayList As New ArrayList()
           AnArrayList.Add("Stuff")
           AnArrayList.Add("Junk")
           AnArrayList.Add("Crap")

     'Return the list to your property.
           Dim SVC As New StandardValuesCollection(AnArrayList)
           Return SVC

       End Function

   End Class

   'Declare the property with TypeConverter Attribute
   <TypeConverter(GetType(MyPropertyTypeConverter)), _
   NotifyParentProperty(True), _
   Bindable(False)> _
   Public Property MyProperty()
       Get
           Return m_MyProperty
       End Get
       Set(ByVal Value)
           m_MyProperty = Value
       End Set
   End Property

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...