Delphi how to make Tshape change color when new text is entered

6.4k Views Asked by At

im new to programming and i'm unsure why this wont work:

procedure TGIFtest.Edit1Change(Sender: TObject);
var
  Text : integer;
begin
 if text < 5 then
   shape1.brush.Color := clred
 else
   if text >= 5 then
     shape1.brush.Color := clgreen;
 end;

I want the shape to be red when a number entered in the edit is less than 5, and green if a number 5 or higher is entered. Thanks.

1

There are 1 best solutions below

0
On

The variable Text is not initialized, so you will have a random value.

If you want to have the integer value from the Edit text (string), then you have to get this text and convert it to an integer value.

procedure TGIFtest.Edit1Change(Sender: TObject);
var
  Text : integer;
begin

  Text := StrToInt( Edit1.Text );

  if text < 5 then
    shape1.brush.Color := clred
  else
    if text >= 5 then
      shape1.brush.Color := clgreen;
end;

If the Edit text can not converted to an integer, an exception is raised.

To avoid this you can use StrToIntDef or TryStrToInt