1. Technology

Displaying Enumerated Properties in a Selectable List

Run-Time Enum Selection in Delphi


Enumerated Values in a Selection-Type Delphi Control

Enumerated Values in a Selection-Type Delphi Control

Properties are the most visible parts of a Delphi control / component. When designing the user interface of your Delphi, at design-time, application you use the Object Inspector to set control properties.

Properties can be simple (string, boolean and integer values, for example) or can be of a more complex type, like enumerations, sets or arrays.

Enum / Enumerated properties at Run-Time
At design-time properties of enumerated types (including Boolean) appear as editable strings. You can also cycle through the possible values by double-clicking the value column, and there is a drop-down list that shows all possible values.

ListView's ViewStyles in RadioGroup

For example, let's consider the ViewStyle property of the TListView control. ViewStyle determines the visual display of items in a list view. The items can be displayed as a set of movable icons, or as columns of text.

If you need to allow a user of your application to change the display of the ListView at run-time, you need to provide some sort of "selectable" user interface. Controls like ComboBox, RadioGroup or ListBox can be used - they all display a collection of selectable items.

For this example, we'll use a TRadioGroup control (group of radio buttons). When the user checks a radio button, all other radio buttons in its group become unchecked. If each radio button represents one enum value from an enumerated type property of a control - when the item is selected - the enum value can be assigned.

Here's how to populate a radio group control with the possible values for the ViewStyle ListView's property:

 uses TypInfo, ... 

 //List ListView VievStyle's in a Radio Button List
   vs : TViewStyle;
   vss : string;
   for vs in [Low(TViewStyle) .. High(TViewStyle)] do
     //get enum name as string
     vss := GetEnumName(TypeInfo(TViewStyle), integer(vs)) ;
     //add enum to a radio button list
     RadioGroup.Items.AddObject(vss, TObject(vs)) ;
You first iterate over the set of possible enum values for ViewStyle. Next, Delphi's RTTI function GetEnumName is used to convert an enum to its string representation.

Finally, the enum as string is added to the radio group's Items collection, by using the AddObject method which adds a string to the list, and associates an object with the string.

The object associated is the actual enum value.

When any of the radio group option buttons is selected / clicked we need to handle the RadioButton.OnClick event.

 procedure TForm.RadioGroupClick(Sender: TObject) ;
   vs : TViewStyle;
   //get the enum value
   vs := TViewStyle(RadioGroup.Items.Objects[RadioGroup.ItemIndex]) ;
   //change the enumerated property of a control
   ListView1.ViewStyle := vs;
We extract the enum value from the selected option button by type casting the object to the actual enum value.

Finally, the ListView's ViewStyle is set to the new value.

That's it. As simple as it should be with Delphi :)

Delphi tips navigator:
» Force TListView's Edit Mode using a Keyboard Shortcut
« Disable CTRL+V Shortcut Key for a TEdit / TMemo (TCustomEdit)

Suggested Reading

©2014 About.com. All rights reserved.