|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionEnums are great. They define a set of permissible values and provide a way of type-checking parameters for functions and methods. The problem is that there is a bit of gap between Enums and user interfaces. So I created the RadioButtonEnum and ListBoxEnum controls allow you to bind a control to an Enum type automatically. BackgroundI got fed up of building these UI components manually and realised I should not be copying values and text from the class to the UI, when the .NET framework can do this for me. Using the codeHow often do you see this in your code: Public Enum Genders
Male
Female
End Enum
And the interface component thus: <asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="0" Text="Male" />
<asp:ListItem Value="1" Text="Female" />
</asp:RadioButtonList>
Then the code changes, for example: Public Enum Genders
NotSpecified
Male
Female
End Enum
Now your interface control is mismatched and an existing value of 1 (female originally) now gets displayed as 'Male' and will throw an exception if you try to bind ApproachI realised that it should be fairly simple to subclass a First I created a subclass of the Public Class RadioButtonEnum
Inherits RadioButtonList
Then I added EnumType property where you specify the typename of the enum. Although this worked fine for a global Enum such as When adding an enum from a module (other than the current module) you also need to specify this. e.g. if MyClass is in Naming IssuesAlthough getting the values and names for an enum type are fairly simple, the naming restrictions of enum values in code are quite tight - alphanumeric and underscores are the only permitted characters. Although an Enum value of SendToCustomer is fine in code, it's not really good practice to use this value in the user interface. To address this I added a FixNames property which can be set to true to adjust enum values with underscores or CamelCase to more readable ones. Thus SendToCustomer becomes Send To Customer in the control. And yet there might be situations where we want other characters, e.g. Urgent! - or extended character sets. I therefore added the capability to look for a Description attribute (System.ComponentModel.DescriptionAttribute). The actual Enum value items are accessible as Fields of the Enum type, so the function Public Enum BookRatingWithDescription
<Description("A <b>great</b> book!")> GreatBook
<Description("Enjoyable read")> Enjoyable
<Description("Not bad - ok")> OK
Poor
<Description("Where's the shredder?")> Terrible
End Enum
HTML TrickA useful trick is that the text of Description attribute can contain HTML tags (this works with a RadioButtonEnum but not a ListBoxEnum) as shown in the first enum value above. I also use XML a lot, so I added the ability to use XmlEnumAttribute values if these are present, and the UseXmlNames property is set to True. ExampleTo test this I've included a web site that demonstrates the various functions. Here is the web page in design mode:
When the page is displayed the controls are filled automatically - hurray!
In the Enum with descriptions the first item uses the HTML tags in the description. The last two examples show the same Enum - in the first instance with ConclusionWell thats it - no more copying the enum values into your controls. Hope you find it useful. HistoryVersion 1.0: RadioButtonEnum and ListBoxEnum. Although written in VS2005 it should be possible to modify this code to work on .NET 1.x by changing the references to generic collections to supported types in 1.x.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||