Solved

Convert enumerated type to string generically

Posted on 2003-11-03
6
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses
Course of the Month7 days, 5 hours left to enroll

623 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