Delphi picking value from the field created at runtime

Asked

Viewed 843 times

0

Create at runtime various Tradiogroup:

Rdb1:= TRadioGroup.Create(Painel);

But with the same name "Rdb1". My doubt is how I get the value of the field that was selected. I tried to do so:

Rdb1.OnExit := Validacao;

procedure TFCad_AnaliseDeTendencias.Validacao(Sender: TObject); 
begin
  if Rdb1.ItemIndex = 1 then
  begin
    showmessage('Acertou');
  end;

However as I have several "Rdb1" it takes the value only of the last Radiogroup created. There is a way for me to get the value of Radiogroup that was "Clicked/Selected"?

  • Because you do not create the amount of radiogroup within a for, giving names to each of them after you need to get the value of a given radiogroup use Findcomponent by passing as parameter the name you gave. hugs!

  • 1

    " But as I have several "Rdb1" ... ". I could not understand. You can’t have multiple components with the same name. ?

1 answer

7


From what you explained, you don’t have multiple components with the same name, but you are using the same variable to create these components. One way you don’t have to worry about it, instead you can just use the Sender of the procedure as follows:

procedure TFCad_AnaliseDeTendencias.Validacao(Sender: TObject); 
begin
  if TRadioGroup(Sender).ItemIndex = 1 then
  begin
    showmessage('Acertou');
  end;
end;

Note: It is important that when creating the component (Create) you determine a name unique to him:

Rdb1.name := 'Radio' + controle;

Whereas controle is the variable you will need to define the logic to be generated.

  • Let’s say I want to play the value "Righted" to an Edit, which was created in the same way as Tradiogroup (Rdb1), as I would? I tried Tedit(Sender). Text := 'Right' but it didn’t work.

  • @Tiagocasanova although your question is relatively simple, it would be another question, not relative to which it has already been answered. I will reply here in the same comment, but it is not the correct procedure. If the answer has already removed the doubt of what was asked, mark as correct answer.

  • NomeEdit.Text := 'Acertou'; to assign. Sender is who sent the action, in this case the radio

  • The problem that "Nomeedit" is all with the same name. If I do Nomeedit.Text := 'Hit', it throws the value in the last field.

  • @Tiagocasanova is impossible that the name of Edit is the same. The name is the property name. Delphi does not allow 2 elements with the same name within a . dfm.

  • Truth expressed me wrong. If I have a String value := 'Edit1', how do I Value.name := 'Hit' ?

Show 2 more comments

Browser other questions tagged

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