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 / integers , c# program to add two numbers / integers using function
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
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: