0

我想根据特定的选择更改 T 的值,但它没有改变。请看一看。变量 T 在“实现”之前已与 Form1:TForm1 一起声明。基本上,T 应该被分配一个线性或非线性方程,具体取决于对相关单选按钮的选择。我在表格中放了一个 TEdit,以便了解它是否有效。最后一部分只是以整数值为例进行检查的一种方法。

此外,如果我无法给出一个清晰的想法,那么只需建议我如何使用 RadioGroup 的 Radiobuttons 存储相关值的值。

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.
4

3 回答 3

4

对 RadioGroup 项目使用文本比较确实不是一个好主意。最好直接使用ItemIndex

procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
  case RadioGroup1.ItemIndex of  
    0: T := 5;
    1: T := 10;
  else
    raise Exception.Create('No item selected - should not get here');
  end;
  ShowMessage(FloatToStr(T));
end;
于 2014-06-04T14:43:51.757 回答
1

不要比较标题,因为您的代码中会有神奇的值。

声明一个包含值和名称的 ValueObject

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;

准备一个包含有效条目的列表

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;

将该列表填充到 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;

现在您只需向 TensionList 询问值

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

选择的值现在只依赖于选择的 ItemIndex 而不是标题文本。

于 2014-06-04T14:42:39.497 回答
1

据我所知,您只是想更改Edit1单击 RadioGroup1 时显示的值。要实现这一点,您需要做的就是移动

Edit1.Text := formatfloat('#.0', T);

到您的RadioGroup1Click程序结束。

我假设Edit1ChangeonChange. Edit1如果是这样,根据文档Text,此过程仅在属性可能已更改时才被调用。因此,不仅不会调用此过程(delphi 怎么知道您打算使用 的值T来更改 的文本Edit1?),当它被调用时,它可能会导致堆栈溢出,因为更改文本值间接调用事件onChange。(尽管将它设置为它已经拥有的相同值可能不会调用它)。

话虽如此,检查一个值是否被正确更改,不需要 a TEdit, aTLabel将更适合那里。尽管在您的情况下,我会选择简单地放置一个断点并单步执行代码以查看值 get 是否正确更改。

您的代码还存在许多其他问题,例如格式不一致、魔法值、错误的命名约定和无用的代码行,我建议您在养成不良习惯之前阅读这些问题。

于 2014-06-04T18:00:09.413 回答