Exportar (0) Imprimir
Expandir Tudo
Expandir Minimizar
Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.
Tradução
Original

Delegado KeyPressEventHandler

Representa o método que manipulará o KeyPress evento de um Control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (em System.Windows.Forms.dll)

public delegate void KeyPressEventHandler(
	Object sender,
	KeyPressEventArgs e
)

Parâmetros

sender
Tipo: System.Object
fonte do evento.
e
Tipo: System.Windows.Forms.KeyPressEventArgs
A KeyPressEventArgs que contém os dados do evento.

conjunto KeyPressEventArgs.Handled para true Para cancelar o KeyPress evento. Isso mantém o controle do processamento de chave pressione.

Quando você cria um KeyPressEventHandler delegado, você identifica o método que manipulará o evento. Para associar o evento com o manipulador de eventos, adicione uma instância do delegate ao evento.O manipulador de evento é chamado sempre que o evento ocorre, a menos que você remova o delegate.Para obter mais informações sobre tratamento eventos com delegados, consulte Eventos e representantes.

using System;
using System.Windows.Forms;

publicclass Form1: Form
{
    public Form1()
    {
        // Create a TextBox control.
        TextBox tb = new TextBox();
        this.Controls.Add(tb);
        tb.KeyPress += new KeyPressEventHandler(keypressed);
    }

    privatevoid keypressed(Object o, KeyPressEventArgs e)
    {
        // The keypressed method uses the KeyChar property to check // whether the ENTER key is pressed. // If the ENTER key is pressed, the Handled property is set to true, // to indicate the event is handled.if (e.KeyChar == (char)Keys.Return)
        {
            e.Handled = true;
        }
    }

    publicstaticvoid Main()
    {
        Application.Run(new Form1());
    }
}


import System.*;
import System.Windows.Forms.*;

public class Form1 extends Form
{
    public Form1()
    {
        // Create a TextBox control.
        TextBox tb = new TextBox();
        this.get_Controls().Add(tb);
        tb.add_KeyPress(new KeyPressEventHandler(KeyPressed));
    } //Form1

    void KeyPressed(Object o, KeyPressEventArgs e)
    {
        // The keypressed method uses the KeyChar property to check 
        // whether the ENTER key is pressed. 
        // If the ENTER key is pressed, the Handled property is set to true, 
        // to indicate the event is handled.
        if (e.get_KeyChar() == (char)(13)) {
            e.set_Handled(true);
        }
    } // KeyPressed

    public static void main(String[] args)
    {
        Application.Run(new Form1());
    } //main
} //Form1


// Boolean flag used to determine when a character other than a number is entered.privatebool nonNumberEntered = false;

// Handle the KeyDown event to determine the type of character entered into the control.privatevoid textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Initialize the flag to false.
    nonNumberEntered = false;

    // Determine whether the keystroke is a number from the top of the keyboard.if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
    {
        // Determine whether the keystroke is a number from the keypad.if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
        {
            // Determine whether the keystroke is a backspace.if(e.KeyCode != Keys.Back)
            {
                // A non-numerical keystroke was pressed.// Set the flag to true and evaluate in KeyPress event.
                nonNumberEntered = true;
            }
        }
    }
}

// This event occurs after the KeyDown event and can be used to prevent// characters from entering the control.privatevoid textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    // Check for the flag being set in the KeyDown event.if (nonNumberEntered == true)
    {
        // Stop the character from being entered into the control since it is non-numerical.
        e.Handled = true;
    }
}



// Boolean flag used to determine when a character other than a number 
// is entered.
private boolean nonNumberEntered = false;

// Handle the KeyDown event to determine the type of character entered 
// into the control.
private void textBox1_KeyDown(Object sender, 
    System.Windows.Forms.KeyEventArgs e)
{
    // Initialize the flag to false.
    nonNumberEntered = false;

    // Determine whether the keystroke is a number from the top of the 
    // keyboard.
    if ((e.get_KeyCode().CompareTo(Keys.D0)) < 0 
        || (e.get_KeyCode().CompareTo(Keys.D9) > 0)) {

        // Determine whether the keystroke is a number from the keypad.
        if ((e.get_KeyCode().CompareTo(Keys.NumPad0) < 0 
            || (e.get_KeyCode().CompareTo(Keys.NumPad9)) > 0)) {

            // Determine whether the keystroke is a backspace.
            if (!(e.get_KeyCode().Equals(Keys.Back))) {
                    // A non-numerical keystroke was pressed.
                    // Set the flag to true and evaluate in KeyPress event.
                    nonNumberEntered = true;
            }
        }
    }
} //textBox1_KeyDown

// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(Object sender, System.Windows.Forms.
    KeyPressEventArgs e)
{
    // Check for the flag being set in the KeyDown event.
    if (nonNumberEntered == true) {
        // Stop the character from being entered into the control since 
        // it is non-numerical.
        e.set_Handled(true);
    }
} //textBox1_KeyPress


Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

o.NET Framework e.NET Compact Framework não oferecem suporte a todas as versões de cada plataforma. Para obter uma lista de versões suportadas, consulte Requisitos de sistema do .NET framework.

.NET Framework

Compatível com: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Compatível com: 3.5, 2.0, 1.0

Contribuições da comunidade

ADICIONAR
Mostrar:
© 2014 Microsoft