Function does not find text in Tedit

0

With the code below I'm trying to find text in a Tedit ,

procedure TForm2.CheckBox3Click(Sender: TObject);
var i: integer;
   begin
   existeCampo := false;
    begin
       for i := 1 to form1.variavel do
       begin
          if TEdit(form1.FindComponent('edt_variavel'+IntToStr(i))).Text = 'dose_adicional' then
           begin
            existeCampo := true;
            break;
            checkbox3.Checked := true;
           end;
         end;

      if existeCampo = false then
        begin
        Showmessage('Para utilizar essa opção, adicione uma variavel com o nome de "dose_adicional");
        Checkbox3.Checked :=false;
        end;
     end;
   end;

I have already done several tests, and it does not find the text, even if I type correctly in the edit, why does this happen?

    
asked by anonymous 11.02.2016 / 19:11

1 answer

1

I tested and found a problem in its encoding in the for part, the components always start at 0 (zero) and not 1 (one), maybe if the text dose_addition was the one it promised it would not fall into the IF. And I've also changed it to a more practical way, but yours is functional.

In the part of the break I passed a line below, otherwise your checkbox will not be checked.

procedure TForm2.CheckBox3Click(Sender: TObject);
var i: integer;
begin
   existeCampo := false;

   for I := 0 to Componentcount-1 do
      begin
         if Components[i] is TEdit then
            begin
               if TEdit(Components[i]).Text = 'dose_adicional' then
                  begin
                     existeCampo := true;
                     checkbox3.Checked := true;
                     break;
                  end;
            end;
      end

  if existeCampo = false then
     begin
        Showmessage('Para utilizar essa opção, adicione uma variavel com o nome de "dose_adicional");
        Checkbox3.Checked :=false;
     end;
end;
    
11.02.2016 / 23:23