Enable or disable navigation buttons C#

Asked

Viewed 2,284 times

1

In the Binding Navigator from my table I have the following buttons:

inserir, cancelar edição, excluir e salvar (figura anexa).

I wanted to leave the inserir and excluir disabled when starting an edit in any field of my table (either in the form tab or on the grid), and enable them again if the changes are saved or cancelled. I thought it was easy to do that, but I’m really not getting it.

inserir a descrição da imagem aqui

1 answer

2


First, collect the names of the buttons you want to disable, so click them once and go to Properties and see the Name parameter

When to start editing? You must double-click each textBox and in the DataGridView and add code within each created method:

Nome-Botão-Inserir.Enabled = false;
Nome-Botão-Excluir.Enabled = false;

To enable just do the reverse, double click save and cancel button and put the codes:

Nome-Botão-Inserir.Enabled = true;
Nome-Botão-Excluir.Enabled = true;

Tip: The control menu created by Visual is not very intuitive, I suggest you create Buttons (Add, Cancel, Save, Clean data) instead of using them. For each button you can use the codes in the event _Click:

Add (add)

desBloq(); //método para ativar todos os campos para edição (evita que um usuário coloque dados antes de clicar no botão de add)
this.TABELABindingSource.AddNew();
this.add.Enabled = false;

Delete/Cancel

if (nomeTextBox.Enabled == false) { } // se os campos tiverem bloqueado ele não faz nada
else
{
    this.TABELABindingSource.RemoveCurrent(); //remove registro atual
    Bloq(); //bloqueia todos campos
    this.add.Enabled = true; // habilita botão add
}

Save

this.Validate(); //valida
this.TABELABindingSource.EndEdit(); // indica fim de edição
this.tableAdapterManager.UpdateAll(this.bibDigitalDataSet); //atualiza a conexão
Bloq(); //bloqueia todos campos
this.add.Enabled = true; //habilita botão add

Clear Fields

if (nomeTextBox.Enabled == false) { } // se tiver desabilitado ele não limpa nada
else
{
    nomeTextBox.Text = ""; // se não limpa cada campo (fazer uma linha para cada textbox
}

Bloq and Unlock Method

private void desBloq()
{
    nomeTextBox.Enabled = true; //habilita textBox (fazer uma linha para daca campo
 // o Bloq seria o inverso, ou seja, tudo false
}

And scheme .Enabled for each button

  • Thank you very much for your suggestions Leonardo. But I think I expressed myself badly in the question... what I really needed was to fire my method of disabling buttons when the user changes the contents of some field of the table understood? For example, if the user starts to change the name of the tenant, the buttons should already change settings, that is, only the save or cancel buttons should be enabled. It would more or less put this method in a table "change" event.

  • Make two clicks in the Textbox of the fields and put in the method the command Botão-Gravar.Enabled = true; and so on, it’s the same way it’s there, only two clicks on the button you give in the padding fields, it will create a method TxtBox_TextChanged then you put the controls to deactivate and activate the buttons inside it

Browser other questions tagged

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