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:
{ TMyTESTList }
TMyTESTList = class(TtiObjectList)
protected
function GetItems(i: integer): String; reintroduce;
procedure SetItems(i: integer; const Value: String); reintroduce;
public
property Items[i: integer]: String read GetItems write SetItems;
procedure Add(const stritem: String); reintroduce;
end;
{ TMyTestingClass }
TMyTestingClass = class(TtiObject)
private
FEdit: Double;
FRadioChoice: Boolean;
FComboTEXT: String;
FCItems: TMyTESTList;
procedure SetComboTEXT(AValue: String);
procedure SetEdit(AValue: Double);
procedure SetRadioChoice(AValue: Boolean);
public
constructor create; override;
procedure NotifyObservers; override;
property CItems: TMyTESTList read FCItems write FCItems;
published
property Edit: Double read FEdit write SetEdit;
property RadioChoice: Boolean read FRadioChoice write SetRadioChoice;
property ComboTEXT: String read FComboTEXT write SetComboTEXT;
end;
Here is the implementation of those classes
the Syntax here is not correct, because i get Strings and not TtiObjects
{ TMyTESTList }
function TMyTESTList.GetItems(i: integer): String;
begin
result := inherited GetItems(i);
end;
procedure TMyTESTList.SetItems(i: integer; const Value: String);
begin
inherited SetItems(i, Value);
end;
procedure TMyTESTList.Add(const stritem: String);
begin
inherited Add(stritem);
NotifyObservers;
end;
{ TMyTestingClass }
procedure TMyTestingClass.SetComboTEXT(AValue: String);
begin
if FComboTEXT=AValue then Exit;
FComboTEXT:=AValue;
NotifyObservers;
end;
procedure TMyTestingClass.SetEdit(AValue: Double);
begin
if FEdit=AValue then Exit;
FEdit:=AValue;
NotifyObservers;
end;
procedure TMyTestingClass.SetRadioChoice(AValue: Boolean);
begin
if FRadioChoice=AValue then Exit;
FRadioChoice:=AValue;
NotifyObservers;
end;
constructor TMyTestingClass.create;
begin
inherited create;
FEdit:= 10;
FRadioChoice:= True;
FComboTEXT:= 'First';
FCItems:= TMyTESTList.Create;
FCItems.Add('First');
FCItems.Add('Second')
end;
procedure TMyTestingClass.NotifyObservers;
begin
inherited NotifyObservers;
end;
Here is the View (Just the View of the Demo a bit changed):
procedure TForm1.FormCreate(Sender: TObject);
begin
FMyTest:= TMyTestingClass.create;
//ComboBox1.Items.Add('First');
//ComboBox1.Items.Add('Second');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//Testing
FMyTest.Edit:= FMyTest.Edit + 1;
FMyTest.RadioChoice:= not FMyTest.RadioChoice;
if FMyTest.ComboTEXT = 'First' then begin
FMyTest.ComboTEXT:= 'Second';
end else begin
FMyTest.ComboTEXT:= 'First';
end;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
SetupMediators;
end;
procedure TForm1.SetupMediators;
begin
if not Assigned(FMediatorTest) then begin
FMediatorTest := TtiModelMediator.Create(Self);
FMediatorTest.Name:= 'MyTestMediator';
FMediatorTest.AddProperty('Edit', Edit1);
FMediatorTest.AddProperty('RadioChoice', RadioButton1);
FMediatorTest.AddProperty('ComboTEXT', ComboBox1).ValueList:= FMyTest.CItems;
end;
FMediatorTest.Subject := FMyTest;
FMediatorTest.Active:= true;
end;
initialization
RegisterFallbackListMediators;
RegisterFallBackMediators;
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.