Example with Textbox Label,Button:-



Add a new Form i.e.,FormArithematic to the Windows Application WABasics
Create three labels,three textboxes and four buttons & set the following properties to them

label1:
     Text    : Enter First Number:
     Name  : lblNum1


label2:
     Text  : Enter Second Number:
     Name: lblNum2


label3:
     Text   : Result:
     Name : lblResult



textbox1:
     Name: txtNum1



textbox2:
     Name: txtNum2



textbox3:
     Name : txtResult
     ReadOnly: True


button1:
     Name: btnAdd
     Text: +

button2:
     Name: btnSubtract
     Text: -

button3:
     Name : btnMultiply
     Text    : *

button4:
     Name  : btnDivide
     Text    : /


Design :-

Generate the functions for btnAdd ,btnSubtract,btnMultiply,btnDevide Click Events
& write the following code in Source[FormArithematic.cs] 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WABasics
{
    public partial class FormArithematic : Form
    {
        int a, b, c;
        public FormArithematic()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            a = Convert.ToInt32(txtNum1.Text);
            b = Convert.ToInt32(txtNum2.Text);
            c = a + b;
            txtResult.Text = c.ToString();
        }

        private void btnSubstract_Click(object sender, EventArgs e)
        {
            a = Convert.ToInt32(txtNum1.Text);
            b = Convert.ToInt32(txtNum2.Text);
            c = a - b;
            txtResult.Text = c.ToString();
        }

        private void btnMultiply_Click(object sender, EventArgs e)
        {
            a = Convert.ToInt32(txtNum1.Text);
            b = Convert.ToInt32(txtNum2.Text);
            c = a * b;
            txtResult.Text = c.ToString();
        }

        private void Divide_Click(object sender, EventArgs e)
        {
            a = Convert.ToInt32(txtNum1.Text);
            b=Convert.ToInt32(txtNum2.Text);
            if (b == 0)
                MessageBox.Show("Second Number Cannot be Zero");
            else
            {
                c = a / b;
                txtResult.Text = c.ToString();
            }

        }
     
    }
}




Result :-

No comments:

Post a Comment