简体   繁体   English

使用TEdit作为变量?

[英]Use TEdit as a variable?

I have several TEdit boxes on my form. 我的表单上有几个TEdit框。 The data is tied to hardware that has the same min and max values for each range but several ranges. 数据绑定到每个范围具有相同的最小值和最大值但具有多个范围的硬件。 If the user updates the max value for any range by typing in the TEdit box, I want update the corresponding min box as well as a TEdit box that holds the product of the max times another value, etc. Since all the TEdit boxes have similar names, I would like to know if I can pass just the distinguishing string to a generic procedure and construct the name of the specific TEdit boxes to alter there. 如果用户通过在TEdit框中键入来更新任何范围的最大值,则我要更新相应的min框以及一个保存最大乘以另一个值的乘积的TEdit框,等等。名称,我想知道是否可以仅将区别字符串传递给通用过程,并构造特定的TEdit框的名称以在其中进行更改。 At run-time, I get "Access violation at address 0074CB11 in module 'MyProject.exe'. Read of address 8BD88B77." 在运行时,我收到“模块'MyProject.exe'中地址0074CB11的访问冲突。读取地址8BD88B77。” Any advice would be appreciated. 任何意见,将不胜感激。

procedure TForm1.R1IMaxEditChange(Sender: TObject);
//User types a new Range 1 Maximum I value
begin
  UpdateIMin_Power('R1')
end;


procedure TForm1.UpdateIMin_Power(Range: string);
var
  R_IMax, R_IMin, R_Power, R_Volts: TEdit;
begin
  //Assign variable names to TEdit boxes already on form.
  R_IMax.Name := Range + 'IMaxEdit';  //'R1IMaxEdit' when called by R1IMaxEditChange
  R_Volts.Name := Range + 'VoltsEdit';
  R_IMin.Name := Range + 'IMinEdit';
  R_IPower.Name := Range + 'IPowerEdit';

  //IMax already manually entered by user in R1IMaxEdit; Volts already on form
  R_IMin.Text := R_IMax.Text;  //Let IMin = IMax
  R_Power.Text := FloatToStr(StrToFloat(R_IMax.Text) * StrToFloat(R_Volts.Text)); //Power = Imax * Voltage
end;

You have four variables R_IMax , R_IMin , R_Power , R_Volts . 您有四个变量R_IMaxR_IMinR_PowerR_Volts You do not initialise these variables, but you then attempt to use methods and properties of them. 您没有初始化这些变量,但随后尝试使用它们的方法和属性。 That is an error. 那是一个错误。

You must assign object references before using them. 您必须先分配对象引用,然后再使用它们。 You need code like this: 您需要这样的代码:

R_IMax := GetEditReferenceFromSomewhere(Range + 'IMaxEdit');

Obviously you need an implementation of GetEditReferenceFromSomewhere . 显然,您需要实现GetEditReferenceFromSomewhere Personally I would pass the four edit controls as parameters to the method: 我个人将四个编辑控件作为参数传递给方法:

procedure TForm1.UpdateIMin_Power(R_IMax, R_IMin, R_Power, R_Volts: TEdit);
begin
  //IMax already manually entered by user in R1IMaxEdit; Volts already on form
  R_IMin.Text := R_IMax.Text;  //Let IMin = IMax
  R_Power.Text := FloatToStr(StrToFloat(R_IMax.Text) * StrToFloat(R_Volts.Text)); //Power = Imax * Voltage
end;

If you want to treat these controls as a group, then create a record type to hold them: 如果要将这些控件视为一个组,则创建一个记录类型来保存它们:

type
  TEditControlGroup = record
    R_IMax: TEdit;
    R_IMin: TEdit;
    R_Power: TEdit;
    R_Volts: TEdit;
  end;

Declare and populate a bunch of these records when your form is created, and pass them to the method which now looks like this: 创建表单时声明并填充一堆这些记录,并将它们传递给现在看起来像这样的方法:

procedure TForm1.UpdateIMin_Power(const Controls: TEditControlGroup);
begin
  //IMax already manually entered by user in R1IMaxEdit; Volts already on form
  Controls.R_IMin.Text := Controls.R_IMax.Text;  //Let IMin = IMax
  Controls.R_Power.Text := FloatToStr(StrToFloat(Controls.R_IMax.Text) * StrToFloat(Controls.R_Volts.Text)); //Power = Imax * Voltage
end;

If you are simply desperate to use the control names then you could use FindComponent but I am simply loathe to recommend that. 如果您只是急于使用控件名称,则可以使用FindComponent但我只是不推荐这样做。

procedure TForm1.UpdateIMin_Power(const Range: string);
var
  R_IMax, R_IMin, R_Power, R_Volts: TEdit;
begin
  //Assign variable names to TEdit boxes already on form.
  R_IMax := FindComponent(Range + 'IMaxEdit');  //'R1IMaxEdit' when called by R1IMaxEditChange
  R_Volts := FindComponent(Range + 'VoltsEdit');
  R_IMin := FindComponent(Range + 'IMinEdit');
  R_IPower := FindComponent(Range + 'IPowerEdit');

  //IMax already manually entered by user in R1IMaxEdit; Volts already on form
  R_IMin.Text := R_IMax.Text;  //Let IMin = IMax
  R_Power.Text := FloatToStr(StrToFloat(R_IMax.Text) * StrToFloat(R_Volts.Text)); //Power = Imax * Voltage
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM