我想根据特定的选择更改T的值,但它不会改变。请看一看。在'implementation'之前,变量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.
答案 0 :(得分: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;
答案 1 :(得分:1)
请勿比较字幕,因为您的代码中会包含魔术值。
声明包含Value和Name
的ValueObjecttype
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,而不依赖于标题文本。
答案 2 :(得分:1)
据我所知,当您点击RadioGroup1时,您只是尝试更改Edit1
上显示的值。要实现这一目标,您需要做的就是移动
Edit1.Text := formatfloat('#.0', T);
到RadioGroup1Click
程序的结尾。
我假设Edit1Change
是onChange
的{{1}}程序。如果是这样,根据the documentation,只有在Edit1
属性可能已更改时才会调用此过程。因此,不仅不会调用此过程(delphi如何知道您打算使用Text
的值来更改T
的文本?),当它被调用时,它可能会导致堆栈溢出,因为更改文本值会间接调用Edit1
事件。 (虽然将它设置为已经具有的相同值可能不会调用它。)
话虽如此,检查一个值是否正确更改,不需要onChange
,TEdit
更适合那里。虽然在您的情况下,我会选择简单地放置断点并单步执行代码以查看值是否正确更改。
您的代码还存在许多其他问题,例如格式不一致,魔术值,错误的命名约定以及没有任何目的的代码行,我建议您在陷入不良习惯之前先阅读这些问题