This documentation is archived and is not being maintained.

Calculator Sample: Windows Forms Pocket Calculator

The Calculator sample implements a simple pocket calculator using C++ and the .NET Framework Windows Forms classes. It demonstrates using .NET Framework classes to write a user-interface without the support of the resource editors, and illustrates the following concepts:

  • Working with forms, menus, edit fields, buttons, and icons for forms

  • Manipulating managed arrays

  • Using delegates/event handlers

  • Exception handling

  • Using enums

  • Conversions between the .NET Framework String, Double, and Char classes

Security noteSecurity Note:

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To get samples and instructions for installing them:

  • On the Visual Studio Help menu, click Samples.

    For more information, see Visual Studio Samples.

  • The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.

To build and run Calculator using Visual Studio

  1. In the Visual Studio IDE, load the solution file calc.sln.

  2. In Solution Explorer, right-click the Calc solution.

  3. On the shortcut menu, click Build.


    Since this program does not compile the icon file (calc.ico) into a XML-based resource file (*.resx), you must copy the icon file into the same directory as the .exe (built from the sample) before continuing.

  4. Run the resulting application and try out various operations with the calculator.

This sample demonstrates the following classes:

Form; TextBox; Button; Icon; MainMenu; MenuItem; MessageBox; Exception; FileNotFoundException; Size structure; Font

This sample demonstrates the following keywords:

__gc; __value; enum; add_KeyPress; KeyPressEventHandler; EventHandler; __try_cast; EventArgs; KeyPressEventArgs; FormStartPosition; FormBorderStyle; SizeGripStyle; BorderStyle; SystemColors; HorizontalAlignment; FlatStyle; add_Click