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

更多的不说了,看原代码吧。

欢迎访问我的blog

http://blog.csdn.net/xjzdr/

要注意的是:

在(一)中,注册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;

            }

        }


    }
}

欢迎访问我的blog

http://blog.csdn.net/xjzdr/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xjzdr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值