Skip to content

Instantly share code, notes, and snippets.

@d630
Last active July 13, 2018 02:16
Show Gist options
  • Save d630/c16f6cbb38740abdbf9ff849c8d89da7 to your computer and use it in GitHub Desktop.
Save d630/c16f6cbb38740abdbf9ff849c8d89da7 to your computer and use it in GitHub Desktop.
Tag 8: Calculator: Windows Forms, Delegates
using System;
using System.Windows.Forms;
namespace Calculator
{
public partial class Form1 : Form
{
public delegate double operate(double x, double y);
private operate op;
public Form1()
{
InitializeComponent();
textBox1.Text = "0";
textBox2.Text = "0";
label1.Text = "0";
}
private double addieren(double x, double y)
{
return x + y;
}
private double subtrahieren(double x, double y)
{
return x - y;
}
private double dividieren(double x, double y)
{
return y == 0 ? 0 : (x / y);
}
private double multiplizieren(double x, double y)
{
return x * y;
}
private void button1_Click(object sender, EventArgs e)
{
op = new operate(addieren);
label2.Text = "+";
}
private void button2_Click(object sender, EventArgs e)
{
op = new operate(subtrahieren);
label2.Text = "-";
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
if (op != null)
label1.Text = op(double.Parse(textBox1.Text), double.Parse(textBox2.Text)).ToString();
}
private void button4_Click(object sender, EventArgs e)
{
op = new operate(multiplizieren);
label2.Text = "*";
}
private void button5_Click(object sender, EventArgs e)
{
op = new operate(dividieren);
label2.Text = "/";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment