Slide 8.12: Operators (cont.)
Slide 9.2: Controlling program flow
Home

Arithmetic Expressions


This program is a simple program that calculates the volume of a cylinder. The interface is designed as below. Generally, there are three basic steps in building a C# application:
    Step 1: Design the interface.
    Step 2: Set properties of the controls (tools).
    Step 3: Write the events’ procedures.

  1. First of all, go to the Properties Window and change the property Text to “Volume of Cylinder.”

  2. Then drag and insert three Labels into the form and change their text to “Base radius,” “Height,” and “Volume,” respectively.

  3. After that, insert three TextBoxes. Named the text boxes as radius, height, and volume, respectively.

  4. Lastly, insert a command Button and change its text to “Calculate” and its name to calculate.

  5. Double click the button and enter the following code:

     using System;
     using System.Windows.Forms;
     namespace WindowsFormsApp3 {
       public partial class Form1 : Form {
         public Form1( ) { InitializeComponent( ); }
    
         private void Calculate_Click( object sender, EventArgs e ) {
           double result = int.Parse( radius.Text ) * int.Parse( radius.Text )
               * 3.14159 * int.Parse( height.Text );
           volume.Text = result.ToString( );
         }
       } 
     }

    where the int.Parse function converts a string to an interger, and the ToString performs the opposite function.