Solved

Convert enumerated type to string generically

Posted on 2003-11-03
6
328 Views
Last Modified: 2010-04-05
I want to be able to generically set Combobox.Items.CommaText to an enumarted type. So for example  

type
    MyEnum = (One, Two, Three)

I want to achieve

Combobox.Items.CommaText := 'One, Two, Three';

by doing

Combobox.Items.CommaText := ConvertCommaString(MyEnum );

I know I can use the TypInfo routines

function GetEnumName(TypeInfo: PTypeInfo; Value: Integer): string;
function GetEnumValue(TypeInfo: PTypeInfo; const Name: string): Integer;

  Name := GetEnumName(TypeInfo(MyEnum), Integer(Three));
  Value := MyEnum(GetEnumValue(Typeinfo(MyEnum), Name));

But I want to have a gereric solutionfor converting any enumerated type. Is this possible?

Thanks, Tom.
0
Comment
Question by:tomcorcoran
  • 4
  • 2
6 Comments
 
LVL 5

Expert Comment

by:snehanshu
ID: 9676181
I guess you are looking for this:

unit EnumUnit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, TypInfo;

type
    MyEnum = (One, Two, Three);

  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    ComboBox: TComboBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
procedure GetValues(EnumType:PTypeInfo);
var
  I:Integer;
  MyStr: String;
begin
  MyStr := '';
  with GetTypeData(EnumType)^ do
  begin
    for I:=MinValue to MaxValue do
    begin
      MyStr := MyStr + GetEnumName(EnumType,i);
      if i < MaxValue then
        MyStr := MyStr + ',';
    end;
  end;
  ComboBox.Items.CommaText := MyStr;
end;
begin
  GetValues(TypeInfo(MyEnum));
end;

end.
Cheers!
...Snehanshu
0
 
LVL 5

Accepted Solution

by:
snehanshu earned 150 total points
ID: 9676196
Well, a slight modification:

procedure TForm1.Button1Click(Sender: TObject);
Function GetValues(EnumType:PTypeInfo): String;
var
  I:Integer;
  MyStr: String;
begin
  MyStr := '';
  with GetTypeData(EnumType)^ do
  begin
    for I:=MinValue to MaxValue do
    begin
      MyStr := MyStr + GetEnumName(EnumType,i);
      if i < MaxValue then
        MyStr := MyStr + ',';
    end;
  end;
//  ComboBox.Items.CommaText := MyStr;
  Result := MyStr;
end;
begin
  ComboBox.Items.CommaText := GetValues(TypeInfo(MyEnum));
end;


:-)
...Shu
0
 

Author Comment

by:tomcorcoran
ID: 9677143
Superb!!!! Thank you.

That looks like exactly what I am looking for.

I'll test that later.

Some of my enumerated types have commas in them. I'll work on modifying the above. Presumably a ',' needs to wrappedin quotes (').

Cheers, Tom.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Expert Comment

by:snehanshu
ID: 9677282
I am glad I could help.
The correction:
MyStr := MyStr + '""GetEnumName(EnumType,i)+'"';

Cheers!
...Shu
0
 

Author Comment

by:tomcorcoran
ID: 9691355
Worked a dream! Only think remaining for meto fihure out now is how to include commas in the string which I assign to commatetx.

Cheers, Tom.
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9691443
Like you already said, wrap it with " to include , in a commatext.
Like
MyStr := MyStr + '"' + GetEnumName(EnumType,i)+ '"';
I don't know how you got an enum name to have comma!
Cheers,
...Shu
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now