KEMBAR78
All Window Program | PDF | Computing | Software
0% found this document useful (0 votes)
46 views33 pages

All Window Program

The document contains multiple examples of C# Windows Forms applications, demonstrating various controls such as MessageBox, ContextMenuStrip, RadioButton, CheckBox, ListBox, ComboBox, Dialog, ProgressBar, ScrollBar, and Timer. Each example includes code snippets that illustrate how to implement these controls and their functionalities. The document is prepared by Bhartiba Parmar from Swami Vivekanand College.

Uploaded by

A Azher Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views33 pages

All Window Program

The document contains multiple examples of C# Windows Forms applications, demonstrating various controls such as MessageBox, ContextMenuStrip, RadioButton, CheckBox, ListBox, ComboBox, Dialog, ProgressBar, ScrollBar, and Timer. Each example includes code snippets that illustrate how to implement these controls and their functionalities. The document is prepared by Bhartiba Parmar from Swami Vivekanand College.

Uploaded by

A Azher Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

1.

Messagebox example

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

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

private void btnclick_Click(object sender, EventArgs e)


{
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
MessageBox.Show("welcome", "project", MessageBoxButtons.RetryCancel,
MessageBoxIcon.Question);
}
}

2.ContextMenuStrip example

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

namespace contextmenuexample
{
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

private void oddEvenToolStripMenuItem_Click(object sender, EventArgs e)


{
int no;
no = int.Parse(textBox1.Text);
if(no%2==0)
{
textBox2.Text = "No Is EVEN";
}
else
{
textBox2.Text = "No Is odd";
}

private void positiveNegativeToolStripMenuItem_Click(object sender, EventArgs e)


{
int no;
no = int.Parse(textBox1.Text);
if (no > 0)
{
textBox2.Text = "No Is posive";
}
else
{
textBox2.Text = "No Is negative";
}

}
}
}

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
3.Addition Substraction Multiplication Division
example.

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

namespace Additionex
{
public partial class Form1 : Form
{
public Form1()
{
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)


{
int no1, no2;

no1 = Convert.ToInt16(txtno1.Text);
no2 = Convert.ToInt16(txtno2.Text);

txtans.Text = (no1 * no2).ToString();

private void button3_Click(object sender, EventArgs e)


{
int no1, no2;

no1 = Convert.ToInt16(txtno1.Text);
no2 = Convert.ToInt16(txtno2.Text);

txtans.Text = (no1 / no2).ToString();

private void btnadd_Click(object sender, EventArgs e)


{
int no1, no2;

no1 = Convert.ToInt16(txtno1.Text);
no2 = Convert.ToInt16(txtno2.Text);

txtans.Text = (no1 + no2).ToString();

private void button4_Click(object sender, EventArgs e)


{
int no1, no2;

no1 = Convert.ToInt16(txtno1.Text);
no2 = Convert.ToInt16(txtno2.Text);

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
txtans.Text = (no1 - no2).ToString();

private void button5_Click(object sender, EventArgs e)


{
txtno1.Text = "";
txtno2.Text = "";
txtans.Text = "";
txtno1.Focus();
}
}
}

4. RadioButton example.

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

private void radioButton1_CheckedChanged(object sender, EventArgs e)


{
pictureBox1.Image = Image.FromFile("C:\\Users\\Lenovo\\Pictures\\Saved
Pictures\\p1.jpg");

private void radioButton2_CheckedChanged(object sender, EventArgs e)


{
pictureBox1.Image = Image.FromFile("C:\\Users\\Lenovo\\Pictures\\Saved
Pictures\\p2.jpg");
}

private void button1_Click(object sender, EventArgs e)


{
string name, gen;
name = textBox1.Text;
if(radioButton1.Checked== true)
{
gen = "Male";

}
else
{

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
gen = "Female";
}
MessageBox.Show("name="+ name+"gender="+gen);
}
}
}

5. CheckBox example.

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

namespace checkboxex
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
if(radioButton1.Checked==true)
{
if(checkBox1.Checked==true)
{
label3.Text = "50";
}
else if(checkBox2.Checked == true)
{
label3.Text = "30";
}
else if (checkBox3.Checked == true)
{
label3.Text = "15";
}
}
if (radioButton2.Checked == true)
{
if (checkBox1.Checked == true)
{
label3.Text = "35";
}
else if (checkBox2.Checked == true)
{
label3.Text = "85";
}
else if (checkBox3.Checked == true)
{
label3.Text = "10";
}
}
}
}
}

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
6. ListBox example.

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

namespace listboxexample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
private void btnclear_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}

private void Form1_Load(object sender, EventArgs e)


{
listBox1.Items.Add("Hello");
listBox1.Items.Add("How");
listBox1.Items.Add("are");
listBox1.Items.Add("you");
listBox1.Items.Add("sv");
listBox1.Items.Add("students");
}

private void btnRemoveAt_Click(object sender, EventArgs e)


{
int pos = Convert.ToInt16(textBox4.Text);
listBox1.Items.RemoveAt(pos);
}

private void btnremove_Click(object sender, EventArgs e)


{
listBox1.Items.Remove(textBox3.Text);
}

private void btncontains_Click(object sender, EventArgs e)


{
string str = textBox1.Text;
if(listBox1.Items.Contains(str)==true)
{
MessageBox.Show("YES IN THE LIST");
}
else
{
MessageBox.Show("No IN THE LIST");
}
}

private void btnindex_Click(object sender, EventArgs e)


{
string str = textBox2.Text;

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
int pos = listBox1.Items.IndexOf(str);
if (pos>-1)
{
MessageBox.Show("POS+"+pos);
}
else
{
MessageBox.Show("Not FOUND POS =" + pos);
}
}
}
}

