using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Calculadora
{
  /// <summary>
  /// Description of MainForm.
  /// </summary>
  public partial class MainForm : Form
  {
    private double dato1, dato2, resu;
    public MainForm()
    {
      //
      // The InitializeComponent() call is required for Windows Forms designer support.
      //
      InitializeComponent();
      
      //
      // TODO: Add constructor code after the InitializeComponent() call.
      //
    }
    
    void CtDato12Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
      TextBox ct = (TextBox)sender; // es la caja de texto
      try
      {
        if (ct == ctDato1)
          dato1 = Convert.ToDouble(ctDato1.Text);
        if (ct == ctDato2)
          dato2 = Convert.ToDouble(ctDato2.Text);
      }
      catch (Exception ex)
      {
        e.Cancel = true;
        ct.SelectAll();
        MessageBox.Show("El dato no es válido");
      }
    }
    
    void BtSumarClick(object sender, EventArgs e)
    {
      resu = dato1 + dato2;
      ctResultado.Text = string.Format("{0:F2}", resu);
    }
    
    void BtRestarClick(object sender, EventArgs e)
    {
      resu = dato1 - dato2;
      ctResultado.Text = string.Format("{0:F2}", resu);
    }
    
    void BtMultiplicarClick(object sender, EventArgs e)
    {
      resu = dato1 * dato2;
      ctResultado.Text = string.Format("{0:F2}", resu);
    }
    
    void BtDividirClick(object sender, EventArgs e)
    {
      resu = dato1 / dato2;
      ctResultado.Text = string.Format("{0:F2}", resu);
    }
    
    void BtCopiarClick(object sender, EventArgs e)
    {
      ctDato1.Text = ctResultado.Text;
      dato1 = resu;
    }
  }
}
