Home

VCL Examples: Geometric Figures

 

The purpose of this exercise is to apply various calculations and perform some geometry-related calculation while doing it. First, this allows us to create a property sheet with its property pages, using the Page Control. In each property page, we use an Image control to represent a geometric figure. On the right side of the body of each property page, we request the necessary value(s) from the user and provide a button that would be used to actually perform the calculations.

Practical Learning Practical Learning: Designing Edit Boxes

 

  1. Open the Geometry project created when reviewing the property pages
  2. By selecting the Label and Edit controls from the Standard tab of the Component Palette, design each page as follows:
     
    Control Name Caption/Text Glyph
    Label   Side:  
    Edit edtSquareSide 0.00  
    BitBtn btnCalcSquare Calculate Square
    Label   Perimeter:  
    Edit edtSquarePerimeter 0.00  
    Label   Area:  
    Edit edtSquareArea 0.00  
    Label   Length:  
    Edit edtRectLength 0.00  
    Label   Height  
    Edit edtRectHeight Height:  
    BitBtn btnCalcRectangle Calculate Rectangle
    Label   Perimeter  
    Edit edtRectPerimeter 0.00  
    Label   Area:  
    Edit edtRectArea 0.00  
    Control Name Caption/Text Glyph
    Label   Radius:  
    Edit edtCircleRadius 0.00  
    BitBtn btnCalcCircle Calculate Circle
    Label   Circumference:  
    Edit edtCircleCircumf 0.00  
    Label   Area:  
    Edit edtCircleArea 0.00  
    Label   radius:  
    Edit edtEllipseSRadius 0.00  
    Label   Radius:  
    Edit edtEllipseLRadius 0.00  
    BitBtn btnCalcEllipse Calculate Ellipse
    Label   Circumference:  
    Edit edtEllipseCircumf 0.00  
    Label   Area:  
    Edit edtEllipseArea 0.00  
    Control Name Caption/Text Glyph
    Label   Side:  
    Edit edtCubeSide 0.00  
    BitBtn btnCalcCube Calculate Cube
    Label   Area:  
    Edit edtCubeArea 0.00  
    Label   Volume:  
    Edit edtCubeVolume 0.00  
    Label   Base:  
    Edit edtBoxBase 0.00  
    Label   Height:  
    Edit edtBoxHeight 0.00  
    Label   Width:  
    Edit edtBoxWidth 0.00  
    BitBtn btnCalcBox Calculate Box
    Label   Area:  
    Edit edtBoxArea 0.00  
    Label   Volume:  
    Edit edtBoxVolume 0.00  
  3. Display the Quadrilateral tab and double-click the top Calculate button
  4. Implement its event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::btnCalcSquareClick(TObject *Sender)
    {
        double side, perimeter, area;
    
        try {
            side = this->edtSquareSide->Text.ToDouble();
            perimeter = side * 4;
            area = side * side;
    
            this->edtSquarePerimeter->Text = FloatToStr(perimeter);
            this->edtSquareArea->Text = FloatToStr(area);
        }
        catch(EConvertError *err)
        {
            ShowMessage("The value you provided is an invalid number.");
            this->edtSquareSide->Text = "0.00";
            this->edtSquarePerimeter->Text = "0.00";
            this->edtSquareArea->Text = "0.00";
        }
    }
    //---------------------------------------------------------------------------
  5. Return to the Quadrilateral property page and double-click the bottom Calculate button
  6. Implement its event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::btnCalcRectangleClick(TObject *Sender)
    {
        double length, height, perimeter, area;
    
        try {
            length = this->edtRectLength->Text.ToDouble();
            height = this->edtRectHeight->Text.ToDouble();
            perimeter = (length + height) * 2;
            area = length * height;
    
            this->edtRectPerimeter->Text = FloatToStr(perimeter);
            this->edtRectArea->Text = FloatToStr(area);
        }
        catch(EConvertError *err)
        {
            ShowMessage("Incorrect value(s). Please try again."); 
            this->edtRectPerimeter->Text = "0.00";
            this->edtRectArea->Text      = "0.00";
        }
    }
    //---------------------------------------------------------------------------
  7. Return to the form and click the Circular tab
  8. Double-click the top Calculate button and implement its event as follows:
     
    const double myPI = 3.14159;
    //---------------------------------------------------------------------------
    void __fastcall TForm1::btnCalcCircleClick(TObject *Sender)
    {
        double radius, circumference, area;
    
        try {
            radius = this->edtCircleRadius->Text.ToDouble();
            circumference = radius * 2 * myPI;
            area = radius * radius * myPI;
    
            this->edtCircleCircumf->Text = FloatToStr(circumference);
            this->edtCircleArea->Text    = FloatToStr(area);
        }
        catch(EConvertError *err)
        {
            ShowMessage("The value you provided for the radius is incorrect.");
            this->edtCircleRadius->Text  = "0.00";
            this->edtCircleCircumf->Text = "0.00";
            this->edtCircleArea->Text    = "0.00";
        }
    }
    //---------------------------------------------------------------------------
  9. Return to the Circular property page and double-click the bottom Calculate button
  10. Implement its event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::edtCalcEllipseClick(TObject *Sender)
    {
        double smallRadius, largeRadius, circumference, area;
    
        try {
            smallRadius = this->edtEllipseSRadius->Text.ToDouble();
            largeRadius = this->edtEllipseLRadius->Text.ToDouble();
            circumference = (smallRadius + largeRadius) * myPI;
            area = smallRadius * largeRadius * myPI;
    
            this->edtEllipseCircumf->Text = FloatToStr(circumference);
            this->edtEllipseArea->Text    = FloatToStr(area);
        }
        catch(EConvertError *err)
        {
            ShowMessage("Invalid Value(s): Please try again."); 
            this->edtEllipseCircumf->Text = "0.00";
            this->edtEllipseArea->Text    = "0.00";
        }
    }
    //---------------------------------------------------------------------------
  11. Return to the form and click the 3-Dimensional tab
  12. Double-click the top Calculate button and implement its event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::edtCalcCubeClick(TObject *Sender)
    {
        double side, area, volume;
    
        try {
            side   = this->edtCubeSide->Text.ToDouble();
            area   = side * side * 6;
            volume = side * side * side;
    
            this->edtCubeArea->Text = FloatToStr(area);
            this->edtCubeVolume->Text = FloatToStr(volume);
        }
        catch(EConvertError *err)
        {
            ShowMessage("The value you provided for the Side is invalid.");
            this->edtCubeSide->Text   = "0.00";
            this->edtCubeArea->Text   = "0.00";
            this->edtCubeVolume->Text = "0.00";
        }
    }
    //---------------------------------------------------------------------------
  13. Return to the 3-Dimensional property page and double-click the bottom Calculate button
  14. Implement its event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::btnCalcBoxClick(TObject *Sender)
    {
        double base, height, width, area, volume;
    
        try {
            base   = this->edtBoxBase->Text.ToDouble();
            height = this->edtBoxHeight->Text.ToDouble();
            width  = this->edtBoxWidth->Text.ToDouble();
            area   = 2 * ((base*height) + (height*width) + (base*width));
            volume = base * height * width;
    
            this->edtBoxArea->Text   = FloatToStr(area);
            this->edtBoxVolume->Text = FloatToStr(volume);
        }
        catch(EConvertError *err)
        {
            ShowMessage("Invalid Value(s): Please try again."); 
            this->edtBoxArea->Text   = "0.00";
            this->edtBoxVolume->Text = "0.00";
        }
    }
    //---------------------------------------------------------------------------
  15. Execute the application and test each geometric figure
  16. Close the form and return to your programming environment
 
Home Copyright 2005-2007 FunctionX, Inc.