Hola estimados como están estoy implemtendo un cierre de caja y tengo este formulario, les muestro esta imagen
ahora, como verán funciona correctamente,pero. me gustaría que me den sus puntos de vista de que esta o no bien implementado. tengo todo este código para hacer ese trabajo
private void BsDoscientos()
{
if (this.textBox1.Text != "")
{
_Valor = textBox1.Text;
_Tag = Lbl200bs.Tag.ToString();
Resultado = decimal.Parse(_Valor) * decimal.Parse(_Tag);
Lbl200R.Text = Resultado.ToString();
Suma();
}
else
{
Lbl200R.Text = 0.ToString();
Suma();
return;
}
}
private void BsCien()
{
string ValorTexto = Txt100.Text;
if (ValorTexto != "")
{
_Valor = ValorTexto;
_Tag = Lbl100Bs.Tag.ToString();
Resultado = decimal.Parse(_Valor) * decimal.Parse(_Tag);
Lbl100R.Text = Resultado.ToString();
Suma();
}
else
{
Lbl100R.Text = 0.ToString();
Suma();
return;
}
}
private void BsCincuenta()
{
string ValorTexto = Txt50.Text;
if (ValorTexto != "")
{
_Valor = ValorTexto;
_Tag = Lbl50Bs.Tag.ToString();
Resultado = decimal.Parse(_Valor) * decimal.Parse(_Tag);
Lbl50R.Text = Resultado.ToString();
Suma();
}
else
{
Lbl50R.Text = 0.ToString();
Suma();
return;
}
}
private void BsVeinte()
{
string ValorTexto = Txt20.Text;
if (ValorTexto != "")
{
_Valor = ValorTexto;
_Tag = Lbl20Bs.Tag.ToString();
Resultado = decimal.Parse(_Valor) * decimal.Parse(_Tag);
Lbl20R.Text = Resultado.ToString();
Suma();
}
else
{
Lbl20R.Text = 0.ToString();
Suma();
return;
}
}
private void BsDiez()
{
string ValorTexto = Txt10.Text;
if (ValorTexto != "")
{
_Valor = ValorTexto;
_Tag = Lbl10Bs.Tag.ToString();
Resultado = decimal.Parse(_Valor) * decimal.Parse(_Tag);
Lbl10R.Text = Resultado.ToString();
Suma();
}
else
{
Lbl10R.Text = 0.ToString();
Suma();
return;
}
}
private void BsCinco()
{
string ValorTexto = Txt5.Text;
if (ValorTexto != "")
{
_Valor = ValorTexto;
_Tag = Lbl5Bs.Tag.ToString();
Resultado = decimal.Parse(_Valor) * decimal.Parse(_Tag);
Lbl5R.Text = Resultado.ToString();
Suma();
}
else
{
Lbl5R.Text = 0.ToString();
Suma();
return;
}
}
private void BsDos()
{
string ValorTexto = Txt2.Text;
if (ValorTexto != "")
{
_Valor = ValorTexto;
_Tag = Lbl2Bs.Tag.ToString();
Resultado = decimal.Parse(_Valor) * decimal.Parse(_Tag);
Lbl2R.Text = Resultado.ToString();
Suma();
}
else
{
Lbl2R.Text = 0.ToString();
Suma();
return;
}
}
private void BsUno()
{
string ValorTexto = Txt1.Text;
if (ValorTexto != "")
{
_Valor = ValorTexto;
_Tag = Lbl1Bs.Tag.ToString();
Resultado = decimal.Parse(_Valor) * decimal.Parse(_Tag);
Lbl1R.Text = Resultado.ToString();
Suma();
}
else
{
Lbl1R.Text = 0.ToString();
Suma();
return;
}
}
private void CincuentaCentavos()
{
string ValorTexto = Txt050.Text;
if (ValorTexto != "")
{
_Valor = ValorTexto;
_Tag = Lbl050Bs.Tag.ToString();
Resultado = decimal.Parse(_Valor) * decimal.Parse(_Tag);
Lbl050R.Text = Resultado.ToString();
Suma();
}
else
{
Lbl050R.Text = 0.ToString();
Suma();
return;
}
}
private void VeinteCentavos()
{
string ValorTexto = Txt020Ctvo.Text;
if (ValorTexto != "")
{
_Valor = ValorTexto;
_Tag = Lbl020Ctv.Tag.ToString();
Resultado = decimal.Parse(_Valor) * decimal.Parse(_Tag);
Lbl020CtvoR.Text = Resultado.ToString();
Suma();
}
else
{
Lbl020CtvoR.Text = 0.ToString();
Suma();
return;
}
}
private void DiezCentavos()
{
string ValorTexto = Txt010Ctvo.Text;
if (ValorTexto != "")
{
_Valor = ValorTexto;
_Tag = Lbl010Ctvo.Tag.ToString();
Resultado = decimal.Parse(_Valor) * decimal.Parse(_Tag);
Lbl010R.Text = Resultado.ToString();
Suma();
}
else
{
Lbl010R.Text = 0.ToString();
Suma();
return;
}
}
private void Txt100_TextChanged(object sender, EventArgs e)
{
BsCien();
}
private void Txt50_TextChanged(object sender, EventArgs e)
{
BsCincuenta();
}
private void Txt20_TextChanged(object sender, EventArgs e)
{
BsVeinte();
}
private void Txt10_TextChanged(object sender, EventArgs e)
{
BsDiez();
}
private void Txt5_TextChanged(object sender, EventArgs e)
{
BsCinco();
}
private void Txt2_TextChanged(object sender, EventArgs e)
{
BsDos();
}
private void Txt1_TextChanged(object sender, EventArgs e)
{
BsUno();
}
private void Txt050_TextChanged(object sender, EventArgs e)
{
CincuentaCentavos();
}
private void Txt020Ctvo_TextChanged(object sender, EventArgs e)
{
VeinteCentavos();
}
private void Txt010Ctvo_TextChanged(object sender, EventArgs e)
{
DiezCentavos();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
BsDoscientos();
}
y para la suma tengo este método,
private void Suma()
{
decimal Doscientos = decimal.Parse(Lbl200R.Text);
decimal Cien = decimal.Parse(Lbl100R.Text);
decimal Cincuenta = decimal.Parse(Lbl50R.Text);
decimal Veinte = decimal.Parse(Lbl20R.Text);
decimal Diez = decimal.Parse(Lbl10R.Text);
decimal Cinco = decimal.Parse(Lbl5R.Text);
decimal Dos = decimal.Parse(Lbl2R.Text);
decimal Un = decimal.Parse(Lbl1R.Text);
decimal CeroCincuenta = decimal.Parse(Lbl050R.Text);
decimal CeroVeinte = decimal.Parse(Lbl020CtvoR.Text);
decimal CeroDiez = decimal.Parse(Lbl010R.Text);
Total = Doscientos + Cien + Cincuenta + Veinte + Diez + Cinco + Dos + Un + CeroCincuenta + CeroVeinte + CeroDiez;
//TxtTotal.Text = Total.ToString("###0,00", CultureInfo.InvariantCulture);
TxtTotal.Text = string.Format("{0:n2}", Total, CultureInfo.InvariantCulture);
}
ahora para pasar de un texbox a otro tengo este método que funciona perfectamente
private void FrmCerrarCaja_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar((Keys.Enter)))
{
e.Handled = true;
SendKeys.Send("{TAB}");
}
}
y ahora tengo un problema, y es que me permite ingresar letras y signos negativos y me manda una exepcion. tengo este código en otro formulario el cual impide que ingrese letras y signos y solo habilita la tecla retroceso.
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
//Para obligar a que sólo se introduzcan números
if (Char.IsDigit(e.KeyChar))
{
e.Handled = false;
}
else
if (Char.IsControl(e.KeyChar)) //permitir teclas de control como retroceso
{
e.Handled = false;
}
else
{
//el resto de teclas pulsadas se desactivan
e.Handled = true;
}
}
ahora lo que había pensado es crear un evento keypress y llamarlo, pero aquí viene mi segunda pregunta, porque la primera es si el código anterior esta bien implementado o si hay como optimizarlo, y la pregunta es. Como hago eso, ya que de hacerlo de uno en uno se imaginan cuantos keypress debo abrir, osea el de cada texbox y agregar el código en cada evento de cada texbox. por favor me puede ayudar a resolver este problema que tengo . agradezco por la ayuda. Rob Les aclaro nuevamente que el evento este yo lo cree porque en otro formulario si funciona para un texbox. pero ahora son varios.
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{ } Lo llame Texbox_KeyPres por llamarlo e identificarlo