[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Chuck P

9/4/2007 7:22:00 PM

Is their an easy way to create a TypeConverter for enums you want displayed
as a CheckBox List.

For example

enum AllowTheseCars
Ford
Chevy
Toyota
Porsche

In the Control
the user would see a Property called
"Allowed Cars"
with all of the enums and a checkbox in front of each one

Allowed Cars
x Ford
Chevy
Toyota
x Porsche

The above example allows Fords and Porsche cars.

thanks,

4 Answers

wawang

9/5/2007 8:56:00 AM

0

Hi Chuck,

I'm not sure if a TypeConverter can be used here, normally a TypeConverter
is applied to an Enum if we need to convert it to other Enum types (maybe a
mapping between two applications).

Instead, I would suggest to create two shared/static functions and "bind"
any enum to a CheckBoxList on demand:


Public Shared Sub BindEnumToCheckBoxList(ByVal enumType As Type, ByVal
enumValue As Integer, ByVal cbl As CheckBoxList)
cbl.Items.Clear()
For Each i As Integer In System.Enum.GetValues(enumType)
Dim item As New ListItem(System.Enum.GetName(enumType, i),
i.ToString())
If (enumValue And i) = i Then
item.Selected = True
End If
cbl.Items.Add(item)
Next
End Sub

Public Shared Function GetCheckBoxListValue(ByVal cbl As CheckBoxList)
As Integer
Dim ret As Integer = 0
For Each item As ListItem In cbl.Items
If item.Selected Then
ret = ret Or Int32.Parse(item.Value)
End If
Next
Return ret
End Function


In your custom control, if you need to show a CheckBoxList that
corresponding to an Enum, simply create the CheckBoxList and bind it to the
enum once. Later you use the other function to read back the value from the
CheckBoxList and convert it into the Enum type:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindEnumToCheckBoxList(GetType(AllowTheseCars), 0,
CheckBoxList1)
End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim checkedCars As AllowTheseCars =
CType(GetCheckBoxListValue(CheckBoxList1), AllowTheseCars)
Stop
End Sub



Hope this helps.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Chuck P

9/5/2007 3:48:00 PM

0

Oops,
I wanted to display it in an asp.net composite control's property box.


wawang

9/6/2007 8:00:00 AM

0

Hi Chuck,

To do that, you will need a custom UI Type Editor; possibly along with a
Type Converter. Note a Type Converter only helps to convert between
different types, at design-time, it's used to return a string
representation of a data type so that it can be serialized by the designer.

By default, an Enum data type will be show as a comboxbox in the
PropertyGrid. To make it show as a dropdown checkbox list, you will need a
custom UI Type Editor.

Please refer to following document for more information:

#Design-Time Architecture
http://msdn2.microsoft.com/en-us/librar...(VS.80).aspx

#How to: Implement a UI Type Editor
http://msdn2.microsoft.com/en-us/librar...(VS.80,d=printer).aspx

Hope this helps. I can help write a working example if needed.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

wawang

9/11/2007 2:10:00 AM

0

Hi Chuck,

Do you need anything else? Please feel free to let me know if you need more
information on the UI Type Editor. Thanks.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.