Click here to Skip to main content
15,886,724 members
Articles / Programming Languages / C#

A Calculator Control Box

Rate me:
Please Sign up or sign in to vote.
4.22/5 (36 votes)
25 Nov 2005CPOL2 min read 143.8K   5.5K   86  
A Calculator Control Box which can be used instead of a text box for numeric input.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace CalculatorBoxTest
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label lblValue;
		private System.Windows.Forms.Label lblText;
		private System.Windows.Forms.Label label4;
		private Calculator.CalculatorBox calculatorBox1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

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

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.lblValue = new System.Windows.Forms.Label();
			this.lblText = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.calculatorBox1 = new Calculator.CalculatorBox();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(40, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(45, 16);
			this.label1.TabIndex = 1;
			this.label1.Text = "Amount";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(208, 32);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(40, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "Value";
			// 
			// lblValue
			// 
			this.lblValue.Location = new System.Drawing.Point(264, 32);
			this.lblValue.Name = "lblValue";
			this.lblValue.Size = new System.Drawing.Size(100, 16);
			this.lblValue.TabIndex = 0;
			this.lblValue.Text = "lblValue";
			// 
			// lblText
			// 
			this.lblText.Location = new System.Drawing.Point(264, 64);
			this.lblText.Name = "lblText";
			this.lblText.Size = new System.Drawing.Size(100, 16);
			this.lblText.TabIndex = 2;
			this.lblText.Text = "lblText";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(208, 64);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(40, 16);
			this.label4.TabIndex = 3;
			this.label4.Text = "Text";
			// 
			// calculatorBox1
			// 
			this.calculatorBox1.Location = new System.Drawing.Point(88, 32);
			this.calculatorBox1.Name = "calculatorBox1";
			this.calculatorBox1.Size = new System.Drawing.Size(104, 20);
			this.calculatorBox1.TabIndex = 4;
			this.calculatorBox1.Value = 0;
			this.calculatorBox1.ErrorOccured += new Calculator.CalculatorBox.ErrorEvent(this.calculatorBox1_ErrorOccured);
			this.calculatorBox1.ValueChanged += new Calculator.CalculatorBox.ValueChangedEvent(this.calculatorBox1_ValueChanged);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(416, 94);
			this.Controls.Add(this.calculatorBox1);
			this.Controls.Add(this.lblText);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.lblValue);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Name = "Form1";
			this.Text = "Calculator Box";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void calculatorBox1_ValueChanged(object sender, double value)
		{
			lblValue.Text=this.calculatorBox1.Value.ToString();
			lblText.Text=this.calculatorBox1.Text;
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			lblValue.Text=this.calculatorBox1.Value.ToString();
			lblText.Text=this.calculatorBox1.Text;
		}

		private void calculatorBox1_ErrorOccured(object sender, string Message)
		{
			lblValue.Text=this.calculatorBox1.Value.ToString();
			lblText.Text=this.calculatorBox1.Text;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) KAZ Software Limited
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions