C++ Builder Tutorials

Crash Course C++Builder - part 4: Event Handlers

Our Foot-To-Meter Convertor still doesn't do anything useful.

So, let's add some real code to the event handlers.
 

OnClick Event Handler for btnFootToMeter

In part 3 we've put some temporary code in the event handler of btnFootToMeter. But what should this event handler really do?

  1. The text "foot" should be shown in lblStartUnit.
  2. The text of edInput should be converted to a numerical value.
  3. From this number (the value in foot), the value in meter should be calculated.
  4. This number is converted again to text, which is shown in lblResult.
  5. Finally, the text " meter" should added to the caption of lblResult.

We'll keep temporary values and texts in the variables Foot, Meter and ResultString.
These variables have to be declared:
   - you indicate the type of each variable
   - you give a name to each variable.

For the moment, don't worry about all the different types of variables. Later on, we shall come back to this in detail.

Now, let's code!

  1. Start C++ Builder and open the project foot2metersimp.dpr.
  2. Replace the experimental text of btnFootToMeter's OnClick event handler with this new text:
    void __fastcall TForm1::btnFootToMeterClick(TObject *Sender)
    {
      float Foot;
      float Meter;
      string ResultString;
      lblStartUnit->Caption = "foot";
      Foot = StrToFloat(edInput->Text);
      Meter = Foot * 0.3048;
      ResultString = FormatFloat("0.00", Meter);
      lblResult->Caption = ResultString + " meter";
    }
  3. Compile and test: it works! ...at least, if you didn't make any typing errors ;-)
    Let's stop the application and continue programming.
  4. btnMeterToFoot should do the reverse of btnFootToMeter: the text "meter" must be shown in lblStartUnit and the value in meter should be converted to foot (divide it by 0.3048).
    The rest is almost identical to the handler we wrote for btnFootToMeter.

    Select btnMeterToFoot, select the OnClick event in the Object Inspector and double-click the empty field next to it.
    C++ Builder gives a name to the new event handler: btnMeterToFootClick.
  5. In the Code Editor, complete this event handler as follows:
    void __fastcall TForm1::btnMeterToFootClick(TObject *Sender)
    {
      float Foot;
      float Meter;
      String ResultString;
      lblStartUnit->Caption = "meter";
      Meter = StrToFloat(edInput->Text);
      Foot = Meter / 0.3048;
      ResultString = FormatFloat("0.00", Foot);
      lblResult->Caption = ResultString + " foot";
    }
  6. Save the project (Save All).
  7. Compile and test. Congratulations!

Adding comments

Now let's add some comments to our code.

Comments is very important! Today you still remember what your code means, but what when you want to modify it later on, in a month or in a year?

Comments text is preceded by a double slash // and is disregarded by the compiler.

Below we abundantly commented our code -- maybe we exagerated it a bit ;)

//----------------------------------------------------------
// Event handler for the OnClick of btnfoottometer
//----------------------------------------------------------
void __fastcall TForm1::btnFootToMeterClick(TObject *Sender)
{
  // Variables
  float Foot;
  float Meter;
  string ResultString;
  
  // Show the name of the starting length unit
  lblStartUnit->Caption = "foot";
  // Convert the text of the EDIT to a numerical value
  Foot = StrToFloat(edInput->Text);
  // Convert from foot to meter
  Meter = Foot * 0.3048;
  // Convert the floating point number to string-format.
  // "0.00" is the "format string", meaning:
  // Format the result with 2 decimal digits in the fractional part
  // (part to the right of the decimal separator)
  ResultString = FormatFloat("0.00", Meter);
  // Add the string " meter" and put the string in the CAPTION of the LABEL
  lblResult->Caption = ResultString + " meter";
}

//----------------------------------------------------------
// Event handler for the OnClick event of btnMeterToFoot
////-------------------------------------------------------- 
void __fastcall TForm1::btnMeterToFootClick(TObject *Sender)
{
  // Variables
  float Foot;
  float Meter;
  string ResultString;

  // Show the name of the starting length unit
  lblStartUnit->Caption = "meter";
  // Convert the text of the EDIT to a numerical value
  Meter = StrToFloat(edInput->Text);
  // Convert from meter to foot
  Foot = Meter / 0.3048;
  // Convert the floating point number to string-format.
  ResultString = FormatFloat("0.00", Foot);
  // Add the string " foot" and put the string in the CAPTION of the LABEL
  lblResult->Caption = ResultString + " foot";
}