Recent

Author Topic: fill Combobox using Mediators (TiOPF)  (Read 148 times)

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 406
  • Weitental is a very beautiful garbage depot.
fill Combobox using Mediators (TiOPF)
« on: July 27, 2022, 10:33:24 am »
Hello,

i was trying to populate my Combobox with Items from a StringList, but could not get it done.
Do i have to write an object "TMyTESTListItems" (bc the List has to store TtiObjects, and not just Strings) which inherits of TTiObject, so the TMyTESTList Class can use it,
or is there a way to use Strings or even more simple just tell the Mediator to use a StringList ?

Here are my Classes:
Code: Pascal  [Select][+][-]
  1.   { TMyTESTList }
  2.  
  3.   TMyTESTList = class(TtiObjectList)
  4.   protected
  5.     function    GetItems(i: integer): String; reintroduce;
  6.     procedure   SetItems(i: integer; const Value: String); reintroduce;
  7.   public
  8.     property    Items[i: integer]: String read GetItems write SetItems;
  9.     procedure   Add(const stritem: String); reintroduce;
  10.   end;
  11.  
  12.   { TMyTestingClass }
  13.   TMyTestingClass = class(TtiObject)
  14.   private
  15.     FEdit: Double;
  16.     FRadioChoice: Boolean;
  17.     FComboTEXT: String;
  18.     FCItems: TMyTESTList;
  19.     procedure SetComboTEXT(AValue: String);
  20.     procedure SetEdit(AValue: Double);
  21.     procedure SetRadioChoice(AValue: Boolean);
  22.   public
  23.     constructor create; override;
  24.     procedure   NotifyObservers; override;
  25.     property CItems: TMyTESTList read FCItems write FCItems;
  26.   published
  27.     property Edit: Double read FEdit write SetEdit;
  28.     property RadioChoice: Boolean read FRadioChoice write SetRadioChoice;
  29.     property ComboTEXT: String read FComboTEXT write SetComboTEXT;
  30.   end;
  31.  

Here is the implementation of those classes
the Syntax here is not correct, because i get Strings and not TtiObjects
Code: Pascal  [Select][+][-]
  1.  
  2. { TMyTESTList }
  3.  
  4. function TMyTESTList.GetItems(i: integer): String;
  5. begin
  6.   result := inherited GetItems(i);
  7. end;
  8.  
  9. procedure TMyTESTList.SetItems(i: integer; const Value: String);
  10. begin
  11.   inherited SetItems(i, Value);
  12. end;
  13.  
  14. procedure TMyTESTList.Add(const stritem: String);
  15. begin
  16.   inherited Add(stritem);
  17.   NotifyObservers;
  18. end;
  19.  
  20. { TMyTestingClass }
  21.  
  22. procedure TMyTestingClass.SetComboTEXT(AValue: String);
  23. begin
  24.    if FComboTEXT=AValue then Exit;
  25.    FComboTEXT:=AValue;
  26.  
  27.    NotifyObservers;
  28. end;
  29.  
  30. procedure TMyTestingClass.SetEdit(AValue: Double);
  31. begin
  32.    if FEdit=AValue then Exit;
  33.    FEdit:=AValue;
  34.  
  35.    NotifyObservers;
  36. end;
  37.  
  38. procedure TMyTestingClass.SetRadioChoice(AValue: Boolean);
  39. begin
  40.    if FRadioChoice=AValue then Exit;
  41.    FRadioChoice:=AValue;
  42.  
  43.    NotifyObservers;
  44. end;
  45.  
  46. constructor TMyTestingClass.create;
  47. begin
  48.    inherited create;
  49.    FEdit:= 10;
  50.    FRadioChoice:= True;
  51.    FComboTEXT:= 'First';
  52.    FCItems:= TMyTESTList.Create;
  53.    FCItems.Add('First');
  54.    FCItems.Add('Second')
  55. end;
  56.  
  57. procedure TMyTestingClass.NotifyObservers;
  58. begin
  59.    inherited NotifyObservers;
  60. end;
  61.  

Here is the View (Just the View of the Demo a bit changed):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   FMyTest:= TMyTestingClass.create;
  4.  
  5.   //ComboBox1.Items.Add('First');
  6.   //ComboBox1.Items.Add('Second');
  7. end;
  8.  
  9. procedure TForm1.Button1Click(Sender: TObject);
  10. begin
  11.   //Testing
  12.    FMyTest.Edit:= FMyTest.Edit + 1;
  13.    FMyTest.RadioChoice:= not FMyTest.RadioChoice;
  14.    if FMyTest.ComboTEXT = 'First' then begin
  15.      FMyTest.ComboTEXT:= 'Second';
  16.    end else begin
  17.      FMyTest.ComboTEXT:= 'First';
  18.    end;
  19. end;
  20.  
  21. procedure TForm1.FormShow(Sender: TObject);
  22. begin
  23.   SetupMediators;
  24. end;
  25.  
  26. procedure TForm1.SetupMediators;
  27. begin
  28.    if not Assigned(FMediatorTest) then begin
  29.     FMediatorTest := TtiModelMediator.Create(Self);
  30.     FMediatorTest.Name:= 'MyTestMediator';
  31.     FMediatorTest.AddProperty('Edit', Edit1);
  32.     FMediatorTest.AddProperty('RadioChoice', RadioButton1);
  33.     FMediatorTest.AddProperty('ComboTEXT', ComboBox1).ValueList:= FMyTest.CItems;
  34.   end;
  35.   FMediatorTest.Subject := FMyTest;
  36.   FMediatorTest.Active:= true;
  37. end;
  38.  
  39. initialization
  40.   RegisterFallbackListMediators;
  41.   RegisterFallBackMediators;
  42.  

Result:
No Error, but the Combobox List is empty. The Text is set.

My goal is to implement a Dynamic Combobox two way Binding to my Model.

thanks in advance for  any help.
« Last Edit: July 27, 2022, 10:40:38 am by Weitentaaal »
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.0

 

TinyPortal © 2005-2018