When I enter a quantity of a product, which is of the Decimal type, I use a TextBox:
decimal STOCK;
if(!decimal.TryParse(txtStock.Text, out STOCK))
{
MessageBox.Show("Debe ingresar un valor correcto para el stock", "Advertencia");
return;
}
I save the variable in producto.stock = STOCK
, also valid from the TextBox event that a number with a comma is entered.
private void txtStock_KeyPress(object sender, KeyPressEventArgs e)
{
//if (e.KeyChar == 8)
//{
// e.Handled = false;
// return;
//}
//bool IsDec = false;
//int nroDec = 0;
//for (int i = 0; i < txtStock.Text.Length; i++)
//{
// if (txtStock.Text[i] == ',')
// IsDec = true;
// if (IsDec && nroDec++ >= 2)
// {
// e.Handled = true;
// return;
// }
//}
//if (e.KeyChar >= 48 && e.KeyChar <= 57)
// e.Handled = false;
//else if (e.KeyChar == 44)
// e.Handled = (IsDec) ? true : false;
//else
// e.Handled = true;
}
My problem is when I enter for example 0,5
saves it as 1
or if income 0,3
saves it as 0
.