using System;
using System.Drawing;
using System.Windows.Forms;
public class CalculatorForm : Form
{
private TextBox TextBoxNumber1;
private TextBox TextBoxNumber2;
private TextBox TextBoxSum;
private Button ButtonCalc;
private Label LabelPlus;
private Label LabelEquals;
public CalculatorForm()
{
TextBoxNumber1 = new TextBox();
TextBoxNumber1.Bounds = new Rectangle(
new Point(16, 16), new Size(72, 20));
TextBoxNumber1.MaxLength = 10;
LabelPlus = new Label();
LabelPlus.AutoSize = true;
LabelPlus.Location = new Point(94, 19);
LabelPlus.Text = "+";
TextBoxNumber2 = new TextBox();
TextBoxNumber2.Bounds = new Rectangle(
new Point(112, 16), new Size(72, 20));
TextBoxNumber2.MaxLength = 10;
LabelEquals = new Label();
LabelEquals.AutoSize = true;
LabelEquals.Location = new Point(191, 18);
LabelEquals.Text = "=";
TextBoxSum = new TextBox();
TextBoxSum.Bounds = new Rectangle(
new Point(208, 16), new Size(72, 20));
TextBoxSum.ReadOnly = true;
ButtonCalc = new Button();
ButtonCalc.Bounds = new Rectangle(
new Point(16, 48), new Size(264, 23));
ButtonCalc.Text = "Calculate sum";
ButtonCalc.Click += new EventHandler(
this.ButtonCalc_Click);
this.AcceptButton = ButtonCalc;
this.ClientSize = new Size(298, 87);
this.Controls.Add(TextBoxNumber1);
this.Controls.Add(LabelPlus);
this.Controls.Add(TextBoxNumber2);
this.Controls.Add(LabelEquals);
this.Controls.Add(TextBoxSum);
this.Controls.Add(ButtonCalc);
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Text = "Calculator";
}
private void ButtonCalc_Click(object aSender, EventArgs aArgs)
{
try
{
int value1 = Int32.Parse(TextBoxNumber1.Text);
int value2 = Int32.Parse(TextBoxNumber2.Text);
int sum = value1 + value2;
TextBoxSum.Text = sum.ToString();
}
catch (FormatException)
{
TextBoxSum.Text = "Invalid!";
}
TextBoxNumber1.SelectAll();
TextBoxNumber2.SelectAll();
TextBoxNumber1.Focus();
}
static void Main()
{
CalculatorForm CalcForm = new CalculatorForm();
Application.Run(CalcForm);
}
}
|