all groups > asp.net building controls > december 2005 >
You're in the

asp.net building controls

group:

create enumerated property for custom control


create enumerated property for custom control Sergey Poberezovskiy
12/29/2005 2:32:04 PM
asp.net building controls: 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.

Re: create enumerated property for custom control Mike MacMillan
12/29/2005 4:04:25 PM
Sergey,
any custom information you'd like displayed by the IDE is design mode
is up to you to implement. take a look at the ControlDesigner class
and its associated members, as that will give you a headstart for
providing custom design-time support for your controls. here's a link
to the SDK:


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuidesigncontroldesignerclasstopic.asp

hope this helps,
Mike MacMillan

[quoted text, click to view]
Re: create enumerated property for custom control Sergey Poberezovskiy
12/29/2005 4:23:02 PM
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..

[quoted text, click to view]
Re: create enumerated property for custom control Mike MacMillan
12/29/2005 5:01:16 PM
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


[quoted text, click to view]
Re: create enumerated property for custom control Sergey Poberezovskiy
12/29/2005 5:34:03 PM
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?

[quoted text, click to view]
Re: create enumerated property for custom control Mike MacMillan
12/29/2005 5:50:38 PM
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


[quoted text, click to view]
Re: create enumerated property for custom control Sergey Poberezovskiy
12/29/2005 6:36:02 PM
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...


[quoted text, click to view]
AddThis Social Bookmark Button