TypeConverter Enum / CheckBox List

Discussion in 'ASP .Net Building Controls' started by Chuck P, Sep 4, 2007.

  1. Chuck P

    Chuck P Guest

    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,
     
    Chuck P, Sep 4, 2007
    #1
    1. Advertising

  2. 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 (, 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.
     
    Walter Wang [MSFT], Sep 5, 2007
    #2
    1. Advertising

  3. Chuck P

    Chuck P Guest

    Oops,
    I wanted to display it in an asp.net composite control's property box.
     
    Chuck P, Sep 5, 2007
    #3
  4. 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 (, 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.
     
    Walter Wang [MSFT], Sep 6, 2007
    #4
  5. 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 (, 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.
     
    Walter Wang [MSFT], Sep 11, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian
    Replies:
    1
    Views:
    467
    Gary Varga
    Jul 18, 2003
  2. Replies:
    0
    Views:
    403
  3. Harshdeep Mehta
    Replies:
    1
    Views:
    2,084
    Teemu Keiski
    Jul 8, 2004
  4. Imar Spaanjaars

    TypeConverter for a generics list

    Imar Spaanjaars, Jan 30, 2008, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    752
    Imar Spaanjaars
    Jan 30, 2008
  5. Imar Spaanjaars

    TypeConverter for a StringCollection or a Generics list

    Imar Spaanjaars, Apr 8, 2008, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    933
    Imar Spaanjaars
    Apr 8, 2008
Loading...

Share This Page