Home

VCL Examples: Operations

 
 

Introduction

This fundamental application illustrates the use of radio buttons to perform simple arithmetic operations such as the addition, the subtraction, the multiplication and the division.

Practical Learning Practical Learning: Creating Radio Buttons

 

  1. Start a new project with the default form
  2. To save it, on the Standard toolbar, click the Save All button
  3. Click the Create New Folder button. Type Operations and press Enter twice
  4. Click Unit1 to select it. Type Main and press Enter
  5. Type Operations as the name of the project and press Enter
  6. On the Standard tab of the Component Palette, click the Panel control
  7. On the form, click and drag from the top left section to the middle center section
  8. On the Object Inspector, click Caption and press Delete to delete the caption of the panel
  9. On the Component Palette, double-click the RadioButton control
  10. On the Object Inspector, click Caption and type &Addition
  11. Click Name and type rdoAddition
  12. Move the new radio button to the top section of and inside the panel
  13. On the Component Palette, click the Radio Button
  14. Click inside the panel
  15. Click Name and type rdoSubtraction
  16. Click Caption and type &Subtraction
  17. In the same way, add another RadioButton control to the panel. Set its Caption to &Multiplication and its Name to rdoMultiplication
  18. Add one more RadioButton to the panel
  19. Set its Name to rdoDivision and its Caption to &Division
     
  20. Move the panel to the right section of the form
  21. Notice that it moves with its “children”. You will redesign the form as follows:
     
  22. Add another panel with four labels captioned
    Number &1:
    Number &2:
    ------------------
    Result:
  23. Add three Edit controls inside the panel and on the right side of the corresponding labels. These edit boxes will be named edtNumber1, edtNumber2, and edtResult
  24. Add another panel to an empty area of the form
  25. While the new and last panel is selected, click the Align field. Click its arrow and select alBottom
  26. On the form double-click the Addition radio button to access its OnClick event
  27. Implement it as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmOperations::rdoAdditionClick(TObject *Sender)
    {
    	double Num1;
    	double Num2;
    	double Result;
    
    	if( edtNumber1->Text == "" )
    		Num1 = 0;
    	else
    		Num1 = StrToFloat(edtNumber1->Text);
    
    	if( edtNumber2->Text == "" )
    		Num2 = 0;
    	else
    		Num2 = StrToFloat(edtNumber2->Text);
    
    	Result = Num1 + Num2;
    	edtResult->Text = Result;
    }
    //---------------------------------------------------------------------------
  28. Click the arrow on the top section of the Object Inspector and select rdoSubtraction
  29. Click the Events tab
  30. Double-click the empty box on the right side of OnClick
  31. Implement the function just like the Click event of the addition but change the operation as follows:
    Result = Num1 - Num2;
  32. On the top combo box of the Object Inspector, select rdoMultiplication
  33. Double-click the box of the OnClick field
  34. Implement the Click event like the previous two but change the operation as follows:
    Result = Num1 * Num2;
  35. In the same way, access the OnClick event for the rdoDivision control and implement it as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmOperations::rdoDivisionClick(TObject *Sender)
    {
    	double Num1;
    	double Num2;
    	double Result;
    
    	if( edtNumber1->Text == "" )
    		Num1 = 0;
    	else
    		Num1 = StrToFloat(edtNumber1->Text);
    
    	if( edtNumber2->Text == "" )
    	{
    		MessageBox(NULL, "The Number 2 Edit Box should not be empty",
    				"Algebraic Operations", MB_OK);
    		edtNumber2->SetFocus();
    	}
    	else if( edtNumber2->Text == 0 )
    	{
    		MessageBox(0, "Cannot divide by zero",
    				"Algebraic Operations", MB_OK);
    		edtNumber2->SetFocus();
    	}
    	else
    	{
    		Num2 = StrToFloat(edtNumber2->Text);
    		Result = Num1 / Num2;
    		edtResult->Text = Result;
    	}
    }
    //---------------------------------------------------------------------------
  36. To display the form, press F12
  37. Double-click the panel on the bottom section of the form to access its OnClick event and implement it as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmOperations::Panel3Click(Tobject *Sender)
    {
    	Close();
    }
    //---------------------------------------------------------------------------
  38. To test the form, press F9
  39. After using it, close the form and save your project

 

Home Copyright © 2005-2007 FunctionX, Inc.