Recent

Author Topic: [SOLVED] Any way to Exit a control when form is clicked?  (Read 1015 times)

Sieben

  • Full Member
  • ***
  • Posts: 148
Re: Any way to Exit a control when form is clicked?
« Reply #15 on: October 09, 2020, 07:01:02 pm »
But again - why? OnExit is triggered when the control is left for whatever reason, and for closing form there is this wonderful event OnCloseQuery where you can even stop the whole thing if something goes wrong, ask the user if she/he really means to leave, if he/she wants to save or not and so on... why not do it the way lucamar suggested...?
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32 / GTK2

marcio2003

  • Jr. Member
  • **
  • Posts: 50
Re: Any way to Exit a control when form is clicked?
« Reply #16 on: October 09, 2020, 08:01:23 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. begin
  4.   Form1.FocusControl( nil );
  5. end;

Windows 10 64bit - Lazarus v2.0.6
Lazarus 2.0.10 Windows 10 64bits

egsuh

  • Hero Member
  • *****
  • Posts: 506
Re: Any way to Exit a control when form is clicked?
« Reply #17 on: October 10, 2020, 12:45:09 am »
I'm not sure addressing this issue again is valuable enough, but I'd like to share my idea and experiements with others.

First of all, I appreciate all the comments and commenters.

My baseline purpose is the content in the controls are saved to a field in object, like

Code: Pascal  [Select][+][-]
  1. type
  2.      TMyObject = class
  3.      published
  4.           Name,
  5.           Address : string;
  6.           OtherInfo: TStringList;
  7.  
  8.           // methods
  9.      end;
  10.  
  11.      TMyFrame = class (TFrame)
  12.            edit1 : TTIEdit;   // name
  13.            edit2 : TTIEdit;   // address
  14.            OtherInfoEdit : TValueListEditor;
  15.  
  16.            // methods
  17.       private
  18.            FMyObject : TMyObject;
  19.            procedure setMyObject(AValue: TMyObject);
  20.       public
  21.             property MyObject : TMyObject read FMyObject write setMyObject;
  22.    
  23.       end;
  24.  
  25. implementation
  26.  
  27. procedure TMyFrame.setMyObject(AValue: TMyObject);
  28. begin
  29.     if AValue <> nil then begin  
  30.         FMyObject := AValue;
  31.         OtherInfoEdit.Strings.Text := FMyObject.OtherInfo.Text;    // initialize the content of valuelist editor
  32.     end;
  33. end;  
  34.  
  35.  

I do not assign any saving procedure myself for RTTI controls. But I have to write them for general controls, TValueListEditor here. So,

Code: Pascal  [Select][+][-]
  1. procedure TMyFrame.OtherInfoEditEditingDone(sender: TObject);   // or
  2. procedure TMyFrame.OtherInfoExit(sender: TObject);   // either one is the same
  3. begin
  4.      MyObject.OtherInfo.Text := OtherInfoEdit.Strings.Text;
  5. end;
  6.  
     
             
And I want the (changed) contents in the controls are automatically saved in the MyObject variable. The contents are saved when focus moves from one control to another, but not (neither RTTI controls nor general controls, neither OnEditingDone nor OnExit) when I leave the form like by running other Windows application, by closing the form pressing Alt-F4 or small x, or on other cases I do not remember exactly. So I was looking for ways that I can save my changes reliably, i.e. will call OnExit or OnEditingDone reliably. 

I thought over solutions suggested here in comments. But I think it better not to introduce any futher things like TWinControl or TNotifyEvent or other event handlers or new variable like ParentForm.

And I found putting ActiveContol to nil in the form.

Code: Pascal  [Select][+][-]
  1. procedure TMyForm.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. begin
  3.      // It is possible to ask whether to save the changes here, but decided to put it later setp, between Object and permanent storage (e.g. database)
  4.      Self.ActiveControl := nil;
  5. end;
  6.  

This will call OnExit or OnEditingDone procedures defined only in the frames. 

Hope that I did not mislead you by the title ".. when form is clicked?".  At first, I tried to save the content by clicking on (title bar or vacant part of) the form but I found that there are other situations like pressing Alt-F4.

Thank all of you again for your kind advices.
« Last Edit: October 10, 2020, 03:51:24 am by egsuh »

 

TinyPortal © 2005-2018