读书人

只好输入int数字的文本控件

发布时间: 2012-12-23 11:28:15 作者: rapoo

只能输入int数字的文本控件

using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Text;using System.Windows.Forms;namespace Renters.UI{    public partial class txtIntBox : UserControl    {        public txtIntBox()        {            InitializeComponent();        }        /// 时间:2007-8-20        /// 修改:CAKEBIRD        /// 功能:用户控件实现对text只能输入int型数字的控制        /// 更新记录        /// 时间:2007-8-29         /// 修改功能:增加数据大于0判断        public string txt_Text        {            get            {                return this.textBox1.Text.Trim().ToString();            }            set            {                this.textBox1.Text = value;            }        }        //文本框的ReadOnly属性        public bool ReadOnly        {            get            {                return this.textBox1.ReadOnly;            }            set            {                this.textBox1.ReadOnly = value;            }        }        private void textBox1_TextChanged(object sender, EventArgs e)        {                       string txt = textBox1.Text.ToString();            if (txt != "")            {                if (tbchange(txt))                {                }                else                {                    textBox1.Text = txt.Remove(txt.Length - 1);                    textBox1.Select(textBox1.Text.Length, 0);                }            }                    }                private bool tbchange(string txt)        {            try            {                                double  i = System.Int32.Parse(txt);                if (i < 0)                {                    return false;                }                return true;            }            catch             {                return false;                            }        }        private void txtIntBox_Load(object sender, EventArgs e)        {            textBox1.Width = this.Size.Width;        }    }}

读书人网 >编程

热点推荐