C # Upload numbers in NumericUpDown and take out a List in a TextBox

0



I have a NumericUpDown and I need that every time I change the value, I show in a textbox a List that I already have predefined. I have this code in the numericUpDownClients_ValueChanged, but I get exception from System.ArgumentOutOfRangeException.

Thanks !!

private void numericUpDownClientes_ValueChanged(object sender, EventArgs e)
    {

        if (numericUpDownClientes.Value == 0)
        {
            tsbGuardar.Enabled = false;
        }
        if (numericUpDownClientes.Value == 1)
        {
            tsbGuardar.Enabled = false;
            txtNombreClientes.Text = listClientes[0].getNombre();
            txtApellidosClientes.Text = listClientes[0].getApellidos();
            mtxtFechaClientes.Text = listClientes[0].getFecha();
            mtxtNifClientes.Text = listClientes[0].getNif();
            txtDireccionClientes.Text = listClientes[0].getDireccion();
            mtxtCodPostClientes.Text = listClientes[0].getCodigoPost().ToString();
        }
    }  

tsbSave is a button.
The ones that start with Txt are TextBox and the ones that start with mtxt are MaskedTextBox.
The list is of objects of a Client class, in which I have defined the constructors and the setters and getters.

Cliente c1 = new Cliente(1, "Pepe", "Lopez Ruiz", "21/05/1997", "65230147-
J", "C/Mayor 23", 2657);
    Cliente c2 = new Cliente(2, "Juan", "Fernandez Polo", "07/11/2000", 
"09086644-W", "C/Picasso 3", 28813);
    Cliente c3 = new Cliente(3, "Carmen", "Cuenca Olmo", "18/10/1996", 
"463017527-M", "C/Aves 25", 24103);
    Cliente c4 = new Cliente(4, "Ana", "Ruiz Dominguez", "01/01/1967", 
"68423671-T", "Avda. Constitucion 83", 26681);
    Cliente c5 = new Cliente(5, "Pedro", "Gomez Hurtado", "09/03/1989", 
"53014895-P", "C/Alameda 79", 28624);

    private bool tsbGuardarClick = false;

    private void valoresArray()
    {
        listClientes.Add(c1);
        listClientes.Add(c2);
        listClientes.Add(c3);
        listClientes.Add(c4);
        listClientes.Add(c5);
    }
    
asked by Beardman 27.10.2017 в 19:37
source

1 answer

0

As a first step you should control the exception that you mention System.ArgumentOutOfRangeException , here I leave a link so you know what the exception is system.argumentoutofrangeexception

As a second step in the valuechanged method that you declared the second if you should be replaced by else if , if it fulfills the 1st condition it does the coding but if it does not meet the Condition should proceed to check the 2 condition with else if .

private void numericUpDownClientes_ValueChanged(object sender, EventArgs e)
{
  try
  {
       if (numericUpDownClientes.Value == 0)
       {
        tsbGuardar.Enabled = false;
       }  
       else if (numericUpDownClientes.Value == 1)
       {
        tsbGuardar.Enabled = false;
        txtNombreClientes.Text = listClientes[0].getNombre();
        txtApellidosClientes.Text = listClientes[0].getApellidos();
        mtxtFechaClientes.Text = listClientes[0].getFecha();
        mtxtNifClientes.Text = listClientes[0].getNif();
        txtDireccionClientes.Text = listClientes[0].getDireccion();
        mtxtCodPostClientes.Text = listClientes[0].getCodigoPost().ToString();
       }
   }catch(ArgumentOutOfRangeException ex)
    {   
    MessageBox.Show("ERROR: " + ex.Message, "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
} 

And finally put a break point when filling the information to know what line your method is falling and have the idea of the reason.

Greetings.

    
answered by 28.10.2017 в 04:18