Docsity

ECET 455 Lab: C++ Object Oriented Programming with Purdue University Calumet - Prof. Omer , Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
Purdue University Calumet Department of Electrical and Computer
Engineering Technology ECET 455 C++ Object Oriented Programming
ECET_455_Lab_Example_01
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.
3) Two Labels
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.
pf3
pf4

Partial preview of the text

Download ECET 455 Lab: C++ Object Oriented Programming with Purdue University Calumet - Prof. Omer and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Engineering Technology ECET 455 C++ Object Oriented Programming

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.

  1. Two Labels

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.

Engineering Technology ECET 455 C++ Object Oriented Programming

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:

Engineering Technology ECET 455 C++ Object Oriented Programming

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.