TextBox.KeyPress : TextBox « System.Windows.Forms « C# / C Sharp by API

Home
C# / C Sharp by API
1.Microsoft.Win32
2.System
3.System.Collections
4.System.Collections.Generic
5.System.Collections.Specialized
6.System.ComponentModel
7.System.Configuration
8.System.Data
9.System.Data.Common
10.System.Data.Linq
11.System.Data.Odbc
12.System.Data.OleDb
13.System.Data.Sql
14.System.Data.SqlClient
15.System.Diagnostics
16.System.DirectoryServices
17.System.Drawing
18.System.Drawing.Drawing2D
19.System.Drawing.Imaging
20.System.Drawing.Printing
21.System.Drawing.Text
22.System.EnterpriseServices
23.System.Globalization
24.System.IO
25.System.IO.Compression
26.System.IO.IsolatedStorage
27.System.IO.Ports
28.System.Linq
29.System.Management
30.System.Media
31.System.Messaging
32.System.Net
33.System.Net.Mail
34.System.Net.NetworkInformation
35.System.Net.Sockets
36.System.Reflection
37.System.Resources
38.System.Runtime
39.System.Runtime.CompilerServices
40.System.Runtime.InteropServices
41.System.Runtime.Remoting
42.System.Runtime.Remoting.Channels
43.System.Runtime.Remoting.Channels.Http
44.System.Runtime.Remoting.Messaging
45.System.Runtime.Serialization
46.System.Runtime.Serialization.Formatters.Binary
47.System.Runtime.Serialization.Formatters.Soap
48.System.Security
49.System.Security.AccessControl
50.System.Security.Cryptography
51.System.Security.Cryptography.X509Certificates
52.System.Security.Permissions
53.System.Security.Policy
54.System.Security.Principal
55.System.ServiceProcess
56.System.Text
57.System.Text.RegularExpressions
58.System.Threading
59.System.Timers
60.System.Web.Security
61.System.Web.Services
62.System.Windows.Controls
63.System.Windows.Forms
64.System.Xml
65.System.Xml.Linq
66.System.Xml.Schema
67.System.Xml.Serialization
68.System.Xml.XPath
69.System.Xml.Xsl
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp by API » System.Windows.Forms » TextBox 
TextBox.KeyPress
  
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
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(4816);
     this.label1.Name = "label1";
     this.label1.Size = new System.Drawing.Size(17623);
     this.label1.TabIndex = 0;
     this.label1.Text = "ISBN Validation";
     // 
     // txtInput
     // 
     this.txtInput.Location = new System.Drawing.Point(7264);
     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.ComponentModel.CancelEventHandler(this.handleCancleEvent);
     // 
     // label2
     // 
     this.label2.Location = new System.Drawing.Point(24104);
     this.label2.Name = "label2";
     this.label2.Size = new System.Drawing.Size(8023);
     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(112104);
     this.lblTrue.Name = "lblTrue";
     this.lblTrue.TabIndex = 3;
     // 
     // label3
     // 
     this.label3.Location = new System.Drawing.Point(32152);
     this.label3.Name = "label3";
     this.label3.Size = new System.Drawing.Size(7223);
     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(112152);
     this.lblCheck.Name = "lblCheck";
     this.lblCheck.TabIndex = 5;
     // 
     // lblResults
     // 
     this.lblResults.Location = new System.Drawing.Point(56192);
     this.lblResults.Name = "lblResults";
     this.lblResults.Size = new System.Drawing.Size(15224);
     this.lblResults.TabIndex = 8;
     // 
     // TextCancelEventKeyEvent
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(513);
     this.ClientSize = new System.Drawing.Size(264293);
     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.ComponentModel.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;
     }
  }     
}

   
    
  
Related examples in the same category
1.TextBox.AcceptsTab
2.TextBox.Anchor
3.TextBox.BorderStyle
4.TextBox.CanFocus
5.TextBox.Clear()
6.TextBox.Click
7.TextBox.ContextMenu
8.TextBox.ContainsFocus
9.TextBox.DataBindings
10.TextBox.DoDragDrop
11.TextBox.DragDrop
12.TextBox.DragEnter
13.TextBox.Focus()
14.TextBox.Focused
15.TextBox.GotFocus
16.TextBox.Lines
17.TextBox.LostFocus
18.TextBox.Multiline
19.TextBox.MouseDown
20.TextBox.PasswordChar
21.TextBox.ScrollBars
22.TextBox.SelectAll()
23.TextBox.SelectedText
24.TextBox.SelectionLength
25.TextBox.TextChanged
26.TextBox.Validated
27.TextBox.Validating
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.