Pass value from Component FormEditor to the main application lazarus / 31-LCL ---------------------------------------------------------------------- JZS · 2014-12-09 13:30 Continue on this: http://forum.lazarus.freepascal.org/index.php/topic,26707.0.html How can I store a value, simple text, in a component when dropped at design time so I can access this property at run-time, as well as when closing and reopening the application, it should always be there, unless cleared or changed at design time. To make it a bit complicated, that simple value to be shown in a FormEditor same like the TMemo when clicking on the property "Lines [...]". What type of property that I need to introduce to store string in it, so when reopened my project the value sticks (just like when adding text to memo and close, the text remains until you clear or change it). What am thinking of is to have a real Form that has memo on it and linked/used by the package. But not really sure what is the best way to achieve that. Any advice please. ---------------------------------------------------------------------- engkin · 2014-12-09 15:35 It seems to me that TMyPropEdit has nothing to do with fFrmEdtr according to this line: Code: [Select]  fFrmEdtr:= TListEditor.Create(nil); Maybe if make TMyPropEdit the owner of fFrmEdtr, then it will be saved. Code: [Select]  fFrmEdtr:= TListEditor.Create(Self); ---------------------------------------------------------------------- howardpc · 2014-12-09 16:56 I'm not sure I understand exactly what you are after. If it is a simple text component that can be edited at run time, the following bare-bones example will give you something to work from. All components have persistence built-in for published properties, so that comes 'free'. For simplicity I've made it editable at run time by double-clicking, but it would not be much harder to add an ellipsis button that opened a modal edit dialog; though to me that seems overkill for a single line of text. Code: [Select]unit uEditAtRuntime; {$mode objfpc}{$H+} interface uses   Classes, StdCtrls, Controls, types, LCLType; type   TEditAtRuntime = class;   { TRuntimeEditor }   TRuntimeEditor = class(TCustomEdit)   strict private     procedure EditorKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);   protected     procedure DoExit; override;   public     constructor Create(AOwner: TComponent); override;   end;   { TEditAtRuntime }   TEditAtRuntime = class(TCustomControl)   const     TextOffsetX = 8;     TextOffsetY = 3;   private     FEditor: TRuntimeEditor;     procedure ShowEditor;     procedure HideEditor;   protected     class function GetControlClassDefaultSize: TSize; override;     procedure DblClick; override;     procedure SetParent(NewParent: TWinControl); override;     procedure Paint; override;   public     constructor Create(AOwner: TComponent); override;   published     property Text;   end;   procedure Register; implementation procedure Register; begin   RegisterComponents('Misc', [TEditAtRuntime]); end; { TRuntimeEditor } procedure TRuntimeEditor.DoExit; begin   TEditAtRuntime(Owner).HideEditor;   inherited DoExit; end; procedure TRuntimeEditor.EditorKeyDown(Sender: TObject; var Key: Word;   Shift: TShiftState); begin   if Visible then   case Key of     VK_ESCAPE: begin       Key:=0;       Text:='';       Visible:=False;       Parent.SetFocus;     end;     VK_RETURN: begin       Key:=0;       Visible:=False;       Parent.SetFocus;     end;   end; end; constructor TRuntimeEditor.Create(AOwner: TComponent); begin   inherited Create(AOwner);   OnKeyDown:=@EditorKeyDown; end; { TEditAtRuntime } constructor TEditAtRuntime.Create(AOwner: TComponent); begin   inherited Create(AOwner);   FEditor:=TRuntimeEditor.Create(Self);   FEditor.Visible:=False;   with GetControlClassDefaultSize do     SetInitialBounds(0, 0, cx, cy);   BorderStyle:=bsSingle;   AutoSize:=True; end; procedure TEditAtRuntime.DblClick; begin   inherited DblClick;   ShowEditor; end; class function TEditAtRuntime.GetControlClassDefaultSize: TSize; begin   Result:=inherited GetControlClassDefaultSize;   Result.cy:=25; end; procedure TEditAtRuntime.HideEditor; begin   if (FEditor.Text <> '') then     Text:=FEditor.Text;    FEditor.Visible:=False; end; procedure TEditAtRuntime.Paint; begin   Canvas.Brush.Color:=Color;   Canvas.FillRect(ClientRect);   Canvas.TextOut(TextOffsetX-2, TextOffsetY-2, Text);   inherited Paint; end; procedure TEditAtRuntime.SetParent(NewParent: TWinControl); var   sz: TSize; begin   inherited SetParent(NewParent);   if (NewParent <> nil) then   begin     sz:=Canvas.TextExtent(Text);     SetBounds(Left, Top, sz.cx + 2*TextOffsetX, sz.cy + 2*TextOffsetY);     FEditor.Parent:=NewParent;   end; end; procedure TEditAtRuntime.ShowEditor; begin   FEditor.Text:=Text;   FEditor.Visible:=True;   FEditor.SetBounds(Left+TextOffsetY, Top+TextOffsetY, Width, Height + TextOffsetY);   FEditor.SetFocus; end; end. ---------------------------------------------------------------------- JZS · 2014-12-09 18:48 Quote from: engkin on December 09, 2014, 04:35:54 pmIt seems to me that TMyPropEdit has nothing to do with fFrmEdtr according to this line: Code: [Select]  fFrmEdtr:= TListEditor.Create(nil); Maybe if make TMyPropEdit the owner of fFrmEdtr, then it will be saved. Code: [Select]  fFrmEdtr:= TListEditor.Create(Self); It does not save. Sure it's my design that is faulty. Quote from: howardpc on December 09, 2014, 05:56:32 pmI'm not sure I understand exactly what you are after. Thanks Howardpc for the sample. It is better if I describe what I want exactly and clearly. Am creating a component that does nothing but saving text in a memo inside the component. I want that component to have a property on the OI that shows [...] when clicked it shows a form that has memo and button. At design time, when text is added to the memo and the button is clicked then this text will be saved whenever the project is recalled. Similar to when dealing with normal TMemo on a normal form. Whenever text is added and the project is saved you get the text in the memo every time you open the project. The text that resides in the memo inside my component shall be available whenever needed in the host applicatio. So when the component gets installed and dropped along with a button (Btn1) on a form and using OI to edit my component's memo, by clicking "Edit Property [...]" of my component, then I set the memo text to "Am text in the memo" at design time, and at the dropped button (Btn1 OnClick) I call "ShowMessage(MyComponent.Memo1.Text);", for instance, I should get "Am text in the memo". Also when adding another button (Btn2) to call "MyComponent.Memo1.Clear" the text should be cleared at run time only so when checking the memo text (Btn1 OnClick) it shall be empty, but when revert to the project design the memo text should be untouched. Hope it's clear. ---------------------------------------------------------------------- engkin · 2014-12-09 21:23 *Warning: At the moment I can not rebuild my IDE, so I can not test my own answers.*  Quote from: JZS on December 09, 2014, 07:48:23 pmIt does not save. Sure it's my design that is faulty. I just spotted another issue. Code: [Select]    property FrmEditor: TListEditor read fFrmEdtr write fFrmEdtr; When you write to the property, it should not lose the original TListEditor you created and saved in fFrmEdtr in the constructor. So this part "... write fFrmEdtr" should be replaced with a procedure, something like "... write SetFrmEdtr". SetFrmEdtr should copy the values to your fFrmEdtr through fFrmEdtr.assign or whatever, but *not* by replacing the original fFrmEdtr. Edit: Take a look at this simple component in this post. I hope Avishai is ok. ---------------------------------------------------------------------- JZS · 2014-12-10 06:58 That is close to what I want except that, I am going to have more than one memo (in PageControl tabs). So that is the trick, how am going to store all the memos text for the main form usage? At the moment what am doing is creating published properties as String and every property stands for a single tab. But I do not like it that way, much better to have External Form showing all the Tabs and the user fills in the tabs. I created the form and added to the uses of the package form but like you suspected the issue within the published property (write fFrmEdtr). That is what I guess what I need to focus on but I do not know what to put there to keep the added text in the memos of the CustomEditor. What am going to have is something like the attached screenshot. ---------------------------------------------------------------------- howardpc · 2014-12-10 09:33 In terms of your original component request, I think the attached may be closer to what you are looking for than what I offered before. ---------------------------------------------------------------------- JZS · 2014-12-10 11:11 Quote from: howardpc on December 10, 2014, 10:33:41 amIn terms of your original component request, I think the attached may be closer to what you are looking for than what I offered before. Indeed it is. Thank you howardpc. We are almost there. Now how if I want to handle a form instead of a string? So, this line: property LinesAsString: string read GetLinesAsString write SetLinesAsString stored GetStored; becomes: property SomeForm: TSomeForm read GetForm write SetForm stored GetStored; So the question is, can we change the following code to read a Form as it does for the string now? If yes, what to use instead of "GetStrValue"? Code: [Select]procedure TRTMemoPropEditor.Edit; var   dlg: TRTPropEditDlg;   s: TSomeForm; begin   s:= ???;//GetStrValue; ---------------------------------------------------------------------- howardpc · 2014-12-10 11:20 I'm still not clear what you want. Do you want a pagecontrol component in its own form with its own built-in runtime editor, so that the user can name, rename, add or delete tabs? Or are you looking for a property editor that is only applicable at design-time by a developer? ---------------------------------------------------------------------- JZS · 2014-12-10 11:51 Here am attaching a sample of what I tried to describe earlier. Am sure I am doing something stupid somewhere or probably missing a standard concept. Keep in mind it's a new world for me. ---------------------------------------------------------------------- JZS · 2014-12-10 11:56 I already know how to have many properties on the OI that hold string. So the challenge here is to have external editor rather than using OI. Hope I made it clear. ---------------------------------------------------------------------- howardpc · 2014-12-10 21:05 Here's a TPageControl descendant and property editor that may do what you are after. As written it installs to the Common Controls palette page with a default icon, but of course you can change that (or anything else). ---------------------------------------------------------------------- JZS · 2014-12-11 09:02 Thank you howardpc. That's a nice work around, but I guess it is not fully adjustable to what I need, beside is not robust. For example, creating tabs based on the lines I add in the CommaTabName property, for me, it is not a good way to go. Also, whenever I click CommaTabName and then add text then OK, the tabs get doubled. Finally when clicking on the tabs (selecting different tab than the currently selected) generates Div by zero error. Anyway, I guess there is no elegant way to make this as simple as I though. Probably I will end up using string properties at the OI rather than External Form. But will keep on trying until I make it. Quote from: howardpc on December 10, 2014, 10:33:41 amIn terms of your original component request, I think the attached may be closer to what you are looking for than what I offered before. I like the way you created the External Form here, can I use this code freely? Does it have any license? ---------------------------------------------------------------------- howardpc · 2014-12-11 10:25 You are free to use the code as you wish. Most of it is based either directly, or by way of inspiration, on code already in the LCL. ---------------------------------------------------------------------- JZS · 2014-12-11 11:51 Many thanks howardpc. And thank you engkin. My case is solved, by work around, so I will keep the post ready for future contributors without marking it solved. Regards,