C++ Builder Tutorials

Crash Course C++Builder - part 2: Foot2Meter simplified

In this part we'll create a simplified version of Foot2Meter, the application that was introduced in part 1.

For now, the accent will not be on what the program does, but rather on how to start a project, how to save the files, add components, and so on. But very soon, we'll continue in a higher gear.

Preparations

  1. If you haven't done so yet, from the downloads section, download foot2metersimpcb.zip to a folder of your choice, such as \CppTutorials.
    Hold your horses! Don't unzip it yet: every project will get it's own directory, and it is best to keep all the original files in \CppTutorials.
  2. Create a new directory: \CppTutorials\Foot2MeterSimp. Of course, you can choose other names for your directories and projects. But if you use the structure that we suggest here, it will be a lot easier to follow along.
  3. Unzip foot2metersimp.zip to directory \CppTutorials\Foot2MeterSimp.

Starting at the end

As usual, we'll start with the end: how should our simplified Foot2Meter look like and what should it do?
  1. Open the project Foot2MeterSimp.cbproj.
  2. Compile the application. If you have forgotten how to do that, have a look at part 1.
    As with every succesful compilation, your application will start in the "environment" of the debugger.
    Just play around with the program.
  3. Notice that if you try to convert an invalid value, C++ Builder gives an error message. Continue your application. Afterwards you do not get a second error message, because in this simplified version of Foot2Meter we are not checking for a valid input (compare this behaviour with part 1).
  4. Stop the application and quit C++ Builder.
  5. Delete all the files from \CppTutorials\Foot2MeterSimp.

Your turn!

  1. In the File menu, select New, next select VCL Forms Application
  2. A new project will be started:

    • C++ Builder's title bar shows the name of the new project: Project1.
    • Notice the window with the title Form1. That window ("form") is the basis for your project.
      C++ applications for Windows are based on Forms. Every GUI application has one or more Forms. A Form is a component in the shape of a window. That's why Bill called it "Windows"...
      On the form you put other components, such as Buttons, Edit-boxes, Radiobuttons, ListBoxes, ComboBoxes and other well known Windows-creatures.
    • You also see the Editor window. That's where the source code can be viewed and edited. The source code is the result of the cooperation between C++ Builder and yourself: for every unit, a template is created automatically that you can complete.
  3. Unit1 is the name of the unit that goes with Form1. Builder creates the files Unit1.cpp, Unit1.h, and Unit1.dfm, containing the source code for Form1, the main form of the application.

    For the moment, all these files only exist in the RAM of your computer.

  4. In the Project Manager (panel at the top right), right click Project1.exe and rename it to Foot2MeterSimp.
  5. Open the File menu and click Save All.
    In the dialog, select directory \CppTutorials\Foot2MeterSimp. Next, save your files.

Files and file naming

It is best to save all files of a new project as soon as possible. That way, you won't experience nasty surprises as lost projects (what was that name again?), or an existing project being overwritten by a new one (and that's a lot worse...)

  • You are completely free in naming the first unit (in our case: the only unit).
  • Of course, all next units of the same project have to be given different names.
  • The name of the project file must be different from all unit-names. In our case, the name Foot2MeterSimp is not allowed for both the unit and the project, even while the file extensions are different.
  • From the project's name, C++ Builder will create the name for the compiled executable. In our case this will be Foot2MeterSimp.exe. Therefore it is worthwile thinking of a suitable name BEFORE starting a project.

Adding the Components

The next steps explain how to add the components to the form:

  1. Bring the form window to the front. Tip: you also can press function key F12, which toggles between the code in the editor and the "form".
  2. In the tool palette, click on the icon of the TEdit component. It is on the Standard page of the palette.
    Next, click somewhere in the form. An Edit-box appears on the form, with the text Edit1.
  3. Drag component Edit1 to the upper left-hand corner of the form, right below the title bar.
  4. Click the TLabel component on the component palette.
  5. Move the cursor to somewhere to the right of Edit1 and click: Label1 is placed on the form.
  6. Add another TLabel to the right of Label1. Label2 is born.
  7. Add a TButton component (also on the standard page), somewhere below the Edit-box. A button with the name Button1 appears.
  8. Below Button1, add a second TButton to the form.
  9. Finally, add a TLabel to the right of Button1. By now, your form should look similar to this:

  10. Drag the components until Form1 more or less matches the picture shown above.
    Size the form by dragging the bottom right corner of its border.
  11. Time to save your work: in the File menu, select Save All.
  12. Let's test. In the Run menu, select Run (or press key F9).

    C++ Builder will compile and run your program.
    The program doesn't seem to do anything yet, but make no mistake: this is a full blown Windows application! You can size and move the window, enter text in the Edit, click the buttons,...