How to clean textboxes converted to Double

1

I have this event of textbox amount sold :

private void txtcantidadvendida_Validated(object sender, EventArgs e)

        {
            double existencia, venta;
            existencia = Convert.ToDouble(txtexistencia.Text);
            venta = Convert.ToDouble(txtcantidadvendida.Text);

            if (venta > existencia)
            {
                MessageBox.Show("La cantidad que quiere vender es mayor que la cantidad disponible actualmente para la venta de este producto.");
                txtcodigo.Clear();
                txtdescripcion.Clear();
                txtreferencia.Clear();
                txtmarca.Clear();
                txtexistencia.Clear();
                txtcantidadvendida.Clear();
                txtpreciopieza.Clear();
                txttotalventa.Clear();
                txtcodigo.Enabled = false;
                btnbuscar.Enabled = false;
                txtcantidadvendida.Enabled = false;
                btnañadir.Enabled = false;
                btneliminar.Enabled = false;
                btnventa.Enabled = false;
                txtcodigo.Enabled = true;
                btnbuscar.Enabled = true;
                txtcodigo.Select();
            }
            else
            {
                Double a = 0;
                a = Double.Parse(txtcantidadvendida.Text) * Double.Parse(txtpreciopieza.Text);
                txttotalventa.Text = a.ToString();
                txtcantidadvendida.Enabled = false;
            }

        }

And a clean button:

private void btnlimpiar_Click(object sender, EventArgs e)

        {
            txtcedula.Enabled = true;
            txtcedula.Clear();
            txtcedula.Select();
            btnbuscar2.Enabled = true;
            txtnombre.Clear();
            txtapellido.Clear();
            txttelefono.Clear();
            txtcelular.Clear();
            txtcodigo.Clear();
            txtdescripcion.Clear();
            txtreferencia.Clear();
            txtmarca.Clear();
            txtexistencia.Clear();
            txtcantidadvendida.Clear();
            txtpreciopieza.Clear();
            txttotalventa.Clear();
            txtcodigo.Enabled = false;
            btnbuscar.Enabled = false;
            txtcantidadvendida.Enabled = false;
            btnañadir.Enabled = false;
            btneliminar.Enabled = false;
            btnventa.Enabled = false;
            txtcedula.Select();
        }

If I already have my textbox of the form filled, but I made a mistake and I want to clean them, when I click on the button it gives me the following error:

  

An exception of type 'System.FormatException' occurred in mscorlib.dll but it was not handled in user code.
  Additional information: The input string does not have the correct format.

And it points out the error in this part of the conversion:

double existencia, venta;'

existencia = Convert.ToDouble(txtexistencia.Text);

venta = Convert.ToDouble(txtcantidadvendida.Text);
    
asked by Samuel Ignacio Susana Confesor 02.07.2017 в 21:11
source

1 answer

3

Before performing the "Convert.ToDouble" function:

double existencia, venta;'

existencia = Convert.ToDouble(txtexistencia.Text);

venta = Convert.ToDouble(txtcantidadvendida.Text);

You need to verify that the textboxes in your Text property have value, this you do in the following way:

 if (!string.IsNullOrEmpty(txtexistencia.Text))
            {

                //Se crea una veriable auxiliar ya que el metodo TryParse requiere un 
                //parametro de tipo double y que sea definido como salida
                double parseAux = 0.0;

                //Aqui es donde trata de parsear el valor que hay en el textbox
                double.TryParse(txtexistencia.Text, out parseAux);

            }

also uses the double.TryParse function to verify that the value in the Text property of the textbox can be parsed.

    
answered by 02.07.2017 / 22:22
source