0
Citation
I would like to add names to a listbox, put a NumericUpDown to associate how many dependent names will be added at most, depending on the number the user enters, and a txtbox to add names and a button to add to listbox, how can I do so so that if 0 dependents are placed, no name can be added, and if a higher value for example 2 can be added only 2 names in the listbox.
I tried that way, but it didn’t work:
private void btn_AdcNomedep_Click(object sender, EventArgs e)
{
    if (numUpDown_Dependentes.Value > 0 && numUpDown_Dependentes.Value >= listbox_NomesDep.Items.Count)
    {
        if (!listbox_NomesDep.Items.Contains(tb_NomeDep.Text))
        {
            listbox_NomesDep.Items.Add(tb_NomeDep.Text);
            tb_NomeDep.Clear();
            tb_NomeDep.Focus();
        }
        else
        {
            MessageBox.Show("Item já existente!", "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            tb_NomeDep.Clear();
            tb_NomeDep.Focus();
        }
    }
    else
    {
        MessageBox.Show("O número de dependentes já foi preenchido!", tb_NomeDep.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}