Vous êtes sur la page 1sur 3

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 AppI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private TextBox objTextBox = null;

private void CtGC_KeyPress(object sender, KeyPressEventArgs e)


{

if (e.KeyChar == Convert.ToChar(13))
{
e.Handled = true;
Conversion(sender);
}
else if (e.KeyChar == Convert.ToChar(8))
{
e.Handled = false;
}

else if (e.KeyChar == ',')


{
TextBox objTextBox = (TextBox)sender;
if (objTextBox.Text.IndexOf(',') != -1)
{
e.Handled = true;
}
}

else if (e.KeyChar == '-' || e.KeyChar == '+')


{
TextBox objTextBox = (TextBox)sender;

/* if (objTextBox.SelectionLength==objTextBox.TextLength)
{
e.Handled = false;
}
/* else if(objTextBox.TextLength != 0)
{
e.Handled = true;
}*/

//Mismodificaciones
if (objTextBox.Text.IndexOf('-') != 0)
{
e.Handled = true;
}

}
else if (e.KeyChar < '0' || e.KeyChar > '9')
{
e.Handled = true;
}
}
private void CtGF_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(13))
{
e.Handled = true;
Conversion(sender);
}

private void Conversion(object sender)


{

TextBox objTextBox = (TextBox)sender;


try
{
double Grados;
if (objTextBox == ctGC)
{
Grados = Convert.ToDouble(ctGC.Text) * 9.0 / 5.0 + 32;
//ctGF.Text = Grados.ToString();
ctGF.Text = string.Format("{0:F2}", Grados);
}

else if (objTextBox == ctGF)


//Si se escribio en la caja de texto Centigrados
{
Grados = (Convert.ToDouble(ctGF.Text) - 32.0) * 5.0 / 9.0;
//ctGC.Text = Grados.ToString();
ctGC.Text = string.Format("{0:F2}", Grados);
}
}

catch (Exception error)


{
MessageBox.Show("Error en la entrada");
MessageBox.Show(error.ToString());
ctGC.Text = "0,00";
ctGF.Text = "32,00";
}
}
private void Aceptar_Click(object sender, EventArgs e)
{
/*try
{
double Grados;
if (objTextBox == ctGC)
{
Grados = Convert.ToDouble(ctGC.Text) * 9.0 / 5.0 + 32;
//ctGF.Text = Grados.ToString();
ctGF.Text = string.Format("{0:F2}", Grados);
}

else if (objTextBox == ctGF)


//Si se escribio en la caja de texto Centigrados
{
Grados = (Convert.ToDouble(ctGF.Text) - 32.0) * 5.0 / 9.0;
//ctGC.Text = Grados.ToString();
ctGC.Text = string.Format("{0:F2}", Grados);
}
}

catch (Exception error)


{
MessageBox.Show("Error en la entrada");
MessageBox.Show(error.ToString());
ctGC.Text = "0,00";
ctGF.Text = "32,00";
}*/
}

private void Form1_Shown(object sender, EventArgs e)


{
ctGC.Focus();
}

private void Form1_Enter(object sender, EventArgs e)


{
objTextBox = (TextBox)sender;
}

}
}