What is Handled for C#

Asked

Viewed 1,252 times

4

I saw in some codes the use of Handled being assigned as true and false of Combobox event arguments - Selectionchanged, Textbox - Lostfocus, Button - Click.

I wonder what it’s for and what it changes in the event or control?

Example

    private void cboNome_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {          
        e.Handled = true; 
    }
  • Have a look at ms: https://msdn.microsoft.com/pt-br/library/system.windows.forms.keyeventargs.handled%28v=vs.110%29.aspx

  • 1

    From what I understand it serves kind of as if to cancel the event action, or better not to change the control q fired the event.

1 answer

2


Definition of Handled according to the microsoft website:

Get or set a value that indicates whether the event has been treated.

Example of the use of handled control.

(It will determine whether the user pressed a non-numeric key in the textbox and if so, cancel the Keypress event using the Handled property)

 //Sinalizador booleano usado para determinar quando uma tecla  não numérica  é digitada.
    private bool nonNumberEntered = false;

    // Manipula o evento KeyDown para determinar o tipo de caractere digitado no controle.
    private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        // Inicializa a flag com false
        nonNumberEntered = false;

        //Determina se o numero pressionado é do conjunto superior do teclado
        if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
        {
            // Determina se o numero pressionado é do keypad.
            if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
            {
                // Determina se a tecla pressionada é backspace
                if(e.KeyCode != Keys.Back)
                {
                    // Um numero não numérico foi pressionado
                    // Seta a flag como true.
                    nonNumberEntered = true;
                }
            }
        }
        //Verifica se a tecla pressiona é shift
        if (Control.ModifierKeys == Keys.Shift) {
            nonNumberEntered = true;
        }
    }

 //Esse evento previne que caracteres digitados apos o evento KeyDown afetem o controle
    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
       // Verifca a flag que esta sendo definida no evento do KeyDown
        if (nonNumberEntered == true)
        {
            // Previne que caracteres não numéricos entrem no controle
            e.Handled = true;
        }
    }

For more details, see

-> https://msdn.microsoft.com/pt-br/library/system.windows.forms.toolstrippanelrendereventargs.handled(v=vs.110). aspx

-> https://msdn.microsoft.com/pt-br/library/system.windows.forms.keyeventargs.handled%28v=vs.110%29.aspx

-> https://social.msdn.microsoft.com/Search/pt-BR?query=Handled&emptyWatermark=true&ac=4

Browser other questions tagged

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