TypeConverter Enum / CheckBox List

C

Chuck P

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,
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 
W

Walter Wang [MSFT]

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/library/c5z9s1h4(VS.80).aspx

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

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

Regards,
Walter Wang ([email protected], 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.
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top