Crash Course C++Builder - part 3: Properties and Events
Time to bring some life into our project.
Let's change the properties of some of the components: their
caption, color, size, font, and so on. And let's add just a little bit
of code. Finally, I hear you sigh... isn't that all is programming
about...? But C++ Builder is at least as much about components and their
The Object Inspector: Properties and Events
- In the Object Inspector you can change the properties of the components at design time.
If you don't see the Object Inspector, open it with the menu: View / Tool Windows / Object Inspector (or press
function key F11).
- Make sure that Form1 is selected in the Object Inpector, as shown in the picture above.
Click next to the Caption property and type: Foot2Meter Simplified. The text in the title bar of the "form" changes as you type.
- On the form, click Label2, in order to select it. In the Object Inspector, change the label's Caption to: equals
- The names of our components are not very descriptive. Let's at least change the names of the components that
will be used in the program's source code. That is, the components whose properties will
be changed or used when the program is running.
Select Edit1 and change its' Name property to: edInput
- Likewise, change the names of the following components:
Label1 : lblStartUnit
Label3 : lblResult
Button1 : btnFootToMeter
Button2 : btnMeterToFoot
- Change the Caption of btnFootToMeter to: Foot -> Meter
- Change the Caption of btnMeterToFoot to: Meter -> Foot
- Change the Text property of edInput to: 1
This assures that the program has a valid value to start
- In the Object Inspector, you also indicate the events to which a component should respond.
Select btnFootToMeter. Next, select the Events tab of the Inspector: you'll see the events OnClick, OnEnter,...
- Double-click in the white field to the right of the OnClick event.
C++ Builder will write btnFootToMeterClick into this field. That will be the name of the
Event Handler, the function
that is executed each time btnFootToMeter is clicked.
- Look in the Code Editor: automatically, an empty template for this Event Handler has been created.
- Let's experiment for a moment with some (temporary) source code. Complete the code of the event handler as follows:
void __fastcall TForm1::btnFootToMeterClick(TObject *Sender)
lblStartUnit->Caption = "foot";
lblResult->Caption = edInput->Text;
- Compile and run the program. Type something in the Edit-box and click the first convert button: the text "foot"
appears in lblStartUnit and the text of the Edit-box appears in lblResult. In C++ language:
- the string "foot" is assigned to the property Caption of a label component;
- the property text of edInput is assigned to the property Caption of another label component.
- Stop the application and save the project (Save All).