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).

Bill Burris
AddThis

Related discussion

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

Events coming up

  • Aug 28

    St. Louis Day of .NET

    St. Charles, United States

    Technical conference with be 2 full days of content with over 40 sessions from local and national speakers, with topics such as:•.NET languages: C#, VB.NET•Technologies: WPF, Silverlight, WCF•Development tools: Visual Studio, TFS, Expression Blend

Want to stay in touch with what's going on? Follow us on twitter!