Csharp/CSharp Tutorial/GUI Windows Forms/KeyEvent

Материал из .Net Framework эксперт
Версия от 12:15, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Change key event (key code) during in the event handler

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class KeyEventDetailInfo : System.Windows.Forms.Form
{
  private System.Windows.Forms.TextBox txtInput;
  private System.Windows.Forms.Label lblUpper;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label lblLower;
  private System.Windows.Forms.Button btnReset;
  public KeyEventDetailInfo()
  {
    InitializeComponent();
  }
  private void InitializeComponent()
  {
    this.txtInput = new System.Windows.Forms.TextBox();
    this.btnReset = new System.Windows.Forms.Button();
    this.lblUpper = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.label1 = new System.Windows.Forms.Label();
    this.lblLower = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // txtInput
    // 
    this.txtInput.Location = new System.Drawing.Point(8, 8);
    this.txtInput.Name = "txtInput";
    this.txtInput.TabIndex = 0;
    this.txtInput.Text = "";
    this.txtInput.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtInput_KeyDown);
    this.txtInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtInput_KeyPress);
    this.txtInput.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtInput_KeyUp);
    // 
    // btnReset
    // 
    this.btnReset.Location = new System.Drawing.Point(328, 8);
    this.btnReset.Name = "btnReset";
    this.btnReset.TabIndex = 2;
    this.btnReset.Text = "Reset";
    this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
    // 
    // lblUpper
    // 
    this.lblUpper.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblUpper.Location = new System.Drawing.Point(368, 56);
    this.lblUpper.Name = "lblUpper";
    this.lblUpper.Size = new System.Drawing.Size(32, 23);
    this.lblUpper.TabIndex = 3;
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(320, 56);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(40, 16);
    this.label2.TabIndex = 4;
    this.label2.Text = "Upper:";
    this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(320, 104);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(40, 16);
    this.label1.TabIndex = 5;
    this.label1.Text = "Lower:";
    this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
    // 
    // lblLower
    // 
    this.lblLower.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblLower.Location = new System.Drawing.Point(368, 104);
    this.lblLower.Name = "lblLower";
    this.lblLower.Size = new System.Drawing.Size(32, 23);
    this.lblLower.TabIndex = 6;
    // 
    // KeyEventDetailInfo
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(417, 293);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                             this.lblLower,
                                             this.label1,
                                             this.label2,
                                             this.lblUpper,
                                             this.btnReset,
                                             this.txtInput});
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new KeyEventDetailInfo());
  }
  private void txtInput_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  {
    Console.WriteLine("KeyCode name: " + e.KeyCode);
    Console.WriteLine("KeyCode key code: " + ((int)e.KeyCode));
    Console.WriteLine("KeyData name: " + e.KeyData);
    Console.WriteLine("KeyData key code: " + ((int)e.KeyData));
    Console.WriteLine("KeyValue: " + e.KeyValue);
    Console.WriteLine("Handled: " + e.Handled);
  }
  private void txtInput_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  {
    char keyChar;
    keyChar = e.KeyChar;
    Console.WriteLine("KeyPress event.");
    Console.WriteLine("KeyChar: " + keyChar);
    Console.WriteLine("KeyChar Code: " + (int)keyChar);
    Console.WriteLine("Handled: " + e.Handled);
    lblUpper.Text = keyChar.ToString().ToUpper();
    lblLower.Text = keyChar.ToString().ToLower();
  
    if (keyChar.ToString() == "$")
    {
      txtInput.AppendText("#");
      e.Handled = true;
    }
  }
  private void txtInput_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
  {
    Console.WriteLine("KeyCode name: " + e.KeyCode);
    Console.WriteLine("KeyCode key code: " + ((int)e.KeyCode));
    Console.WriteLine("KeyData name: " + e.KeyData);
    Console.WriteLine("KeyData key code: " + ((int)e.KeyData));
    Console.WriteLine("KeyValue: " + e.KeyValue);
    Console.WriteLine("Handled: " + e.Handled);
  }
  private void btnReset_Click(object sender, System.EventArgs e)
  {
    txtInput.Text = "";
    lblUpper.Text = "";
    lblLower.Text = "";
  }
}

