Search code examples
delphidelphi-xe6

Storing the value of variable using TRadioGroup


I want to change the value of T according to a particular selection but it's not changing. Please have a look. The variable T has been declared along with Form1:TForm1 before 'implementation'. Basically, T should get assigned a linear or non linear equation depending upon the the selection of the respected radio buttons. I put a TEdit in the form so as to get an idea whether it is working or not. The last part is just a way to check by taking an example of Integer values.

Also, if I am not able to give a clear idea then just suggest me how to store a value of the concerned value using the Radiobuttons of the RadioGroup.

procedure TForm1.RadioGroup1Click(Sender: TObject);
begin

 if RadioGroup1.Items[RadioGroup1.ItemIndex] = 'Linear Tension' then
    T:= 5;
  if RadioGroup1.Items[RadioGroup1.ItemIndex] = 'Non-Linear tension' then
    T:= 10;
end;

procedure TForm1.Edit1Change(Sender: TObject);

var
code: Integer;
value: Real;
begin
  Val(Edit1.Text,value,code);
  Edit1.Text := formatfloat('#.0', T);

end;

end.

Solution

  • Do not compare the captions because you will have magic values in your code.

    Declare a ValueObject containing the Value and the Name

    type
      TTensionValue = record
      private
        FValue : Integer;
        FName : string;
      public
        constructor Create( AValue : Integer; const AName : string );
        class function EMPTY : TTensionValue;
        property Value : Integer read FValue;
        property Name : string;
      end;
    
      TTensionValues = TList<TTensionValue>;
    
    class function TTensionValue.EMPTY : TTensionValue;
    begin
      Result.FValue := 0;
      Result.FName := '';
    end;
    
    constructor TTensionValue.Create( AValue : Integer; const AName : string );
    begin
      // Validation of AValue and AName
      if AName = '' then
        raise Exception.Create( 'AName' );
      if AValue < 0 then
        raise Exception.Create( 'AValue' );
    
      FValue := AValue;
      FName := AName;
    end;
    

    Prepare a List with valid entries

    type
      TForm1 = class( TForm )
      ...
        procedure RadioGroup1Click( Sender: TObject );
      private
        FTensions : TTensionValues;
        procedure PopulateTensions( AStrings : TStrings );
      public
        procedure AfterConstruction; override;
        procedure BeforeDestruction; override;
      end;
    
    procedure TForm1.AfterConstruction;
    begin
      inherited;
      FTensions := TTensionValues.Create;
    
      FTensions.Add( TTensionValue.Create( 5, 'Linear Tension' ) );
      FTensions.Add( TTensionValue.Create( 10, 'Non-Linear tension' ) );
    end;
    
    procedure TForm1.BeforeDestruction;
    begin
      FTenstions.Free;
      inherited;
    end;
    

    Populate that list to the RadioGroup

    procedure TForm1.PopulateTensions( AStrings : TStrings );
    var
      LValue : TTensionValue;
    begin
      AStrings.BeginUpdate;
      try
        AStrings.Clear;
        for LValue in FTensions.Count - 1 do
          AStrings.Add( LValue.Name );
      finally
        AStrings.EndUpdate;
      end;
    end;
    
    procedure TForm1.FormShow( Sender.TObject );
    begin
      PopulateTensions( RadioGroup1.Items );
    end;
    

    Now you only ask the TensionList for the value

    procedure TForm1.RadioGroup1Click( Sender: TObject );
    begin
      T := FTensions[RadioGroup1.ItemIndex].Value;
    end;
    

    The selected value now only rely on the chosen ItemIndex and not on the caption text.