要注意的是:
在(一)中,注冊TextChanged事件的代碼和本例中,注冊KeyPress的代碼不一樣。該代碼如果不會寫,可以通過在Form中,添加一個textbox的keypress事件,然後将代碼COPY過來。
注冊textchanged事件:
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
注冊KeyPress事件:
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
全部原代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
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);
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