create enumerated property for custom control

  • Thread starter Sergey Poberezovskiy
  • Start date
S

Sergey Poberezovskiy

Hi,

I want to create a property that can only accept values from my custom list.
Say
1, 5, 10, 15, 20, 30.

I know I can easily create an enumeration with values set to those from my
list, and have the Visual Studio designer display a list of enumerated
strings shown in the property (e.g. one, five, ten, etc.).

What I want is a list of numbers shown in the IDE - and I have no idea how to
do that...

Any help is greatly appreciated.
 
S

Sergey Poberezovskiy

Mike,

I was not asking about the DesignTimeHtml - I know how to use that...
The question was about how to implement a property similar to say,
BorderStyle, so that when user selects the property in the Properties window
the valid values are listed in the dropdown box.

Hope that makes sense..
 
M

Mike MacMillan

Sergey,
any properties who's type are an enum will be bound to that list of
values in design mode. if you view properties of the control/class
who's property you'd like to edit, if one is an enum, VS will display a
dropdownlist with the values from the enum as a list of available
choices.

Mike MacMillan
 
S

Sergey Poberezovskiy

Mike,

As you could see from the very first post the type of property is integer
(1, 5, 10...)
I have no problem restricting the user to enter those values only by raising
an error in the property set block. My goal is that the user can select one
of the valid values in the dropdown.

The property I am trying to implement is the minute interval on TimePicker
control - it makes sense to make it of an integer (byte) type.

Any ideas?
 
M

Mike MacMillan

Sergey,
perhaps you can define an enumeration (as you said in the first
post), then replace the direct int property assignment with the enum.
this would restrict the user's selection in design time(and in general)
to only the values defined in the enum. then, in the set accessor of
the enum's property, you can set an internal int value that represents
the actual int value of the selected enum member. this would pretty
much do as you requested, unless i misunderstand (please let me know).

hope this helps,
Mike MacMillan
 
S

Sergey Poberezovskiy

Mike,

While I was trying to reason an answer to you that your proposed solution is
not exactly that I wanted, I worked out the way to do it:

1. Define a converter class, say
2. Set property attributes

The code follows:

1.

Public Class ShortConverter
Inherits Int16Converter

Public Shared defaultEnum As StandardValuesCollection

Public Overloads Overrides Function GetStandardValuesSupported(ByVal
context As System.ComponentModel.ITypeDescriptorContext) As Boolean
Return True
End Function

Public Overloads Overrides Function GetStandardValuesExclusive(ByVal
context As System.ComponentModel.ITypeDescriptorContext) As Boolean
Return True
End Function

Public Overloads Overrides Function GetStandardValues(ByVal context As
System.ComponentModel.ITypeDescriptorContext) As
System.ComponentModel.TypeConverter.StandardValuesCollection
Return defaultEnum
End Function
End Class

2. in the control class:

Private Const _DEF_MINUTES_INTERVAL As Short = 1
Private Shared _intervals() As Short = {_DEF_MINUTES_INTERVAL, 5, 10, 15,
20, 30}

<TypeConverter(GetType(ShortConverter)), _
Description("Specifies minutes intervals."), _
DefaultValue(_DEF_MINUTES_INTERVAL)> _
Public Property MinutesInterval() As Short
Get
If Me.Context Is Nothing Then
ShortConverter.defaultEnum = New
TypeConverter.StandardValuesCollection(_intervals)
End If
Return Common.GetInt16Property(Me.ViewState, "MinutesInterval",
_DEF_MINUTES_INTERVAL)
End Get
Set(ByVal Value As Short)
If Array.IndexOf(_intervals, Value) > -1 Then
Common.SetInt16Property(Me.ViewState, "MinutesInterval", Value,
_DEF_MINUTES_INTERVAL)
End If
End Set
End Property

Hope that may help someone else...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top