我有一個表格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