KeyEvent: key char(code)

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class TextCancelEventKeyEvent : System.Windows.Forms.Form
{
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.TextBox txtInput;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label lblTrue;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label lblCheck;
    private System.Windows.Forms.Label lblResults;
  public TextCancelEventKeyEvent()
  {
    InitializeComponent();
  }
  private void InitializeComponent()
  {
     this.label1 = new System.Windows.Forms.Label();
     this.txtInput = new System.Windows.Forms.TextBox();
     this.label2 = new System.Windows.Forms.Label();
     this.lblTrue = new System.Windows.Forms.Label();
     this.label3 = new System.Windows.Forms.Label();
     this.lblCheck = new System.Windows.Forms.Label();
     this.lblResults = new System.Windows.Forms.Label();
     this.SuspendLayout();
     // 
     // label1
     // 
     this.label1.Font = new System.Drawing.Font("Tahoma", 14.25F, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
     this.label1.Location = new System.Drawing.Point(48, 16);
     this.label1.Name = "label1";
     this.label1.Size = new System.Drawing.Size(176, 23);
     this.label1.TabIndex = 0;
     this.label1.Text = "ISBN Validation";
     // 
     // txtInput
     // 
     this.txtInput.Location = new System.Drawing.Point(72, 64);
     this.txtInput.Name = "txtInput";
     this.txtInput.TabIndex = 1;
     this.txtInput.Text = "";
     this.txtInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtInput_KeyPress);
     this.txtInput.Validating += new System.ruponentModel.CancelEventHandler(this.handleCancleEvent);
     // 
     // label2
     // 
     this.label2.Location = new System.Drawing.Point(24, 104);
     this.label2.Name = "label2";
     this.label2.Size = new System.Drawing.Size(80, 23);
     this.label2.TabIndex = 2;
     this.label2.Text = "True Number:";
     // 
     // lblTrue
     // 
     this.lblTrue.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
     this.lblTrue.Location = new System.Drawing.Point(112, 104);
     this.lblTrue.Name = "lblTrue";
     this.lblTrue.TabIndex = 3;
     // 
     // label3
     // 
     this.label3.Location = new System.Drawing.Point(32, 152);
     this.label3.Name = "label3";
     this.label3.Size = new System.Drawing.Size(72, 23);
     this.label3.TabIndex = 4;
     this.label3.Text = "Check Digit:";
     // 
     // lblCheck
     // 
     this.lblCheck.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
     this.lblCheck.Location = new System.Drawing.Point(112, 152);
     this.lblCheck.Name = "lblCheck";
     this.lblCheck.TabIndex = 5;
     // 
     // lblResults
     // 
     this.lblResults.Location = new System.Drawing.Point(56, 192);
     this.lblResults.Name = "lblResults";
     this.lblResults.Size = new System.Drawing.Size(152, 24);
     this.lblResults.TabIndex = 8;
     // 
     // TextCancelEventKeyEvent
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(264, 293);
     this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.lblResults,
                                                                  this.lblCheck,
                                                                  this.label3,
                                                                  this.lblTrue,
                                                                  this.label2,
                                                                  this.txtInput,
                                                                  this.label1});
     this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new TextCancelEventKeyEvent());
  }
  private void handleCancleEvent(object sender, System.ruponentModel.CancelEventArgs e)
  {
    TextBox tb = (TextBox)sender;
    string strInput = tb.Text;
        Console.WriteLine(strInput);
  }     
  private void txtInput_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  {
     char keyChar;
     keyChar = e.KeyChar;
     if(!Char.IsDigit(keyChar)      // 0 - 9
        &&
        keyChar != 8               // backspace
        &&
        keyChar != 13              // enter
        &&
        keyChar != "x"
        &&
        keyChar != 45              //  dash/minus
        ){
        //  Do not display the keystroke
        e.Handled = true;
     }
  }     
}

