Function does not find text in Tedit

Asked

Viewed 135 times

0

With the code below I’m trying to find a 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’ve done several tests, and he can’t find the text, even though I typed it correctly on Edit, why does this happen?

  • There is a component edt_variavel<i> ? He is a TEdit or some other component? form1.variavel ? What is the usefulness of that x:=0; in the middle of nowhere?

  • yes it exists, it is a Tedit, Form1.variable for being from 1 to 15. x = 0 has been removed.

  • it’s just a kick, so I won’t put it as an answer, but try to put as TEdit on the return of FindComponent I think you’re having trouble because it’s transforming the return of FindComponent which is a TComponent in a TEdit, and then in the case he has the name, but you won’t get the text

  • but I do the same thing to get the text and play inside an array, why wouldn’t it work?

1 answer

1


I tested and found a problem in its coding in the for part, the components always start at 0 (zero) and not at 1 (one), maybe if the additional dose_text were the first one it would not fall in the IF. And I also switched to a more practical form, but its functional.

In the break part 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;
  • negative, error still persists.

  • which error message?

  • He just can’t find.

  • guy on my test passed, put some 5 Dit on the screen and put random words and the additional dose and went from good... debug the code to see if it goes through each Dit or another component and picks up the right text too

  • 1

    really, I did the same test and it worked.. so ta response functional.. I’ll see what’s happening thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.