0

A simple C# program showing you how easy it is to create a basic Windows GUI program which will display the results of a factorial number calculator.

// calculating factorials using recursion
// this is a Windows GUI program with a button and label
// compiled with C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe

using System;
using System.Windows.Forms;  // GUI stuff

namespace FactorialSpace
{
   public class FactorialTest : System.Windows.Forms.Form
   {
      // required designer variables
      private System.ComponentModel.Container components = null;
      private System.Windows.Forms.Button factButton;
      private System.Windows.Forms.Label resultLabel;

      public FactorialTest()
      {
         // needed for the Windows GUI
         InitializeComponent();
      }

      // clean up any resources being used
      protected override void Dispose( bool disposing )
      {
         if ( disposing )
         {
            if (components != null) 
            {
               components.Dispose();
            }
         }
         base.Dispose( disposing );
      }

      // these are the GUI components
      private void InitializeComponent()
      {
         this.resultLabel = new System.Windows.Forms.Label();
         this.factButton = new System.Windows.Forms.Button();
         this.SuspendLayout();
         // 
         // resultLabel
         // 
         this.resultLabel.Location = new System.Drawing.Point(10, 50);
         this.resultLabel.Name = "resultLabel";
         this.resultLabel.Size = new System.Drawing.Size(200, 230);
         this.resultLabel.TabIndex = 0;
         // 
         // factButton
         // 
         this.factButton.Location = new System.Drawing.Point(70, 10);
         this.factButton.Name = "factButton";
         this.factButton.Size = new System.Drawing.Size(100, 25);
         this.factButton.TabIndex = 1;
         this.factButton.Text = "Calc factorials";
         this.factButton.Click += new System.EventHandler(this.factButton_Click);
         // 
         // basic Window Frame
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
         this.ClientSize = new System.Drawing.Size(240, 250);
         this.Controls.AddRange(new System.Windows.Forms.Control[] {
              this.factButton, this.resultLabel});
         this.BackColor = System.Drawing.Color.LightCyan;  // optional
         this.Name = "factorialTest";
         this.Text = "Factorial Calculator";
         this.ResumeLayout(false);
      }

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

      public long Factorial( long number )
      {
         if ( number <= 1 )
            return 1;
         else
            return number * Factorial( number - 1 );
      }

      private void factButton_Click(object sender, System.EventArgs ev)
      {
         resultLabel.Text = "";
         // 13! is max for C#
         for ( long i = 0; i <= 13; i++ )
            resultLabel.Text += i + "! = " + Factorial( i ) + "\n";
      }
   }
}
1
Contributor
2
Replies
4
Views
10 Years
Discussion Span
Last Post by Lardmeister
0

To compile the program, copy and paste the code into an editor, and save it for instance as "factorial.cs". Then find the C# compiler "csc.exe" in the "C:\Windows\Microsoft.NET\Framework\ ..." folder and run the command line (you might have to specify the folders too):
csc.exe factorial.cs
That should produce a file "factorial.exe". The nice thing about this approach is that you have one clean code file and one clean small executable file.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.