Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delphi pick a random value from an enum

Tags:

delphi

I need a delphi solution for this solution in java JAVA CODE

type
 TColors = (red, green, blue, white, purple, orange, yellow, black);
type
 TForm1 = class(TForm)
 Button1: TButton;
 Memo1: TMemo;
 procedure Button1Click(Sender: TObject);
 private
 { Private-Deklarationen }
 public
 { Public-Deklarationen }
 end;

 var Form1: TForm1;

 implementation

 {$R *.fmx}

 function RandomColor: TColors;
 begin
  result := blue;  //   make this random value from enum ????
end;

procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
begin
  s := GetEnumName(TypeInfo(TColors), integer(RandomColor));
 Memo1.Lines.Add(s);   ///  print random color to memo 
end;
like image 898
Franz Avatar asked Jul 29 '26 13:07

Franz


1 Answers

function RandomColor: TColors;
begin
  Result := TColors(Random(Succ(Ord(High(TColors)))));
end;

var
  MyColor: TColors;
begin
  Randomize; //call this once at startup
  MyColor := RandomColor;
like image 127
Chris Rolliston Avatar answered Aug 01 '26 12:08

Chris Rolliston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!