VS2005 winform用户控件(二):只能输入数字的textbox控件

要注意的是: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

相关文章
相关标签/搜索