Components Notebook
Home

Notebooks
C#
C++
Agile Hacker
Hardware
Photos

Book Reviews

Your First C# Windows Application

This example is to get you started creating Windows Forms applications in C# using Visual Studio.NET.

Create a new C#, Windows Application and call it Calculator.

Select the Form in the Form Designer. In the Properties Window, change the Text property to Calculator and the (name) property to MainForm. This will change the name in the title bar of the Form, and the name of the Form class in the code. In The Solution Explorer right click on Form1.cs and rename it to MainForm.cs.

When changing the (name) property for the Form, Visual Studio missed changing one line in the code. Right click on the Form in the Form Designer and select View Code from the popup menu. In the code locate:

static void Main() { Application.Run(new Form1()); } 

and change it to:

static void Main() { Application.Run(new MainForm()); } 

Add 3 TextBox and 4 Button Controls to the Form. Use the Format menu or Layout toolbar to arrange the controls on the Form.

If the layout toolbar as shown above, is not visible right click in a blank spot in the toolbar area. From the context menu which pops up select Layout.

For the 3 TextBox controls, clear the Text property, and change the (Name) properties to Op1TextBox, Op2TextBox, and ResultTextBox. Change the text on the buttons to "+, -, *, & /". Change the (Name)s to "AddButton, SubtractButton, MultiplyButton, and DivideButton. Select all 4 buttons then adjust the Font property in the Properties Window.

Double click each button to create its event handler, then modify the code as shown here.

private void AddButton_Click(object sender, System.EventArgs e)
{
   double dAns;
   dAns = double.Parse( Op1TextBox.Text ) + double.Parse( Op2TextBox.Text );
   ResultTextBox.Text = dAns.ToString();
}

private void SubtractButton_Click(object sender, System.EventArgs e)
{
   double dAns;
   dAns = double.Parse( Op1TextBox.Text ) - double.Parse( Op2TextBox.Text );
   ResultTextBox.Text = dAns.ToString();
}

private void MultiplyButton_Click(object sender, System.EventArgs e)
{
   double dAns;
   dAns = double.Parse( Op1TextBox.Text ) * double.Parse( Op2TextBox.Text );
   ResultTextBox.Text = dAns.ToString();
}

private void DivideButton_Click(object sender, System.EventArgs e)
{
   double dAns;
   dAns = double.Parse( Op1TextBox.Text ) / double.Parse( Op2TextBox.Text );
   ResultTextBox.Text = dAns.ToString();
} 

Run the application and type values into the two operand text boxes. Click the four function button to test the application. This program has no error checking so if you give it invalid input it will crash.

Add some simple error handling:

private void AddButton_Click(object sender, System.EventArgs e)
{
   double dAns;
   try
   {
      dAns = double.Parse( Op1TextBox.Text ) + double.Parse( Op2TextBox.Text );
      ResultTextBox.Text = dAns.ToString();
   }
   catch( Exception ex )
   {
      MessageBox.Show( ex.Message );
   }
}

private void SubtractButton_Click(object sender, System.EventArgs e)
{
   double dAns;
   try
   {
      dAns = double.Parse( Op1TextBox.Text ) - double.Parse( Op2TextBox.Text );
      ResultTextBox.Text = dAns.ToString();
   }
   catch( Exception ex )
   {
      MessageBox.Show( ex.Message );
   }
}

private void MultiplyButton_Click(object sender, System.EventArgs e)
{
   double dAns;
   try
   {
      dAns = double.Parse( Op1TextBox.Text ) * double.Parse( Op2TextBox.Text );
      ResultTextBox.Text = dAns.ToString();
   }
   catch( Exception ex )
   {
      MessageBox.Show( ex.Message );
   }
}

private void DivideButton_Click(object sender, System.EventArgs e)
{
   double dAns;
   try
   {
      dAns = double.Parse( Op1TextBox.Text ) / double.Parse( Op2TextBox.Text );
      ResultTextBox.Text = dAns.ToString();
   }
   catch( Exception ex )
   {
      MessageBox.Show( ex.Message );
   }
}

C# Notebook


wburris at telusplanet dot net