Recent

Author Topic: A little funny test with RTTI controls  (Read 700 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1614
A little funny test with RTTI controls
« on: May 08, 2024, 05:36:49 am »
I did some experiments with RTTI controls, and found out the following.

My intention is to define all possible values in "AliasValues", and then limit the available choices via "Items" definition, as shown below.

This WORKs with TIComboBox, but NOT with TICheckGroup.


Code: Pascal  [Select][+][-]
  1. unit MainForm;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  9.   RTTICtrls;
  10.  
  11. type
  12.   TColor = (red, blue, green, c4, c5, c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,
  13.             c17,c18,c19,c20,c21,c22,c23,c24,c25,c26,c27,c28,c29,c30,
  14.             c31,c32);
  15.   TColors = set of TColor;
  16.  
  17.   { TfrmMain }
  18.  
  19.   TfrmMain = class(TForm)
  20.      Button1: TButton;
  21.     TICheckGroup1: TTICheckGroup;
  22.     TIComboBox1: TTIComboBox;
  23.     procedure Button1Click(Sender: TObject);
  24.     procedure FormCreate(Sender: TObject);
  25.  
  26.   private
  27.     FColor: TColor;
  28.     IColors: integer;
  29.     function getColors: TColors;
  30.     procedure setColors(AValue: TColors);
  31.  
  32.   published
  33.      property Colors : TColors read getColors write setColors;
  34.      property Color: TColor read FColor write FColor;
  35.   public
  36.  
  37.   end;
  38.  
  39. var
  40.   frmMain: TfrmMain;
  41.  
  42. implementation
  43.  
  44. {$R *.lfm}
  45.  
  46. function TfrmMain.getColors: TColors;
  47. begin
  48.    Result := TColors(IColors);
  49. end;
  50.  
  51. procedure TfrmMain.setColors(AValue: TColors);
  52. begin
  53.    IColors := Integer(AValue);
  54. end;
  55.  
  56.  
  57. procedure TfrmMain.FormCreate(Sender: TObject);
  58. var
  59.   acolor: TColor;
  60.   ts: string;
  61. begin
  62.    TIComboBox1.Link.TIObject:= Self;
  63.    TIComboBox1.Link.TIPropertyName:= 'Color';
  64.  
  65.    TICheckGroup1.Link.TIObject:= Self;
  66.    TICheckGroup1.Link.TIPropertyName:= 'Colors';
  67.  
  68.    // with TiCheckGroup1 do begin
  69.    with TiComboBox1 do begin
  70.       with Items do begin
  71.          Clear;
  72.          Add('Red');
  73.          Add('Blue');
  74.          Add('C6');
  75.          Add('C22');
  76.       end;
  77.  
  78.       with Link.AliasValues do begin
  79.          Clear;
  80.          for aColor in TColor do begin
  81.              writestr(ts, aColor);
  82.              Add(Format('%s=%s', [ts, uppercase(ts)]));
  83.          end;
  84.       end;
  85.    end;
  86.  
  87. end;
  88.  
  89. procedure TfrmMain.Button1Click(Sender: TObject);
  90. var
  91.    ts: string;
  92. begin
  93.    // ShowMessageFmt('FColor:%d, IColor:%d', [SizeOf(FColors), SizeOf(IColors)]);
  94.    // WriteStr(ts, Color);
  95.    // ShowMessage(ts);
  96.    // ShowMessage(InttoStr(IColors));
  97.  
  98.    ShowMessage(BoolToStr(Red in Colors, true));
  99. end;
  100.  
  101. end.

egsuh

  • Hero Member
  • *****
  • Posts: 1614
Re: A little funny test with RTTI controls
« Reply #1 on: May 08, 2024, 06:16:02 am »
I found limiting the displayed items is possible with TIRadioGroup, as well as TIComboBox.

 

TinyPortal © 2005-2018