7. Example Of combobox Control.

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

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
namespace comboboxex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void comboBox1_SelectedIndexChanged(object sender,


EventArgs e)
{
int a, b, c;
a = Convert.ToInt16(textBox1.Text);
b = Convert.ToInt16(textBox2.Text);
if(comboBox1.Text=="+")
{
c = a + b;
textBox3.Text = Convert.ToString(c);
}
else if (comboBox1.Text == "-")
{
c = a - b;
textBox3.Text = Convert.ToString(c);
}
else if (comboBox1.Text == "*")
{
c = a * b;
textBox3.Text = Convert.ToString(c);
}
else if (comboBox1.Text == "/")
{
c = a / b;
textBox3.Text = Convert.ToString(c);
}

}
}
}

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
8. Example Of Dialog Control.

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

namespace dialogcontrolex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
private void button1_Click(object sender, EventArgs e)
{
if(colorDialog1.ShowDialog()==DialogResult.OK)
{
richTextBox1.BackColor = colorDialog1.Color;
}
}

private void button2_Click(object sender, EventArgs e)


{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.ForeColor = colorDialog1.Color;
}
}

private void button3_Click(object sender, EventArgs e)


{

if (fontDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.ForeColor = fontDialog1.Color;
richTextBox1.Font = fontDialog1.Font;
}
}

private void button4_Click(object sender, EventArgs e)


{
openFileDialog1.Filter = "Text Files| *.txt|word
file|*.doc";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.LoadFile(openFileDialog1.FileName,
RichTextBoxStreamType.PlainText);
this.Text = openFileDialog1.FileName;
}
}

private void button5_Click(object sender, EventArgs e)


{
SaveFileDialog savedialog = new SaveFileDialog();
savedialog.Title = "save";
saveFileDialog1.Filter = "Text Files( *.txt)|*.txt" + "|"
+ "Image Files(*.png;*.jpg)| *.png;*.jpg"+"|"+"All Files(*.*)|*.*";

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
if(savedialog.ShowDialog()==DialogResult.OK)
{
string file = savedialog.FileName;
}
/*if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.LoadFile(saveFileDialog1.FileName,
RichTextBoxStreamType.PlainText);
this.Text = saveFileDialog1.FileName;
}*/
}
}
}

9. Example Of listbox Control.

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

private void btnRmov1_Click(object sender, EventArgs e)


{
listBox2.Items.Add(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);

private void btnRselected_Click(object sender, EventArgs e)


{
for(int i=0;i<=listBox1.SelectedItems.Count;i++)
{
listBox2.Items.Add(listBox1.SelectedItems[i]);
listBox1.Items.Remove(listBox1.SelectedItems[i]);
}
}

private void btnRall_Click(object sender, EventArgs e)


{
for (int i = 0; i <= listBox1.Items.Count-1; i++)
{
listBox2.Items.Add(listBox1.Items[i]);

}
listBox1.Items.Clear();

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
private void btnmov1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(listBox2.SelectedItem);
listBox2.Items.Remove(listBox2.SelectedItem);
}

private void btnselected_Click(object sender, EventArgs e)


{
for (int i = 0; i <= listBox2.SelectedItems.Count; i++)
{
listBox1.Items.Add(listBox2.SelectedItems[0]);
listBox2.Items.Remove(listBox2.SelectedItems[0]);
}
}

private void btnall_Click(object sender, EventArgs e)


{
for (int i = 0; i <= listBox2.Items.Count - 1; i++)
{
listBox1.Items.Add(listBox2.Items[i]);

}
listBox2.Items.Clear();

}
}
}
10. Example Of Progressbar Control.

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

