Recent

Author Topic: help editchange  (Read 5237 times)

theflash09

  • New member
  • *
  • Posts: 9
help editchange
« on: January 17, 2019, 10:25:15 am »
procedure TForm1.FormCreate(Sender: TObject);
var
  num1:integer;
  num2:double;
begin
  num1:= 275;
  num2:= 3.18;
   if edit1.text = IntToStr(num1) then edit2.text = FloatToStr(num2);
end;                                           

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: help editchange
« Reply #1 on: January 17, 2019, 03:26:15 pm »
What is your question?

Please see here:
« Last Edit: January 17, 2019, 03:33:24 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: help editchange
« Reply #2 on: January 17, 2019, 03:50:09 pm »
@theflash09

Don't use FormCreate but put the code in Edit1.OnEditingDone:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1EditingDone(Sender: TObject);
  2. const
  3.   num1 = 275;
  4.   num2 = 3.18;
  5. begin
  6.   if Edit1.Text = num1.ToString then
  7.     Edit2.Text := num2.ToString;
  8. end;

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: help editchange
« Reply #3 on: January 17, 2019, 05:04:34 pm »
procedure TForm1.FormCreate(Sender: TObject);
var
  num1:integer;
  num2:double;
begin
  num1:= 275;
  num2:= 3.18;
   if edit1.text = IntToStr(num1) then edit2.text = FloatToStr(num2);
end;
Please, see attached project.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     Edit2: TEdit;
  18.     Edit3: TEdit;
  19.     Edit4: TEdit;
  20.     Label1: TLabel;
  21.     Label2: TLabel;
  22.     Label3: TLabel;
  23.     Label4: TLabel;
  24.     procedure Button1Click(Sender: TObject);
  25.     procedure FormCreate(Sender: TObject);
  26.     procedure FormShow(Sender: TObject);
  27.   private
  28.  
  29.   public
  30.  
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. { TForm1 }
  41.  
  42. procedure TForm1.FormCreate(Sender: TObject);
  43. begin
  44.   Edit1.Text     := '275';
  45.   Edit3.Text     := '3.18';
  46.   Edit1.ReadOnly := True;
  47.   Edit3.ReadOnly := True;
  48.   Edit4.ReadOnly := True;
  49. end;
  50.  
  51. procedure TForm1.FormShow(Sender: TObject);
  52. begin
  53.   Edit2.SetFocus;
  54. end;
  55.  
  56. procedure TForm1.Button1Click(Sender: TObject);
  57. begin
  58.   if Edit2.Text = Edit1.Text then
  59.     Edit4.Text := Edit3.Text
  60.   else
  61.     Edit4.Text := 'different';
  62. end;
  63.  
  64. end.

theflash09

  • New member
  • *
  • Posts: 9
Re: help editchange
« Reply #4 on: January 19, 2019, 10:37:48 am »
procedure TForm1.FormCreate(Sender: TObject);
var
  num1:integer;
  num2:double;
begin
  num1:= 275;
  num2:= 3.18;
   if edit1.text = IntToStr(num1) then edit2.text = FloatToStr(num2);
end;

Please, see attached project.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     Edit2: TEdit;
  18.     Edit3: TEdit;
  19.     Edit4: TEdit;
  20.     Label1: TLabel;
  21.     Label2: TLabel;
  22.     Label3: TLabel;
  23.     Label4: TLabel;
  24.     procedure Button1Click(Sender: TObject);
  25.     procedure FormCreate(Sender: TObject);
  26.     procedure FormShow(Sender: TObject);
  27.   private
  28.  
  29.   public
  30.  
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. { TForm1 }
  41.  
  42. procedure TForm1.FormCreate(Sender: TObject);
  43. begin
  44.   Edit1.Text     := '275';
  45.   Edit3.Text     := '3.18';
  46.   Edit1.ReadOnly := True;
  47.   Edit3.ReadOnly := True;
  48.   Edit4.ReadOnly := True;
  49. end;
  50.  
  51. procedure TForm1.FormShow(Sender: TObject);
  52. begin
  53.   Edit2.SetFocus;
  54. end;
  55.  
  56. procedure TForm1.Button1Click(Sender: TObject);
  57. begin
  58.   if Edit2.Text = Edit1.Text then
  59.     Edit4.Text := Edit3.Text
  60.   else
  61.     Edit4.Text := 'different';
  62. end;
  63.  
  64. end.
