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.