Solved

Convert enumerated type to string generically

Posted on 2003-11-03
6
338 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing problem 2 102
Convert MS Word document to a PDF file 9 91
RESTRequest Parameter 4 43
Twebbrowser add css to the header 3 29
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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