Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2008

Tip: Looking for answers? Try searching our database.

Databind enum to a ComboBox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rowland Shaw - 19 Nov 2007 12:10 GMT
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
Alexander Vasilevsky - 22 Nov 2007 12:52 GMT
May be TypeConverter can be used only in design-time mode?

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

> I'm trying to display values of an enumeration (below)  in a combo box.
>
[quoted text clipped - 120 lines]
>
> End Class
Bob Powell [MVP] - 15 Feb 2008 08:09 GMT
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.

Signature

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.

> I'm trying to display values of an enumeration (below)  in a combo box.
>
[quoted text clipped - 116 lines]
>
> End Class

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.