when i input 275 in edit1 , edit2  will output 3.18
« Last Edit: January 19, 2019, 10:39:20 am by theflash09 »

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: help editchange
« Reply #5 on: January 19, 2019, 11:37:00 am »
Have you tried my solution?

theflash09

  • New member
  • *
  • Posts: 9
Re: help editchange
« Reply #6 on: January 19, 2019, 12:14:45 pm »
yes i got an error

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: help editchange
« Reply #7 on: January 19, 2019, 02:19:48 pm »
What was the error?

theflash09

  • New member
  • *
  • Posts: 9
Re: help editchange
« Reply #8 on: January 20, 2019, 01:43:18 am »
What was the error?
illegal expression

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: help editchange
« Reply #9 on: January 20, 2019, 04:44:25 am »
It works without error on my test:

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.     Label1: TLabel;
  18.     procedure Edit1EditingDone(Sender: TObject);
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. { TForm1 }
  29.  
  30. procedure TForm1.Edit1EditingDone(Sender: TObject);
  31. const
  32.   num1 = 275;
  33.   num2 = 3.18;
  34. begin
  35.   if Edit1.Text = num1.ToString then
  36.     Edit2.Text := num2.ToString;
  37. end;
  38.  
  39. end.

Download the test.zip below and test it on your computer:

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: help editchange
« Reply #10 on: January 20, 2019, 04:58:26 am »
If it keeps giving you an "Illegal expression" error try changing
  const
    num1 = 275;
    num2 = 3.18;

to vars declaration:
  var
    num1: Integer = 275;
    num2: Single = 3.18;


I kind of remember having seen this when trying to use type helpers with constants.
« Last Edit: January 20, 2019, 05:00:51 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: help editchange
« Reply #11 on: January 20, 2019, 05:23:01 am »
But why did OP get error? Did he used FPC version that does not support type helpers? What possibilities can be?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: help editchange
« Reply #12 on: January 20, 2019, 06:06:56 am »
Humm ... let me check something... Well, it works here too, with Lazarus/FPC 1.8.4/3.0.4 Linux-i386-GTK2
« Last Edit: January 20, 2019, 06:19:02 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

theflash09

  • New member
  • *
  • Posts: 9
Re: help editchange
« Reply #13 on: January 20, 2019, 06:43:53 am »
Humm ... let me check something... Well, it works here too, with Lazarus/FPC 1.8.4/3.0.4 Linux-i386-GTK2
it works now thanks how about when i input 275 in edit1 , edit2 will output 3.18  and edit3 will output 2.97 and edit4 will output 6.56  is it possible

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: help editchange
« Reply #14 on: January 20, 2019, 06:52:47 am »
Yes, it can. Try this new code:

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.     Edit4: TEdit;
  19.     Label1: TLabel;
  20.     procedure Edit1EditingDone(Sender: TObject);
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.Edit1EditingDone(Sender: TObject);
  33. const
  34.   num1 = 275;
  35.   num2 = 3.18;
  36.   num3 = 2.97;
  37.   num4 = 6.56;
  38. begin
  39.   if Edit1.Text = num1.ToString then
  40.   begin
  41.     Edit2.Text := num2.ToString;
  42.     Edit3.Text := num3.ToString;
  43.     Edit4.Text := num4.ToString;
  44.   end;
  45. end;
  46.  
  47. end.

 

TinyPortal © 2005-2018