Library code snippets

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();
}

Comments

  1. 29 Jan 2005 at 05:07

    This toping is very interesting and informative for beginners


    Really it is very much helpfull to me
    Thanking you,
    Expecting similar classes and bit higher version


    S.Krishna
    kri_it2003@yahoo.com

  2. 29 Jan 2005 at 05:06

    This toping is very interesting and informative for beginners


    Really it is very much helpfull to me
    Thanking you,
    Expecting similar classes and bit higher version


    S.Krishna
    kri_it2003@yahoo.com

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Getting Started With C#.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related podcasts

  • Looking into the C# Crystal Ball with Charlie Calvert and Bill Wagner

    One of the most exciting announcements from PDC was the news about C# 4.0 and Visual Studio 2010. With all the excitement and discussion throughout the event about these new developer tools, we reached out to two experts in the fields. Charlie Calvert and Bill Wagner sat down with Keith and Woody...

Events coming up

  • Dec 6

    Developing AJAX Web Applications with Castle Monorail

    London, United Kingdom

    Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!