Help with textbox validation

2

Have a good afternoon.

I'm having trouble completing a task, which is as follows:

I have a customer registration, which when the user edits something from the register, when saving a screen is displayed, to indicate the reason for the change, but in this screen, you can not leave the reason blank, problem ..

I'm using IF to check if it's populated or not, but even if it falls on the else and displaying my warning message, it closes the screen and returns to the client's registration screen,

At the customer registration screen, clicking the save button executes the code:

        else if (editar == 1)
        {
            Observacao_cliente obs = new Observacao_cliente(id_cliente); // verificar qual tipo de retorno para poder cadastrar a atualização.

            if (obs.ShowDialog() == DialogResult.OK)
            {
                chek_new_cli = cadastro_bd.cadastro_cliente(editar, textBox13.Text, textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox15.Text, textBox16.Text, textBox6.Text, textBox9.Text, textBox10.Text, textBox11.Text, textBox12.Text, comboBox1.Text, bloq_v, textBox7.Text, textBox8.Text, textBox14.Text);

                if (chek_new_cli > 0)
                { 
                   //inicia o cadastro no banco....

In the form of client_exception, clicking the save button executes the code:

private void button1_Click(object sender, EventArgs e) // botão salvar
    {
        if (comboBox1.Text != string.Empty && textBox2.Text != string.Empty)
        {
            check_salvar = cadastro.cadastro_obs_cliente(id_cliente, dateTimePicker1.Text, comboBox1.Text, textBox2.Text);

            if (check_salvar > 0)
            {
                this.Close();
            }
        }
        else
        {
            MessageBox.Show("O título e nem o motivo podem estar em branco!", "Observações Gerais", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }
    
asked by anonymous 21.07.2016 / 17:53

2 answers

3

You can perform the OnClose event handling of the form. This is the event that is called after the Close method is called. In it you can specify if you really can close.

The MSDN article a> explains well. Take a look at the Cancel documentation

And from there, you'll try the best that your rule allows:

e.Cancel = true;
MessageBox.Show("Opa! Não consigo fechar porque...");

or

e.Cancel = false;
MessageBox.Show("Já posso fechar a tela. Adeus!");
    
21.07.2016 / 18:09
0

Good afternoon friends.

After the help of our colleague, Andre Mesquita, I was able to solve it by adding the event below.

        private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
        {
            if(check_salvar == 0)
            {
                e.Cancel = true;
            }
        }

Thanks

    
21.07.2016 / 18:34