Getting Started With C#

This example is to get you started using C# in Visual Studio.NET.  It is intended to be used as your first program to test your Visual Studio .NET installation, and to start finding your way around in VS.  I have left out many of the steps, assuming that you have used previous generation tools.  The screen shots are given, to help you find your way around the screen.  I have given instructions for changing names which the wizard has given you.  This is not to convince you to use my naming conventions, but to convince you not to use what the wizard gives you.

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

Click the Form in the Form Designer to select it.  In the Properties Window, change the text property to Calculator.  This will change the name in the title bar of the Form.

In the Solution Explorer, right click on Form1.cs and rename it as FCalculator.cs.  Also go through the source code and change all instances of Form1 to FCalculator.

Add 3 TextBox and 4 Button Controls to the Form.  Use the Format menu to arrange the controls on the Form.  For the 3 TextBox controls, clear the Text property, and change the (Name) properties to textBoxOp1, textBoxOp2, and textBoxAns.  Change the text on the buttons to "+, -, *, & /".  Change the (Name)s to "AddButton, SubtractButton, MultiplyButton, and DivideButton.

Double click each button to create its event handler, and add the code.

private void AddButton_Click(object sender, System.EventArgs e)
{
   double dAns;
   dAns = double.Parse( textBoxOp1.Text ) + double.Parse( textBoxOp2.Text );
   textBoxAns.Text = dAns.ToString();
}

private void SubtractButton_Click(object sender, System.EventArgs e)
{
   double dAns;
   dAns = double.Parse( textBoxOp1.Text ) - double.Parse( textBoxOp2.Text );
   textBoxAns.Text = dAns.ToString();
}

private void MultiplyButton_Click(object sender, System.EventArgs e)
{
   double dAns;
   dAns = double.Parse( textBoxOp1.Text ) * double.Parse( textBoxOp2.Text );
   textBoxAns.Text = dAns.ToString();
}

private void DivideButton_Click(object sender, System.EventArgs e)
{
   double dAns;
   dAns = double.Parse( textBoxOp1.Text ) / double.Parse( textBoxOp2.Text );
   textBoxAns.Text = dAns.ToString();
}

You might also like...

Comments

Bill Burris

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++: an octopus made by nailing extra legs onto a dog.” - Steve Taylor