Thursday, August 27, 2009

Windows Programming - Simple Addition or Subtraction of Two Numbers

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
Button b1, b2;
public myfrm()
{

t1 = new TextBox();
t1.SetBounds(100, 100, 150, 50);
t2 = new TextBox();
t2.SetBounds(100, 200, 150, 50);
this.Controls.Add(t2);
t3 = new TextBox();
t3.SetBounds(100, 300, 150, 50);


b1 = new Button();
b1.Text = "Add";
b1.SetBounds(100, 100, 150, 50);

b2 = new Button();
b2.Text = "Sub";
b2.SetBounds(100, 300, 150, 50);

this.Controls.Add(t1);
this.Controls.Add(t2);
this.Controls.Add(t3);
this.Controls.Add(b1);
this.Controls.Add(b2);

b1.Click +=new EventHandler(fun);
b2.Click +=new EventHandler(fun);
}

void fun(object o, EventArgs e)
{
double a,b,c;
a = Convert.ToDouble(t1.Text);
b = Convert.ToDouble(t2.Text);


if (o.Equals(b1))
{
c = a + b;
}
if (o.Equals(b2))
{
c = a - b;
}

t3.Text = c.ToString();

}

}

No comments:

Post a Comment