Using Edit Box
In this tutorial, we’re going to learn using Edit component. Edit component or Edit box is in Standard page of component palette. An Edit box has some properties (such as Enabled, Name, Text, MaxLength, PasswordChar, ReadOnly, Visible etc.) and events (such as OnChange, OnEnter, OnExit, OnKeyPress, OnKeyUp etc.). Below is a table of some frequently used properties :
Using Edit Boxes for counting a rectangle area
For practicing using edit box, you need to create a form with some component like picture shown below :
First, change the form’s properties like table below :
Property | Value |
---|---|
Caption | Counting Rectangle Area |
Name | FormRectangleArea |
Position | poDesktopCenter |
Add three labels into the form. And each label has properties like table below :
Label | Property | Value |
---|---|---|
Label1 | Caption | Width |
Label2 | Caption | Length |
Label3 | Caption | Area |
Then add three edit boxes into the form. Set the properties like table below :
Label | Property | Value |
---|---|---|
Label1 | Caption | Width |
Label2 | Caption | Length |
Label3 | Caption | Area |
The last component you need is a button, which has properties like table below :
Property | Value |
---|---|
Caption | &Count |
Name | CountButton |
Now, save the project into a folder with any name. Remember, that you have to save the project into a folder, because a
Adding the codes
Countbutton is meant to count rectangle area by multiplying the value of width and length. To add codes, first click Countbutton. Activate the object inspector, click event tab, then double click the OnClick event. Type these codes :
procedure TFormRectangleArea.CountButtonClick(Sender: TObject);
var
W, L, A : integer;
begin
W := StrToInt(Width.Text);
L := StrToInt(Length.Text);
A := W * L;
Area.Text := IntToStr(A);
end;
Function StrToInt() is for converting a string value to integer, and function IntToStr() is for converting an integer value to string. An error will occur when you input a wrong width or length value. Ie : if you input 1A into width or length value, an error message will appear because 1A can not be converted into an integer value. To avoid this error, type these codes for OnKeyPress event of Width edit box and Length edit box :
procedure TFormRectangleArea.WidthKeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9', #8]) then key := #0;
end;
procedure TFormRectangleArea.LengthKeyPress(Sender: TObject;
var Key: Char);
begin
if not (key in ['0'..'9', #8]) then key := #0;
end;
The code if not (key in ['0'..'9', #8] then key := #0 will set key value to #0 (null) if the value is not in 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 or #8. #8 is Backspace.
makasih ya atas sharingnya, walaupun dengan gratul gratul pakde mencoba untuk memahami tutorial ini, pakde pikir sampean sangat berani , karena memberikan tutorial dengan berbahaa inggris.
ReplyDeletewah, lagu lama nih... dah lama sekali gak pegang delpi :)
ReplyDelete@ Pakde : thanks 4 the comment, it encourages me
ReplyDelete@Secangkir teh : let's learn together, I need your opinion
Hello Ari, i'm dropping by to wish you a nice weekend!
ReplyDelete@ Ana : Thank you Ana..
ReplyDeletes The best and a lot of secret drop-shipping suppliers in the world which will provide your products globally available for you directly to your customers doors. Also get the best drop-shipping manufacturer on your organization.
ReplyDelete