This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

5 Dec 2012

SIMPLE EXAMPLE TO START WITH C# WINDOWS APPLICATION


Example For Arithmetic Operations in C#.Net Windows


In previous posts, we have seen c# program to add two numbers / integersc# program to add two numbers / integers using method

In this post we will see a simple example to start with windows c# application.

Here we will perform basic Arithmetic operations by allowing user to provide input values through textbox.

There are four buttons on form to perform operations on its respective click event and display result in Result textbox provided on form.

I am designing the form as in the image below



After this, the code in the code behind file is as follows..

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                 int a= Convert.ToInt32(txtValue1.Text);
                 int b = Convert.ToInt32(txtValue2.Text);
                 txtResult.Text = (a + b).ToString();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSubtract_Click(object sender, EventArgs e)
        {
            try
            {
                int a = Convert.ToInt32(txtValue1.Text);
                int b = Convert.ToInt32(txtValue2.Text);
                txtResult.Text = (a - b).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnMultiply_Click(object sender, EventArgs e)
        {
            try
            {
                int a = Convert.ToInt32(txtValue1.Text);
                int b = Convert.ToInt32(txtValue2.Text);
                txtResult.Text = (a * b).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDivide_Click(object sender, EventArgs e)
        {
            try
            {
                int a = Convert.ToInt32(txtValue1.Text);
                int b = Convert.ToInt32(txtValue2.Text);
                txtResult.Text = (a / b).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

Look at the image below for output:



Download the sample code: WindowsApplication1.rar


No comments:

Post a Comment

Thanks for your comments.
-Sameer