Mega Code Archive

 
Categories / Delphi / Ide Indy
 

Getting properties of components to your GUI and back

Title: Getting properties of components to your GUI and back. Question: Getting your components to map your GUI so you don't have to type Myobject.SomeString = Edit1.text; Answer: Dont u just hate it when u need to make a component Logic (so u can use it annywhere in COM, XML, DCOM, StandAlone-Apps and Internet (ISAPI/NSAPI) applications In a StandAlone-App u wil have to map the data on to the gui and get it back (if editeble) without mapping everyting in sutch a olf fasion like this Someobject.Somestring = Edit1.text ; Someobject.Some1000stStringProperty = UntracebleEdit.text ; just build all the logic in a class and wrap that with the aproperiat GUI and take my routines here to do your dirty work. in this example i added 1 RadioGroep with some items in its Items property And its component name is exactly the same as the aExampleEnum property in the object. i added a TCombobox and added some items to and set its Style property to csDropDownList and its name to AInteger I added aTDateTimePicker and named it ADate and a Edit control with the name AString and a checkbox named Abool. Now on two buttons i programd the call to the mapping recursion note than u can use subobjects and call the Controls like this SubobjectPropertyName_SomeProp unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls; type TExampleEnum = (Enum1,Enum2,Enum3,Enum4,Enum5,Enum6,Enum7,Enum8,Enum9,Enum10); TExampleComponent = Class(TComponent) private FAString: String; FAFloat: Double; FAInteger: Integer; FaExampleEnum: TExampleEnum; FaBool: Boolean; FADate: tDatetime; published // Anyting you want streamed and is streameble by Delphi you can publish // as property Property AString : String read FAString write FAString; Property AInteger : Integer read FAInteger write FAInteger; Property AFloat : Double read FAFloat write FAFloat; Property aExampleEnum : TExampleEnum read FaExampleEnum write FaExampleEnum; Property aBool : Boolean read FaBool write FaBool; Property ADate : tDatetime read FADate write FADate; end; TForm1 = class(TForm) ObjectToGui: TButton; GuiToObject: TButton; aExampleEnum: TRadioGroup; AInteger: TComboBox; aBool: TCheckBox; AFloat: TEdit; ADate: TDateTimePicker; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure ObjectToGuiClick(Sender: TObject); procedure GuiToObjectClick(Sender: TObject); private FExampleComponent: TExampleComponent; procedure ControlsToPersist(APersistent: TPersistent; AControl: TControl; Parentname: String); procedure GetComponent(AComponent: TComponent;var aValue: Variant); procedure PresistentToControls(APersistent: TPersistent; AControl: TControl; ParentName: String); procedure SetComponent(AComponent: TComponent; aValue: Variant); { Private declarations } public Property ExampleComponent : TExampleComponent read FExampleComponent write FExampleComponent; { Public declarations } end; var Form1: TForm1; implementation uses typInfo ; {$R *.DFM} { TForm1 } procedure TForm1.PResistentToControls(APersistent: TPersistent; AControl: TControl; ParentName : String ); var PropList: PPropList; PropCount : Integer ; ClassTypeInfo: PTypeInfo; ClassTypeData: PTypeData; i : integer; Propname : String; aValue : Variant ; AComponent : TComponent ; begin ClassTypeInfo := APersistent.ClassInfo ; ClassTypeData := GetTypeData(ClassTypeInfo); PropCount := ClassTypeData.PropCount -1 ; // reserveer geheugen GetMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount); // Error trap try // Vul de prop list GetPropList(APersistent.ClassInfo,tkAny,PropList); for i := 0 to PropCount do begin try Propname := Parentname + '_' + PropList[i]^.Name ; If Propname[1] = '_'then Propname[1]:= ' ' ; Propname := trim(Propname); AComponent := AControl.FindComponent(Propname); if AComponent nil then begin Case PropList[i]^.PropType^.Kind of tkString,tkLString, tkWString,tkWChar, tkChar : begin if (assigned(PropList[i]^.getProc)) then aValue := GetStrProp(APersistent,PropList[i]) ; end ; tkInteger, tkEnumeration: begin aValue := GetOrdProp(APersistent,PropList[i]); end; tkFloat : begin if (assigned(PropList[i]^.getProc)) then if (PropList[i]^.PropType^.Name = 'TDateTime') then begin aValue := DateTimeToStr(GetFloatProp(APersistent,PropList[i])); end else begin aValue := GetFloatProp(APersistent,PropList[i]) ; end; end; end ;// end case SetComponent(AComponent,aValue); end; if PropList[i]^.PropType^.Kind = tkClass then begin If GetObjectProp(APersistent,PropList[i]) is TPersistent then begin PResistentToControls(TPersistent(GetObjectProp(APersistent,PropList[i])),AControl,Propname); end; end; Except On e : Exception do begin // Handel error here Showmessage(e.Message); end; end; end; // for i := 0 to PropCount do finally FreeMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount); end; end; procedure TForm1.SetComponent(AComponent: TComponent; aValue : Variant); begin If AComponent is TCustomEdit then begin TCustomEdit(AComponent).text := VarToStr(aValue); // Onchange is a protected property of TCustomEdit so every one got it :) If assigned(TEdit(AComponent).onchange) then TEdit(AComponent).onchange(Self); end else If AComponent is TRadioGroup then begin TRadioGroup(AComponent).ItemIndex := StrTOInt(VarToStr(aValue)) ; end else If AComponent is TComboBox then begin If TComboBox(AComponent).Style = csDropDownList then begin If VarToStr(aValue) '' then TComboBox(AComponent).ItemIndex := aValue ; end else TComboBox(AComponent).text := VarToStr(aValue) ; end else IF AComponent is TCheckBox then begin if VarToStr(aValue) = '1' then TCheckBox(AComponent).Checked := True else TCheckBox(AComponent).Checked := False ; IF assigned(TCheckBox(AComponent).OnClick) then TCheckBox(AComponent).OnClick(self); end else If AComponent is TDateTimePicker then begin If VarToStr(aValue) '' then TDateTimePicker(AComponent).DateTime := VarToDateTime(aValue) ; end end; procedure TForm1.ControlsToPersist(APersistent: TPersistent; AControl: TControl ;Parentname : String ); var PropList: PPropList; PropCount : Integer ; ClassTypeInfo: PTypeInfo; ClassTypeData: PTypeData; i : integer; TempString : String ; Propname : String; Value : Variant ; AComponent : TComponent ; begin ClassTypeInfo := APersistent.ClassInfo ; ClassTypeData := GetTypeData(ClassTypeInfo); PropCount := ClassTypeData.PropCount -1 ; // Get mem for the list GetMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount); // Error trap try // Fill prop list GetPropList(APersistent.ClassInfo,tkAny,PropList); // For every property do for i := 0 to PropCount do begin try Propname := Parentname + '_' + PropList[i]^.Name ; If Propname[1] = '_'then Propname[1]:= ' ' ; Propname := trim(Propname); AComponent := AControl.FindComponent(Propname); IF AComponent nil then begin GetComponent(AComponent,Value); Case PropList[i]^.PropType^.Kind of tkString,tkLString, tkWString,tkWChar, tkChar : begin if (assigned(PropList[i]^.SetProc)) then begin SetStrProp(APersistent,PropList[i], VarToStr(Value)); end; end ; tkInteger, tkEnumeration: begin Tempstring := VarToStr(Value) ; If Tempstring '' then begin if (assigned(PropList[i]^.SetProc)) then SetOrdProp(APersistent,PropList[i],StrToInt(Tempstring)); end; end; tkFloat : begin IF VarToStr(Value) '' then if (assigned(PropList[i]^.SetProc)) then if (PropList[i]^.PropType^.Name = 'TDateTime') then begin SetFloatProp(APersistent,PropList[i],VarToDateTime(Value)); end else begin SetFloatProp(APersistent,PropList[i],StrTOFLoat(VarToStr(Value))); end; end; end ;// end case end; // Alway do subojects even when there readonley. if PropList[i]^.PropType^.Kind = tkClass then begin If GetObjectProp(APersistent,PropList[i]) is TPersistent then begin ControlsToPersist(TPersistent(GetObjectProp(APersistent,PropList[i])),AControl,Propname); end; end; Except On E: Exception do begin // Handel Errors here Showmessage(e.Message); end; end ; end; // end for i := 0 to PropCount finally FreeMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount); end; end; procedure TForm1.GetComponent(AComponent: TComponent;var aValue : Variant ); begin If AComponent is TCustomEdit then begin aValue := TCustomEdit(AComponent).text ; end else If AComponent is TRadioGroup then begin aValue := TRadioGroup(AComponent).ItemIndex ; end else If AComponent is TComboBox then begin If TComboBox(AComponent).Style = csDropDownList then aValue := TComboBox(AComponent).ItemIndex else aValue := TComboBox(AComponent).text ; end else IF AComponent is TCheckBox then begin if TCheckBox(AComponent).Checked then aValue := 1 else aValue := 0 ; end else IF AComponent is TDateTimePicker then begin aValue := VarFromDateTime(TDateTimePicker(AComponent).DateTime) ; end ; end; procedure TForm1.FormCreate(Sender: TObject); begin FExampleComponent := TExampleComponent.Create(Self); end; procedure TForm1.FormDestroy(Sender: TObject); begin FExampleComponent.Free ; end; procedure TForm1.ObjectToGuiClick(Sender: TObject); begin PresistentToControls(FExampleComponent,Self,''); end; procedure TForm1.GuiToObjectClick(Sender: TObject); begin ControlsToPersist(FExampleComponent,Self,''); end; end.