Recent

Author Topic: (SOLVED) TEdit OnChange question  (Read 8714 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
(SOLVED) TEdit OnChange question
« on: May 26, 2017, 12:17:53 pm »
Hi guys, I have a problem. I want to close a form if some TEdit has been modified but still has the focus must be triggered on the OnChange event. In part the code is written but I do not know how to complete it. Does anyone help me? Thank you

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);
  3. var
  4.    i, iLenghtEdtBx : integer;
  5. begin
  6.  
  7.     for i := 0 to Self.ComponentCount - 1 do
  8.     begin
  9.  
  10.          if (Self.Components[i] is TEdit) then
  11.             if TEdit(Self.Components[i]).Modified then
  12.                if TEdit(Self.Components[i]).Focused then
  13.                   ?????
  14.  
  15.     end;
  16.  
  17. end;  
  18.  
  19.  
« Last Edit: May 28, 2017, 03:44:21 pm by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: TEdit OnChange question
« Reply #1 on: May 26, 2017, 12:38:58 pm »
Use: Sorry, typing error. Don't use this below, but use Bart's suggestion.
Code: Pascal  [Select][+][-]
  1. TEdit(Self.Components[i])(TEdit(Self.Components[i]));

The code above is wrong. Try this below which I modified to make it easier to read:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);
  2. var
  3.   Edit: TEdit;
  4.   i: integer;
  5. begin
  6.   for i := 0 to Self.ComponentCount - 1 do
  7.     if (Self.Components[i] is TEdit) then begin
  8.       Edit := TEdit(Self.Components[i]);
  9.       if Edit.Modified and Edit.Focused then
  10.         Edit.OnChange(Edit);
  11.     end;
  12. end;

Edit: some typing error.
« Last Edit: May 26, 2017, 12:47:01 pm by Handoko »

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: TEdit OnChange question
« Reply #2 on: May 26, 2017, 12:39:04 pm »
Untested code ...
Code: [Select]
  TEdit(Self.Components[i]).OnChange(TEdit(Self.Components[i]);

Bart

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TEdit OnChange question
« Reply #3 on: May 26, 2017, 12:45:32 pm »
I suspect you want to call the edit's OnEditingDone, not the OnChange.
OnChange will have been called already, the last time the user made any change in the edit, so it is pointless to call it again when nothing has changed.

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: TEdit OnChange question
« Reply #4 on: May 26, 2017, 12:50:26 pm »
If I change the "OnChange" to "OnEditingDone", I get External:SIGSEGV exception.

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: TEdit OnChange question
« Reply #5 on: May 26, 2017, 12:54:04 pm »
I use this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);
  2. var
  3.   Ctrl: TWinControl;
  4. begin
  5.   Ctrl := ActiveControl;
  6.   ActiveControl := nil;
  7.   ActiveControl := Ctrl;
It will trigger all the things you need.

(Saving the activecontrol and restoring it from Ctrl might not even be necessary for you)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TEdit OnChange question
« Reply #6 on: May 26, 2017, 02:38:51 pm »
If I change the "OnChange" to "OnEditingDone", I get External:SIGSEGV exception.
Well you have to assign the event and put some code there of course.
Calling a nil event ought to give you a seg fault, and sure enough it does.

sky_khan

  • Guest
Re: TEdit OnChange question
« Reply #7 on: May 26, 2017, 03:35:41 pm »
Are you trying to do something like this ?
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Edit1: TEdit;
  16.     Edit2: TEdit;
  17.     Edit3: TEdit;
  18.     procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
  19.     procedure FormCreate(Sender: TObject);
  20.     procedure FormDestroy(Sender: TObject);
  21.   private
  22.     FSnapshot : TStrings;
  23.     procedure Snapshot;
  24.     function IsChanged:boolean;
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.FormCreate(Sender: TObject);
  39. begin
  40.   Snapshot;
  41. end;
  42.  
  43. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);
  44. begin
  45.   CanClose:=IsChanged;
  46.   if not CanClose then ShowMessage('You did not enter required information');
  47. end;
  48.  
  49. procedure TForm1.FormDestroy(Sender: TObject);
  50. begin
  51.   FSnapshot.Free;
  52. end;
  53.  
  54. procedure TForm1.Snapshot;
  55. var
  56.   I: Integer;
  57. begin
  58.   if FSnapshot=nil then FSnapshot:=TStringList.Create;
  59.   for I:=0 to ControlCount-1 do
  60.     if Controls[I] is TEdit then
  61.       FSnapshot.Values[Controls[I].Name]:=(Controls[I] as TEdit).Text;
  62. end;
  63.  
  64. function TForm1.IsChanged: boolean;
  65. var
  66.   I: Integer;
  67. begin
  68.   Result:=false;
  69.   for I:=0 to ControlCount-1 do
  70.     if (Controls[I] is TEdit) and (FSnapshot.Values[Controls[I].Name]<>(Controls[I] as TEdit).Text) then
  71.         Exit(true);
  72. end;
  73.  
  74. end.
  75.  

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: TEdit OnChange question
« Reply #8 on: May 26, 2017, 05:06:21 pm »
My two cents.
Code: Pascal  [Select][+][-]
  1. var
  2.   C: TComponent;
  3. begin
  4.   for C in Self do
  5.     if C is TEdit
  6.       ..
more readable

@SkyKhan, Controls enumerate only direct controls without child elements. For example, a form with a pair of panels and a lot of Edits on each. In this case, only the panels will be enumerated.

sky_khan

  • Guest
Re: TEdit OnChange question
« Reply #9 on: May 26, 2017, 06:15:02 pm »
@ASerge
I really do not understand why my code examples often trigger unnecessary replies which is trying to be pedantic or something.
For example, my example code above has no any panel or other container components. In this case, your reminder is non-essential.
I guess I'll never understand why
« Last Edit: May 26, 2017, 06:19:06 pm by SkyKhan »

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: TEdit OnChange question
« Reply #10 on: May 27, 2017, 05:58:15 am »
I guess I'll never understand why
Your signature "...but to help beginners mostly". The beginner can ignore the top of the code with the definition of the components and just copy the text with the enumeration of the controls and get nothing. Option with components more universal and correct.

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: TEdit OnChange question
« Reply #11 on: May 28, 2017, 03:44:01 pm »
Untested code ...
Code: [Select]
  TEdit(Self.Components[i]).OnChange(TEdit(Self.Components[i]);

Bart

Thank you very much  :D
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018