private void Form1_Load(object sender, EventArgs e)


{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
}

private void timer1_Tick(object sender, EventArgs e)


{
if (progressBar1.Value < 100)
{
progressBar1.Value = progressBar1.Value + 5;
}
}
}
}

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
11. Example Of scrollbar Control.

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

namespace scrolbarex
{
public partial class Form1 : Form
{
public Form1()
{
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
InitializeComponent();
}

void changecolor()
{
this.BackColor = Color.FromArgb(hScrollBar1.Value,
hScrollBar2.Value, hScrollBar3.Value);
}

private void Form1_Load(object sender, EventArgs e)


{

private void hScrollBar1_Scroll(object sender, ScrollEventArgs


e)
{
changecolor();
}

private void hScrollBar2_Scroll(object sender, ScrollEventArgs


e)
{
changecolor();
}

private void hScrollBar3_Scroll(object sender, ScrollEventArgs


e)
{
changecolor();
}
}
}

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
12. Example Of Timer Control.

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

namespace timerex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int flag = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if(flag==0)
{
pictureBox1.Visible = true;
pictureBox2.Visible = false;
pictureBox3.Visible = false;

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
flag = 1;
}
else if(flag==1)
{
pictureBox1.Visible = false;
pictureBox2.Visible = true;
pictureBox3.Visible = false;

flag = 2;
}

else
{
pictureBox1.Visible = false;
pictureBox2.Visible = false;
pictureBox3.Visible = true;

flag = 0;
}

}
}
}
13. Example Of Tooltip Control.

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

private void Form1_Load(object sender, EventArgs e)


{
toolTip1.SetToolTip(textBox1, "hello");
}
}
}

14. Example Of Treeview Control.

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

private void btnaddroot_Click(object sender, EventArgs e)


{
treeView1.Nodes.Add(textBox1.Text);
}

private void btnchild_Click(object sender, EventArgs e)


{
treeView1.SelectedNode.Nodes.Add(textBox1.Text);
}

private void btnclear_Click(object sender, EventArgs e)


{
treeView1.Nodes.Clear();
}

private void btncount_Click(object sender, EventArgs e)


{
MessageBox.Show(Convert.ToString(treeView1.Nodes.Count));
}

private void btnexpand_Click(object sender, EventArgs e)


{
treeView1.ExpandAll();
}

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
private void btncollapse_Click(object sender, EventArgs e)
{
treeView1.CollapseAll();
}

private void btnremove_Click(object sender, EventArgs e)


{
treeView1.SelectedNode.Remove();
}
}
}

