


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



ECET_455_Lab_Example_
Task Defination: The integer input is taken from two edit controls and when a button is clicked the result is shown with a label.
Procedure: Place on the Form the following objects: 1)Two edit controls 2)A Button.
Pace the following code under the Button1Click event handler:
void __fastcall TForm1::Button1Click(TObject *Sender) { try { Label1->Caption = IntToStr(StrToInt(Edit1->Text) * StrToInt(Edit2->Text)); } catch(...) { Application->MessageBox("You must specify integer values. Please try again.", "IntToStr Example", IDOK); }
} Explanation Note:
Edit control: TEdit is a wrapper for a Windows single-line edit control.
Use a TEdit object to put a standard Windows edit control on a form. Edit controls are used to retrieve text that users type. Edit controls can also display text to the user.
When only displaying text to the user, choose an edit control to allow users to select text and copy it to the Clipboard. Choose a label object if the selection capabilities of an edit control are not needed.
TEdit implements the generic behavior introduced in TCustomEdit. TEdit publishes many of the properties inherited from TCustomEdit, but does not introduce any new behavior. For specialized edit controls, use other descendant classes of TCustomEdit or derive from it.
Text property: Use the Text property to read the Text of the control or specify a new string for the Text value. By default, Text is the control name. For edit controls and memos, the Text appears within the control. For combo boxes, the Text is the content of the edit control portion of the combo box.
Note: Controls that display text use either the Caption property or the Text property to specify the text value. Which property is used depends on the type of control. In general, Caption is used for text that appears as a window title or label, while Text is used for text that appears as the content of a control.
Text Examples: This example copies the text in an edit box into a null-terminated string, and puts this string in another edit box when the user clicks the button on the form.
void __fastcall TForm1::Button1Click(TObject *Sender)
{ int Size = Edit1->GetTextLen(); //Get length of string in Edit
Size++; //Add room for null character
char *Buffer = new char[Size]; //Creates Buffer dynamic variable
Edit1->GetTextBuf(Buffer,Size); //Puts Edit1->Text into Buffer Edit2->Text = Buffer; delete Buffer;
}
Note: the same thing could be accomplished more simply as follows:
void __fastcall TForm1::Button1Click(TObject *Sender)
{ Edit2->Text = Edit1->Text; }
StrToInt method: Converts an AnsiString that represents an integer (decimal or hex notation) to a number. StrToInt converts the AnsiString S, which represents an integer-type number in either decimal or hexadecimal notation, into a number. If S does not represent a valid number, StrToInt throws an EConvertError exception.
StrToInt Example:
Because TLabel is not a descendant of TWinControl, it does not have its own window and can’t receive direct input from the keyboard. To add an object to a form that can respond to keyboard input (other than setting focus to another object when an accelerator key is typed) in addition to displaying text, use TStaticText.
To add an object to a form that displays text that a user can scroll or edit, use TEdit.
Caption, property: Use Caption to specify the text string that labels the control.
To underline a character in a Caption that labels a component, include an ampersand (&) before the character. This type of character is called an accelerator character. The user can then select the component by pressing Alt while typing the underlined character. To display an ampersand character in the caption, use two ampersands (&&).
Note: Controls that display text use either the Caption property or the Text property to specify the text value. Which property is used depends on the type of control. In general, Caption is used for text that appears as a window title or label, while Text is used for text that appears as the content of a control.