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.
Factorial Numbers
// 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";
}
}
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.