ToolStripControlHost.KeyPress Zdarzenie

Definicja

Występuje, gdy klawisz jest naciskany, gdy hostowana kontrolka ma fokus.

public:
 event System::Windows::Forms::KeyPressEventHandler ^ KeyPress;
public event System.Windows.Forms.KeyPressEventHandler KeyPress;
public event System.Windows.Forms.KeyPressEventHandler? KeyPress;
member this.KeyPress : System.Windows.Forms.KeyPressEventHandler 
Public Custom Event KeyPress As KeyPressEventHandler 

Typ zdarzenia

Przykłady

Poniższy przykład kodu przedstawia użycie tego elementu członkowskiego. W tym przykładzie program obsługi zdarzeń zgłasza wystąpienie zdarzenia KeyPress . Ten raport pomaga dowiedzieć się, kiedy wystąpi zdarzenie i może pomóc w debugowaniu. Aby zgłosić wiele zdarzeń lub zdarzeń, które występują często, rozważ zastąpienie MessageBox.Show komunikatu ciągiem Console.WriteLine lub dołączeniem go do wielowierszowego TextBoxelementu .

Aby uruchomić przykładowy kod, wklej go w projekcie zawierającym wystąpienie typu ToolStripControlHost o nazwie ToolStripControlHost1. Następnie upewnij się, że program obsługi zdarzeń jest skojarzony ze zdarzeniem KeyPress .

private void ToolStripControlHost1_KeyPress(Object sender, KeyPressEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "KeyChar", e.KeyChar );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Handled", e.Handled );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "KeyPress Event" );
}
Private Sub ToolStripControlHost1_KeyPress(sender as Object, e as KeyPressEventArgs) _ 
     Handles ToolStripControlHost1.KeyPress

    Dim messageBoxVB as New System.Text.StringBuilder()
    messageBoxVB.AppendFormat("{0} = {1}", "KeyChar", e.KeyChar)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Handled", e.Handled)
    messageBoxVB.AppendLine()
    MessageBox.Show(messageBoxVB.ToString(),"KeyPress Event")

End Sub

Uwagi

Aby uzyskać więcej informacji na temat obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.

Dotyczy

Zobacz też