How to validate ComboBox?

2

I have a Form of register that has some fields TextBox and ComboBox , I want my code to allow the user to register only when all fields are filled, but only validating the TextBox thus allowing the registration to be carried out without selecting an option in ComboBox . Here is the code:

   private void btnCadastrar_Click(object sender, EventArgs e)
        {
            if (txtSerie.Text == string.Empty) 
            {
                MessageBox.Show("Por favor, selecione uma série!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtSerie.BackColor = Color.Red;
            }

            else
            {
                MessageBox.Show("O CADASTRO FOI REALIZADO COM SUCESSO!", "Novo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                LimpaTela();
            }  
        }
    
asked by anonymous 15.04.2018 / 02:09

1 answer

4

You can check the .SelectedIndex property of ComboBox . It returns a int that represents the position (Index) of the Item in ComboBox .

Add this:

else if (suaComboBox.SelectedIndex.Equals(-1))
{
    //--> seu código para tratar           
}
    
15.04.2018 / 05:46