Databind enum to a ComboBox

G

Guest

I'm trying to display values of an enumeration (below) in a combo box.

As a proof of concept, I've attempted to do this via a TypeConverter; but it
would appear that the associated TypeConverter is never instantiated, let
alone called.

Am I missing something silly, or is there some limitation of the ComboBox
that I'm unaware of? -- there certainly seem to be plenty of examples out
there that suggest this *should* work?

Code follows:

Imports System.ComponentModel
Imports System.Globalization

<TypeConverter(GetType(InvoiceDateFormatConverter))> _
Public Enum InvoiceDateFormat
Us = 1
[Ansi] = 2
British = 3
German = 4
Italian = 5
Usa = 10
Japan = 11
Iso = 12
UsCentury = 101
AnsiCentury = 102
BritishCentury = 103
GermanCentury = 104
ItalianCentury = 105
UsaCentury = 110
JapanCentury = 111
IsoCentury = 112
End Enum

Public Class InvoiceDateFormatConverter
Inherits EnumConverter

Public Sub New(ByVal type As Type)
MyBase.New(type)
End Sub

Public Overridable Overloads Function CanConvertTo(ByVal context As
ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
If destinationType Is GetType(String) Then
Return True
End If

Return MyBase.CanConvertTo(context, destinationType)
End Function

' Overrides the ConvertTo method of TypeConverter.
Public Overloads Overrides Function ConvertTo(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object,
ByVal destinationType As Type) As Object
If destinationType Is GetType(String) Then
ConvertToString(context, culture, value)
End If

Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function

''' <summary>
''' Converts the given value to a string representation, using the
specified context and culture information.
''' </summary>
''' <param name="context">An <seealso cref="ITypeDescriptorContext" />
that provides a format context.</param>
''' <param name="culture">A <seealso cref="CultureInfo" /> object. If a
null reference (Nothing in Visual Basic) is passed, the current culture is
assumed.</param>
''' <param name="value">An <seealso cref="Object" /> to convert.</param>
''' <remarks></remarks>
Public Overloads Function ConvertToString(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object)
As String

Dim dateFormatString As String
Select Case CType(value, InvoiceDateFormat)
Case InvoiceDateFormat.Us
dateFormatString = "MM/dd/yy"
Case InvoiceDateFormat.UsCentury
dateFormatString = "MM/dd/yyyy"
Case InvoiceDateFormat.Ansi
dateFormatString = "yy.MM.dd"
Case InvoiceDateFormat.AnsiCentury
dateFormatString = "yyyy.MM.dd"
Case InvoiceDateFormat.British
dateFormatString = "dd/MM/yy"
Case InvoiceDateFormat.BritishCentury
dateFormatString = "dd/MM/yyyy"
Case InvoiceDateFormat.German
dateFormatString = "dd.MM.yy"
Case InvoiceDateFormat.GermanCentury
dateFormatString = "dd.MM.yyyy"
Case InvoiceDateFormat.Italian
dateFormatString = "dd-MM-yy"
Case InvoiceDateFormat.ItalianCentury
dateFormatString = "dd-MM-yyyy"
Case InvoiceDateFormat.Usa
dateFormatString = "MM-dd-yy"
Case InvoiceDateFormat.UsaCentury
dateFormatString = "MM-dd-yyyy"
Case InvoiceDateFormat.Japan
dateFormatString = "yy/MM/dd"
Case InvoiceDateFormat.JapanCentury
dateFormatString = "yyyy/MM/dd"
Case InvoiceDateFormat.Iso
dateFormatString = "yyMMdd"
Case InvoiceDateFormat.IsoCentury
dateFormatString = "yyyyMMdd"
Case Else
dateFormatString = "dd/MM/yyyy"
End Select

Return dateFormatString

End Function

End Class
 
A

Alexander Vasilevsky

May be TypeConverter can be used only in design-time mode?

http://www.alvas.net - Audio tools for C# and VB.Net developers


Rowland Shaw said:
I'm trying to display values of an enumeration (below) in a combo box.

As a proof of concept, I've attempted to do this via a TypeConverter; but
it
would appear that the associated TypeConverter is never instantiated, let
alone called.

Am I missing something silly, or is there some limitation of the ComboBox
that I'm unaware of? -- there certainly seem to be plenty of examples out
there that suggest this *should* work?

Code follows:

Imports System.ComponentModel
Imports System.Globalization

<TypeConverter(GetType(InvoiceDateFormatConverter))> _
Public Enum InvoiceDateFormat
Us = 1
[Ansi] = 2
British = 3
German = 4
Italian = 5
Usa = 10
Japan = 11
Iso = 12
UsCentury = 101
AnsiCentury = 102
BritishCentury = 103
GermanCentury = 104
ItalianCentury = 105
UsaCentury = 110
JapanCentury = 111
IsoCentury = 112
End Enum

Public Class InvoiceDateFormatConverter
Inherits EnumConverter

Public Sub New(ByVal type As Type)
MyBase.New(type)
End Sub

Public Overridable Overloads Function CanConvertTo(ByVal context As
ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
If destinationType Is GetType(String) Then
Return True
End If

Return MyBase.CanConvertTo(context, destinationType)
End Function

' Overrides the ConvertTo method of TypeConverter.
Public Overloads Overrides Function ConvertTo(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As
Object,
ByVal destinationType As Type) As Object
If destinationType Is GetType(String) Then
ConvertToString(context, culture, value)
End If

Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function

''' <summary>
''' Converts the given value to a string representation, using the
specified context and culture information.
''' </summary>
''' <param name="context">An <seealso cref="ITypeDescriptorContext" />
that provides a format context.</param>
''' <param name="culture">A <seealso cref="CultureInfo" /> object. If a
null reference (Nothing in Visual Basic) is passed, the current culture is
assumed.</param>
''' <param name="value">An <seealso cref="Object" /> to
convert.</param>
''' <remarks></remarks>
Public Overloads Function ConvertToString(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As
Object)
As String

Dim dateFormatString As String
Select Case CType(value, InvoiceDateFormat)
Case InvoiceDateFormat.Us
dateFormatString = "MM/dd/yy"
Case InvoiceDateFormat.UsCentury
dateFormatString = "MM/dd/yyyy"
Case InvoiceDateFormat.Ansi
dateFormatString = "yy.MM.dd"
Case InvoiceDateFormat.AnsiCentury
dateFormatString = "yyyy.MM.dd"
Case InvoiceDateFormat.British
dateFormatString = "dd/MM/yy"
Case InvoiceDateFormat.BritishCentury
dateFormatString = "dd/MM/yyyy"
Case InvoiceDateFormat.German
dateFormatString = "dd.MM.yy"
Case InvoiceDateFormat.GermanCentury
dateFormatString = "dd.MM.yyyy"
Case InvoiceDateFormat.Italian
dateFormatString = "dd-MM-yy"
Case InvoiceDateFormat.ItalianCentury
dateFormatString = "dd-MM-yyyy"
Case InvoiceDateFormat.Usa
dateFormatString = "MM-dd-yy"
Case InvoiceDateFormat.UsaCentury
dateFormatString = "MM-dd-yyyy"
Case InvoiceDateFormat.Japan
dateFormatString = "yy/MM/dd"
Case InvoiceDateFormat.JapanCentury
dateFormatString = "yyyy/MM/dd"
Case InvoiceDateFormat.Iso
dateFormatString = "yyMMdd"
Case InvoiceDateFormat.IsoCentury
dateFormatString = "yyyyMMdd"
Case Else
dateFormatString = "dd/MM/yyyy"
End Select

Return dateFormatString

End Function

End Class
 
B

Bob Powell [MVP]

You can databind the combo box to the list of values returned by
Enum.GetValues method.

In this case the standard enum type converter is used.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Rowland said:
I'm trying to display values of an enumeration (below) in a combo box.

As a proof of concept, I've attempted to do this via a TypeConverter; but it
would appear that the associated TypeConverter is never instantiated, let
alone called.

Am I missing something silly, or is there some limitation of the ComboBox
that I'm unaware of? -- there certainly seem to be plenty of examples out
there that suggest this *should* work?

Code follows:

Imports System.ComponentModel
Imports System.Globalization

<TypeConverter(GetType(InvoiceDateFormatConverter))> _
Public Enum InvoiceDateFormat
Us = 1
[Ansi] = 2
British = 3
German = 4
Italian = 5
Usa = 10
Japan = 11
Iso = 12
UsCentury = 101
AnsiCentury = 102
BritishCentury = 103
GermanCentury = 104
ItalianCentury = 105
UsaCentury = 110
JapanCentury = 111
IsoCentury = 112
End Enum

Public Class InvoiceDateFormatConverter
Inherits EnumConverter

Public Sub New(ByVal type As Type)
MyBase.New(type)
End Sub

Public Overridable Overloads Function CanConvertTo(ByVal context As
ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
If destinationType Is GetType(String) Then
Return True
End If

Return MyBase.CanConvertTo(context, destinationType)
End Function

' Overrides the ConvertTo method of TypeConverter.
Public Overloads Overrides Function ConvertTo(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object,
ByVal destinationType As Type) As Object
If destinationType Is GetType(String) Then
ConvertToString(context, culture, value)
End If

Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function

''' <summary>
''' Converts the given value to a string representation, using the
specified context and culture information.
''' </summary>
''' <param name="context">An <seealso cref="ITypeDescriptorContext" />
that provides a format context.</param>
''' <param name="culture">A <seealso cref="CultureInfo" /> object. If a
null reference (Nothing in Visual Basic) is passed, the current culture is
assumed.</param>
''' <param name="value">An <seealso cref="Object" /> to convert.</param>
''' <remarks></remarks>
Public Overloads Function ConvertToString(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object)
As String

Dim dateFormatString As String
Select Case CType(value, InvoiceDateFormat)
Case InvoiceDateFormat.Us
dateFormatString = "MM/dd/yy"
Case InvoiceDateFormat.UsCentury
dateFormatString = "MM/dd/yyyy"
Case InvoiceDateFormat.Ansi
dateFormatString = "yy.MM.dd"
Case InvoiceDateFormat.AnsiCentury
dateFormatString = "yyyy.MM.dd"
Case InvoiceDateFormat.British
dateFormatString = "dd/MM/yy"
Case InvoiceDateFormat.BritishCentury
dateFormatString = "dd/MM/yyyy"
Case InvoiceDateFormat.German
dateFormatString = "dd.MM.yy"
Case InvoiceDateFormat.GermanCentury
dateFormatString = "dd.MM.yyyy"
Case InvoiceDateFormat.Italian
dateFormatString = "dd-MM-yy"
Case InvoiceDateFormat.ItalianCentury
dateFormatString = "dd-MM-yyyy"
Case InvoiceDateFormat.Usa
dateFormatString = "MM-dd-yy"
Case InvoiceDateFormat.UsaCentury
dateFormatString = "MM-dd-yyyy"
Case InvoiceDateFormat.Japan
dateFormatString = "yy/MM/dd"
Case InvoiceDateFormat.JapanCentury
dateFormatString = "yyyy/MM/dd"
Case InvoiceDateFormat.Iso
dateFormatString = "yyMMdd"
Case InvoiceDateFormat.IsoCentury
dateFormatString = "yyyyMMdd"
Case Else
dateFormatString = "dd/MM/yyyy"
End Select

Return dateFormatString

End Function

End Class
 

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

Top