要注意的是:ide
在(一)中,註冊TextChanged事件的代碼和本例中,註冊KeyPress的代碼不同。該代碼若是不會寫,能夠經過在Form中,添加一個textbox的keypress事件,而後將代碼COPY過來。this
註冊textchanged事件:spa
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);.net
註冊KeyPress事件:orm
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);blog
所有原代碼以下:事件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;get
namespace WindowsControlLibrary1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}博客
private void UserControl1_Load(object sender, EventArgs e)
{
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);it
}
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
int ch = e.KeyChar;
if (((ch >= 48) && (ch <= 57) || ch == 8 || ch == 13) == false)
{
e.Handled = true;
}
}
}
}
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/xjzdr/archive/2008/02/05/2084126.aspx