Friday 9 April 2010

Delphi Tutorial # 3

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 :

PropertyValue
CaptionCounting Rectangle Area
NameFormRectangleArea
PositionpoDesktopCenter

Add three labels into the form. And each label has properties like table below :

LabelPropertyValue
Label1CaptionWidth
Label2CaptionLength
Label3CaptionArea

Then add three edit boxes into the form. Set the properties like table below :

LabelPropertyValue
Label1CaptionWidth
Label2CaptionLength
Label3CaptionArea

The last component you need is a button, which has properties like table below :

PropertyValue
Caption&Count
NameCountButton

Now, save the project into a folder with any name. Remember, that you have to save the project into a folder, because a Delphi project contains some files. Give meaningful names for the unit and the project you created, such as UnitRectangle and ProjectRectangle. Run the program by pressing F9. If you press the button, nothing happen because we haven’t written some codes yet.

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.

Go to Previous Tutorial or Next Tutorial

6 comments:

  1. 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.

    ReplyDelete
  2. wah, lagu lama nih... dah lama sekali gak pegang delpi :)

    ReplyDelete
  3. @ Pakde : thanks 4 the comment, it encourages me
    @Secangkir teh : let's learn together, I need your opinion

    ReplyDelete
  4. Hello Ari, i'm dropping by to wish you a nice weekend!

    ReplyDelete
  5. s 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

These links are part of a pay per click advertising program called Infolinks. Infolinks is an In Text advertising service; they take my text and create links within it. If you hover your mouse over these double underlined links, you will see a small dialog box containing an advertisement related to the text. You can choose to move the mouse away and go on with your browsing, or to click on the box and visit the relevant ad. Click here to learn more about Infolinks Double Underline Link Ads.