Key event: key down, key up, key pressed

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class KeyEventDetailInfo : System.Windows.Forms.Form
{
  private System.Windows.Forms.TextBox txtInput;
  private System.Windows.Forms.Label lblUpper;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label lblLower;
  private System.Windows.Forms.Button btnReset;
  public KeyEventDetailInfo()
  {
    InitializeComponent();
  }
  private void InitializeComponent()
  {
    this.txtInput = new System.Windows.Forms.TextBox();
    this.btnReset = new System.Windows.Forms.Button();
    this.lblUpper = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.label1 = new System.Windows.Forms.Label();
    this.lblLower = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // txtInput
    // 
    this.txtInput.Location = new System.Drawing.Point(8, 8);
    this.txtInput.Name = "txtInput";
    this.txtInput.TabIndex = 0;
    this.txtInput.Text = "";
    this.txtInput.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtInput_KeyDown);
    this.txtInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtInput_KeyPress);
    this.txtInput.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtInput_KeyUp);
    // 
    // btnReset
    // 
    this.btnReset.Location = new System.Drawing.Point(328, 8);
    this.btnReset.Name = "btnReset";
    this.btnReset.TabIndex = 2;
    this.btnReset.Text = "Reset";
    this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
    // 
    // lblUpper
    // 
    this.lblUpper.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblUpper.Location = new System.Drawing.Point(368, 56);
    this.lblUpper.Name = "lblUpper";
    this.lblUpper.Size = new System.Drawing.Size(32, 23);
    this.lblUpper.TabIndex = 3;
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(320, 56);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(40, 16);
    this.label2.TabIndex = 4;
    this.label2.Text = "Upper:";
    this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(320, 104);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(40, 16);
    this.label1.TabIndex = 5;
    this.label1.Text = "Lower:";
    this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
    // 
    // lblLower
    // 
    this.lblLower.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblLower.Location = new System.Drawing.Point(368, 104);
    this.lblLower.Name = "lblLower";
    this.lblLower.Size = new System.Drawing.Size(32, 23);
    this.lblLower.TabIndex = 6;
    // 
    // KeyEventDetailInfo
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(417, 293);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                             this.lblLower,
                                             this.label1,
                                             this.label2,
                                             this.lblUpper,
                                             this.btnReset,
                                             this.txtInput});
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new KeyEventDetailInfo());
  }
  private void txtInput_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  {
    Console.WriteLine("KeyCode name: " + e.KeyCode);
    Console.WriteLine("KeyCode key code: " + ((int)e.KeyCode));
    Console.WriteLine("KeyData name: " + e.KeyData);
    Console.WriteLine("KeyData key code: " + ((int)e.KeyData));
    Console.WriteLine("KeyValue: " + e.KeyValue);
    Console.WriteLine("Handled: " + e.Handled);
  }
  private void txtInput_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  {
    char keyChar;
    keyChar = e.KeyChar;
    Console.WriteLine("KeyPress event.");
    Console.WriteLine("KeyChar: " + keyChar);
    Console.WriteLine("KeyChar Code: " + (int)keyChar);
    Console.WriteLine("Handled: " + e.Handled);
    lblUpper.Text = keyChar.ToString().ToUpper();
    lblLower.Text = keyChar.ToString().ToLower();
  
    if (keyChar.ToString() == "$")
    {
      txtInput.AppendText("#");
      e.Handled = true;
    }
  }
  private void txtInput_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
  {
    Console.WriteLine("KeyCode name: " + e.KeyCode);
    Console.WriteLine("KeyCode key code: " + ((int)e.KeyCode));
    Console.WriteLine("KeyData name: " + e.KeyData);
    Console.WriteLine("KeyData key code: " + ((int)e.KeyData));
    Console.WriteLine("KeyValue: " + e.KeyValue);
    Console.WriteLine("Handled: " + e.Handled);
  }
  private void btnReset_Click(object sender, System.EventArgs e)
  {
    txtInput.Text = "";
    lblUpper.Text = "";
    lblLower.Text = "";
  }
}