我有一个表格TForm1
有5 TEdit
和2 TBitBtn
。如何读取和更改TEdit控件的值?
我还需要程序,以便在BitBtn1Click
,Edit1
和Edit2
价值Edit1
和Edit2
输入数字数据后,会总结,并会显示在Edit3
。
我有一个表格TForm1
有5 TEdit
和2 TBitBtn
。如何读取和更改TEdit控件的值?
我还需要程序,以便在BitBtn1Click
,Edit1
和Edit2
价值Edit1
和Edit2
输入数字数据后,会总结,并会显示在Edit3
。
你想要做这样的事情:
var
val1, val2, sum: Integer;
...
val1 := StrToInt(Edit1.Text);
val2 := StrToInt(Edit2.Text);
sum := val1 + val2;
Edit3.Text := IntToStr(sum);
如果你想浮点运算做这样的
var
val1, val2, sum: Double;
...
val1 := StrToFloat(Edit1.Text);
val2 := StrToFloat(Edit2.Text);
sum := val1 + val2;
Edit3.Text := FloatToStr(sum);
读取和设置一个TEdit控件的值,你只需简单地引用该控件的Text
属性。 Text属性的类型是String。
由于Text是一个String属性,因此您可以在代码中将其作为String变量处理。你可以将它传递到期望一个字符串常量的函数:
// Edit1 is the name of the TEdit control
// Display the value in the edit control to the user
ShowMessage(Edit1.Text);
你可以用一个简单的分配将其分配给一个字符串变量:
var
// My string variable
myString: String;
begin
// Edit1 is the Name of the control
myString := Edit1.Text;
end;
要设置TEdit控件的值,只需将一个字符串分配给Text属性即可。这可能是一个字符串常量:
Edit1.Text := 'hello';
或者它可能是从字符串变量:
Edit1.Text := myString;
数学上的数字类型完成,因此算术,你需要使用一个功能将字符串值转换为数字。
整数运算,可以使用StrToInt()
或StrToIntDef()
:
var
myInteger: Integer;
begin
// Convert Edit1.Text string to a number and assign to numeric type for math
// If the value in Edit1.Text cannot be converted, an exception will be raised
myInteger := StrToInt(Edit1.Text);
end;
使用StrToIntDef()
:
var
myInteger: Integer;
begin
// If Edit1.Text cannot be converted, the default value of 0 will be used
myInteger := StrToIntDef(Edit1.Text, 0);
end;
对于浮点运算,使用StrToFloat()
或StrToFloatDef()
代替。
要指定一个整数回到Text属性,你需要将整数转换为字符串分配前:
var
myInteger: Integer;
begin
myInteger := 12;
Edit1.Text := IntToStr(myInteger);
end;
对于浮点,使用FloatToStr()
。
最后,把一切融合在一起,得到的两个编辑框的数值,并显示在第三编辑框中的总和,只要做到这一点:
var
// Floating point variables
value1: Real;
value2: Real;
sum: Real;
begin
// Get the values from the edit boxes, converting them to floating point types
value1 := StrToFloat(Edit1.Text);
value2 := StrToFloat(Edit2.Text);
// Sum them
sum := value1 + value2;
// Convert the sum to string and assign back to edit box
Edit3.Text := FloatToStr(sum);
end;
或者一步到位:
Edit3.Text := FloatToStr(StrToFloat(Edit1.Text) + StrToFloat(Edit2.Text));
你忘了放在try..except语句来检查非数字输入。 – Johan 2011-12-16 21:07:18
@Johan,这是故意的,但我警告过例外情况。保持简单。 – 2011-12-16 21:40:50
我注意到下面的代码片段:
,使得EDIT1和EDIT2
输入数值数据后
如果您只想允许数字数据,最好在编辑框中禁止非数字数据。
以下是如何做到这一点。
const
TabKey = #9;
Backspace = #8;
Enter = #13;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9','-',TabKey,Enter,Backspace]) then Key:= #0; //integers
//realnumbers: if not (Key in ['0'..'9','-','e','E','.',TabKey,Enter,Backspace]) then Key:= #0;
end;
如果你只有整数数据,这是不行的,如果你有科学的数字,你需要做一些测试信e
,小数点以及允许无理数。
不管你做什么,检查输入是一个有效的数字并让用户知道是个好主意。
procedure TForm1.Edit1Change(Sender: TObject);
var
MyEdit: TEdit;
OtherEdit: TEdit;
TryNumber: double;
OtherNumber: double;
Success: boolean;
begin
Success:= true;
if (Sender is TEdit) then begin
MyEdit:= TEdit(Sender);
try
if MyEdit.Text = '' then TryNumber:= 0
else TryNumber:= StrToFloat(MyEdit.Text);
MyEdit.Color:= clWindow; //all is OK make edit standard white.
MyEdit.Hint:= '';
except
MyEdit.Color:= clRed; //Let the user know the output will not compute.
MyEdit.Hint:= MyEdit.Text + ' is not a valid number ';
Success:= false;
end;
end;
if (MyEdit = Edit1) then OtherEdit:= Edit2
else OtherEdit:= Edit1;
try
if OtherText.Text = '' then OtherNumber:= 0
else OtherNumber:= StrToFloat(OtherEdit.Text);
except
Success:= false;
end;
if Success then Edit3.Text:= FloatToStr(TryNumber + OtherNumber);
end;
请注意,您可以将这个事件既Edit1
和Edit2
,所以你不必两次写的代码。 (但我相信你已经知道了)。
(两个编辑共享相同的事件)。
重要的事情要记住
try..except
发现错误,使你的程序不会出错打破,请参阅:http://www.delphibasics.co.uk/Article.asp?Name=Exceptionsmaskedit
,或者编写自己的过滤器(如果这样做是微不足道的)。
你不需要在你的问题中告诉我们你的名字。在问题的下面。另外,除了delphi-xe2标签之外,我还添加了通用的delphi标签,因为这会在更多潜在的回答者面前提出您的问题。最后,请不要在该问题中提供下载链接。我们不想下载东西。把代码放在问题中并对其进行格式化。 – 2011-12-16 18:11:56