Recent

Author Topic: Entering data through a form TEdit item  (Read 3614 times)

FPnewbie

  • Newbie
  • Posts: 6
Entering data through a form TEdit item
« on: April 24, 2014, 07:47:54 pm »
Easy enough to create a form with labels and make a TEdit enter field. Now, how do you use data that you enter in the form? I have a form (Form3) and a TEdit field (Edit1:TEdit). On event (OnEnter) I would like to use the information entered in field. Do I have to define a variable in my program somehow give it the "value" of the data entered in the Edit1:TEdit field? Am I right that anything entered in the field is a string?
This is probably a very basic operation, but I am having trouble. Really need an example showing how to enter numerical data and use it in a program. For example enter the number "12.0" in the field and get it ready to use as a real or float in my program.

Jkey

  • New Member
  • *
  • Posts: 44
Re: Entering data through a form TEdit item
« Reply #1 on: April 24, 2014, 10:16:12 pm »
TEdit has an OnChange event, which fires every time you add or delete a char in an Edit component. If you add this event (e.g. via Object Inspector), you can read the text value as a string (so yes, you're right).
Code: [Select]
procedure TForm3.Edit1Change(Sender: TObject);
begin
  Form3.Caption := Edit1.Text;
end;
If you try to get numerical data, you should try to convert it in this procedure, but be careful. You should always validate user's inputs before working with them, and prepare your application for incorrect formats. This example tries to convert the input to an integer and a real variables.
Code: [Select]
procedure TForm3.Edit1Change(Sender: TObject);
var   i: integer;
      x: real;
begin
  i := StrToIntDef(Edit1.Text,0);
  if not TryStrToFloat(Edit1.Text, x) then x := 0;
  Form3.Caption := 'Integer: ' + IntToStr(i) + '  Real: '+FormatFloat('0.##',x);
end;
« Last Edit: April 24, 2014, 10:17:43 pm by Jkey »

Evochrome

  • New Member
  • *
  • Posts: 25
Re: Entering data through a form TEdit item
« Reply #2 on: April 24, 2014, 10:31:18 pm »
You mean this?(see attachment)
I used a button but you can use the code in either the OnEditingDone or OnChange event or your OnEnter event :).
Cheers and good luck!

P.S. If you'll only use numbers, you might want to use a spinedit
« Last Edit: April 24, 2014, 10:46:46 pm by Evochrome »
Windows 8 64 bit. Lazarus 1.2.2 32 bit.
Windows 7 64 bit. Lazarus 1.2.2 32 bit.
Mac OS X Mavericks with lazarus

 

TinyPortal © 2005-2018