15. Example Of Calculator.

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CaclulatorDemo
{
public partial class Form1 : Form
{
const string divideByZero = "Err!";
const string syntaxErr = "SYNTAX ERROR!";
bool decimalPointActive = false;
public Form1()
{
InitializeComponent();
}

private void BtnCopy_Click(object sender, EventArgs e)


{
if (string.IsNullOrWhiteSpace(txtDisplay.Text)) return;
Clipboard.SetText(txtDisplay.Text);
}

private void BtnReset_Click(object sender, EventArgs e)


{
decimalPointActive = false;
PreCheck_ButtonClick();
previousOperation = Operation.None;
txtDisplay.Clear();
}

private void BtnClear_Click(object sender, EventArgs e)


{
decimalPointActive = false;
PreCheck_ButtonClick();
if (txtDisplay.Text.Length > 0)
{
double d;

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
if
(!double.TryParse(txtDisplay.Text[txtDisplay.Text.Length -
1].ToString(), out d))
{
previousOperation = Operation.None;
}

txtDisplay.Text =
txtDisplay.Text.Remove(txtDisplay.Text.Length - 1, 1);
}
if (txtDisplay.Text.Length == 0)
{
previousOperation = Operation.None;
}
if(previousOperation != Operation.None)
{
currentOperation = previousOperation;
}
}

private void BtnDiv_Click(object sender, EventArgs e)


{
if (txtDisplay.TextLength == 0) return;
PreCheck_ButtonClick();
currentOperation = Operation.Div;
PerformCalculation(previousOperation);

previousOperation = currentOperation;
EnableOperatorButtons(false);
txtDisplay.Text += (sender as Button).Text;
}

private void BtnMul_Click(object sender, EventArgs e)


{
if (txtDisplay.TextLength == 0) return;
PreCheck_ButtonClick();
currentOperation = Operation.Mul;
PerformCalculation(previousOperation);
previousOperation = currentOperation;
EnableOperatorButtons(false);
txtDisplay.Text += (sender as Button).Text;
}

private void BtnSub_Click(object sender, EventArgs e)


{

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
if (txtDisplay.TextLength == 0 || previousOperation ==
Operation.Sub) return;
PreCheck_ButtonClick();
currentOperation = Operation.Sub;
PerformCalculation(previousOperation);

previousOperation = currentOperation;
EnableOperatorButtons(false);
txtDisplay.Text += (sender as Button).Text;
}

private void BtnAdd_Click(object sender, EventArgs e)


{
if (txtDisplay.TextLength == 0) return;
PreCheck_ButtonClick();
currentOperation = Operation.Add;
PerformCalculation(previousOperation);

previousOperation = currentOperation;
EnableOperatorButtons(false);
txtDisplay.Text += (sender as Button).Text;
}

private void PerformCalculation(Operation previousOperation)


{
try
{
if (previousOperation == Operation.None)
return;
List<double> lstNums = null;

switch (previousOperation)
{
case Operation.Add:
if (currentOperation == Operation.Sub)
{
currentOperation = Operation.Add;
return;
}
lstNums =
txtDisplay.Text.Split('+').Select(double.Parse).ToList();
txtDisplay.Text = (lstNums[0] +
lstNums[1]).ToString();
break;
case Operation.Sub:

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
int idx = txtDisplay.Text.LastIndexOf('-'); //
To handle ex: -9-2
if (idx > 0)
{
var op1 =
Convert.ToDouble(txtDisplay.Text.Substring(0, idx));
var op2 =
Convert.ToDouble(txtDisplay.Text.Substring(idx + 1));
txtDisplay.Text = (op1 - op2).ToString();
}
break;
case Operation.Mul:
if (currentOperation == Operation.Sub)
{
currentOperation = Operation.Mul;
return;
}
lstNums =
txtDisplay.Text.Split('*').Select(double.Parse).ToList();
txtDisplay.Text = (lstNums[0] *
lstNums[1]).ToString();
break;
case Operation.Div:
if (currentOperation == Operation.Sub)
{
currentOperation = Operation.Div;
return;
}
try
{
lstNums =
txtDisplay.Text.Split('/').Select(double.Parse).ToList();
if (lstNums[1] == 0)
{
throw new DivideByZeroException();
}
txtDisplay.Text = (lstNums[0] /
lstNums[1]).ToString();
}
catch (DivideByZeroException)
{
txtDisplay.Text = divideByZero;
}
break;
case Operation.None:

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
default:
break;
}
}
catch (Exception)
{
txtDisplay.Text = syntaxErr;
}
}

private void BtnNum_Click(object btn, EventArgs e)


{
if(txtDisplay.Text == syntaxErr || txtDisplay.Text ==
divideByZero)
{
txtDisplay.Text = string.Empty;
}
EnableOperatorButtons();
PreCheck_ButtonClick();
txtDisplay.Text += (btn as Button).Text;
}

private void PreCheck_ButtonClick()


{
if (txtDisplay.Text == divideByZero || txtDisplay.Text ==
syntaxErr)
txtDisplay.Clear();
if(previousOperation != Operation.None)
{
EnableOperatorButtons();
}
}

private void EnableOperatorButtons(bool enable = true)


{
btnMul.Enabled = enable;
btnDiv.Enabled = enable;
btnAdd.Enabled = enable;
if (!enable)
{
decimalPointActive = false;
}
//btnSub.Enabled = enable;
}
enum Operation

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
{
Add,
Sub,
Mul,
Div,
None
}

Operation previousOperation = Operation.None;


Operation currentOperation = Operation.None;

private void BtnRes_Click(object sender, EventArgs e)


{
if (txtDisplay.TextLength == 0) return;
if (previousOperation != Operation.None)
PerformCalculation(previousOperation);

previousOperation = Operation.None;
}

private void BtnDecimal_Click(object sender, EventArgs e)


{
if (decimalPointActive) return;
if (txtDisplay.Text == syntaxErr || txtDisplay.Text ==
divideByZero)
{
txtDisplay.Text = string.Empty;
}
EnableOperatorButtons();
PreCheck_ButtonClick();
txtDisplay.Text += (sender as Button).Text;
decimalPointActive = true;
}

private void tableLayoutPanel2_Paint(object sender,


PaintEventArgs e)
{

}
}
}

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College

You might also like