Help with textbox validation

Asked

Viewed 164 times

2

Guys good afternoon.

I’m having difficulty performing a task, which is as follows::

I have a customer registration, that when the user edits something from the register, when saving is presented a screen, for it indicate the reason for the change, but on this screen, can not leave the reason blank, that enters the problem..

I’m using IF to check whether it’s filled or not, but even if it falls on LSE and displaying my warning message, it closes the screen and goes back to the customer registration screen,

In the client registration screen, when 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 observation_client form, when 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);
        }
    }
  • 1

    Have you tried treating the Form Onclose event? On onclose you will test if the field is empty and does not change the state preventing the event from continuing.

  • Onclose? I will search on. Thank you.

  • 1

    Read this article showing how the event treatment works Article MSDN

  • 1

    Thanks a lot friend. I managed to settle with your help.

2 answers

3


You can perform the Onclose event treatment from the form. This is the event that is called after the Close method is called. In it you can specify if you can actually close.

The article of the MSDN explains well. Take a look at property Cancel in the documentation

And from there, you handle the best way 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!");

0

Friends good afternoon.

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

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

Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.