C++ Builder Tutorials

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, 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 properties!

The Object Inspector: Properties and Events

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

  2. 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 Form1 changes as you type.
  3. On the form, click Label2, in order to select it. In the Object Inspector, change the label's Caption to: equals
  4. 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
  5. Likewise, change the names of the following components:

       Label1 : lblStartUnit
       Label3 : lblResult
       Button1 : btnFootToMeter
       Button2 : btnMeterToFoot
  6. Change the Caption of btnFootToMeter to: Foot -> Meter
  7. Change the Caption of btnMeterToFoot to: Meter -> Foot
  8. Change the Text property of edInput to: 1
    This assures that the program has a valid value to start working with.
  9. 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,...
  10. 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.
  11. Look in the Code Editor: automatically, an empty template for this Event Handler has been created.
  12. 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;
    }
  13. 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.
  14. Stop the application and save the project (Save All).