Mega Code Archive

 
Categories / Delphi / Ide Indy
 

Cycle Enum Values in Delphi Round Robin Enumeration

Title: Cycle Enum Values in Delphi - Round Robin Enumeration An enumerated type in Delphi, or enum lets you define a list of values. The values have no inherent meaning, and their ordinality follows the sequence in which the identifiers are listed. For example, the Align property most Delphi controls expose is of type TAlign which is an enum type defined as TAlign = (alNone, alTop, alBottom, alLeft, alRight, alClient, alCustom) Another example is the ViewStyle property of a TListView. 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. The TViewStyle enum is defined as: TViewStyle = (vsIcon, vsSmallIcon, vsList, vsReport) Round Robin Cycling Through Enum Values 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. You can choose to display enumerated properties in a selectable list. If you want to cycle through the possible set of values defined by an enum, you can implement a so called "round robin" algorithm. From the user interface point of view, you could have a button that when clicked changes, by "increasing" the current value, the current ViewStyle of a list view. Here's how to cycle through enumeration values for any enum: //with ListView1 do ViewStyle := TViewStyle((1 + Ord(ViewStyle)) mod (1 + Ord(High(TViewStyle)))) ; The above code cycles through the TViewStyle enumeration by assigning an increased value to the ViewStyle property. The above cycling can be used for any enumeration type - just make sure what's a property and what's a type declaration, in this code snippet :)