一个自定义的数字输入框控件

一个自定义的数字输入框控件


        项目开发中经常会用到只允许输入数字的文本框控件。虽然很多第三方控商已经提供了不少此类优秀的控件,但是我们为什么就不能自已动手也来DIY一把呢。以下就是本人的一个小例子:

        我的设计思路是在用户每次敲击键盘时对键入的字符进行校验以过滤不合法的字符。在文本框失去焦点时再对录入的数据进行一次校验以符合预定的格式。以下是该控件的实现代码。
  1 None.gif using  System;
  2 None.gif using  System.Collections.Generic;
  3 None.gif using  System.ComponentModel;
  4 None.gif using  System.Drawing;
  5 None.gif using  System.Data;
  6 None.gif using  System.Text;
  7 None.gif using  System.Windows.Forms;
  8 None.gif
  9 None.gif namespace  Nelson.ControlLibrary
 10 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 11InBlock.gif    public partial class NumberBox : System.Windows.Forms.TextBox
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 13InBlock.gif        public NumberBox()
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 15InBlock.gif            InitializeComponent();
 16ExpandedSubBlockEnd.gif        }

 17InBlock.gif
 18ContractedSubBlock.gifExpandedSubBlockStart.gif        自定义成员#region 自定义成员
 19InBlock.gif        private int maxIntegerLength = 10;
 20InBlock.gif        private int minIntegerLength = 0;
 21InBlock.gif        private int maxDecimalLength = 4;
 22InBlock.gif        private int minDecimalLength = 0;
 23InBlock.gif        private int integerLength;
 24InBlock.gif        private int decimalLength;
 25ExpandedSubBlockEnd.gif        #endregion

 26InBlock.gif
 27ContractedSubBlock.gifExpandedSubBlockStart.gif        自定义属性#region 自定义属性
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 29InBlock.gif        /// 最大整数位数
 30ExpandedSubBlockEnd.gif        /// </summary>

 31InBlock.gif        public int MaxIntegerLength
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 33InBlock.gif            get
 34ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 35InBlock.gif                return maxIntegerLength;
 36ExpandedSubBlockEnd.gif            }

 37InBlock.gif            set
 38ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 39InBlock.gif                if (value >= 0 && value >= minIntegerLength)
 40ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 41InBlock.gif                    maxIntegerLength = value;
 42ExpandedSubBlockEnd.gif                }

 43InBlock.gif                else
 44ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 45InBlock.gif                    throw new Exception("最大整数位数不应小于最小整数位数");
 46ExpandedSubBlockEnd.gif                }

 47ExpandedSubBlockEnd.gif            }

 48ExpandedSubBlockEnd.gif        }

 49InBlock.gif
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 51InBlock.gif        /// 最小整数位数
 52ExpandedSubBlockEnd.gif        /// </summary>

 53InBlock.gif        public int MinIntegerLength
 54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 55InBlock.gif            get
 56ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 57InBlock.gif                return minIntegerLength;
 58ExpandedSubBlockEnd.gif            }

 59InBlock.gif            set
 60ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 61InBlock.gif                if (value >= 0 && value <= maxIntegerLength)
 62ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 63InBlock.gif                    minIntegerLength = value;
 64ExpandedSubBlockEnd.gif                }

 65InBlock.gif                else
 66ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 67InBlock.gif                    throw new Exception("最小整数位数不应大于最大整数位数");
 68ExpandedSubBlockEnd.gif                }

 69ExpandedSubBlockEnd.gif            }

 70ExpandedSubBlockEnd.gif        }

 71InBlock.gif
 72ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 73InBlock.gif        /// 最大小数位数
 74ExpandedSubBlockEnd.gif        /// </summary>

 75InBlock.gif        public int MaxDecimalLength
 76ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 77InBlock.gif            get
 78ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 79InBlock.gif                return maxDecimalLength;
 80ExpandedSubBlockEnd.gif            }

 81InBlock.gif            set
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 83InBlock.gif                if (value >= 0 && value >= minDecimalLength)
 84ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 85InBlock.gif                    maxDecimalLength = value;
 86ExpandedSubBlockEnd.gif                }

 87InBlock.gif                else
 88ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 89InBlock.gif                    throw new Exception("最大小数位数不应小于最小小数位数");
 90ExpandedSubBlockEnd.gif                }

 91ExpandedSubBlockEnd.gif            }

 92ExpandedSubBlockEnd.gif        }

 93InBlock.gif
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 95InBlock.gif        /// 最小小数位数
 96ExpandedSubBlockEnd.gif        /// </summary>

 97InBlock.gif        public int MinDecimalLength
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 99InBlock.gif            get
100ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
101InBlock.gif                return minDecimalLength;
102ExpandedSubBlockEnd.gif            }

103InBlock.gif            set
104ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
105InBlock.gif                if (value >= 0 && value <= maxDecimalLength)
106ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
107InBlock.gif                    minDecimalLength = value;
108ExpandedSubBlockEnd.gif                }

109InBlock.gif                else
110ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
111InBlock.gif                    throw new Exception("最小小数位数不应大于最大小数位数");
112ExpandedSubBlockEnd.gif                }

113ExpandedSubBlockEnd.gif            }

114ExpandedSubBlockEnd.gif        }

115ExpandedSubBlockEnd.gif        #endregion

116InBlock.gif
117ContractedSubBlock.gifExpandedSubBlockStart.gif        重写方法#region 重写方法
118InBlock.gif        protected override void OnKeyPress(KeyPressEventArgs e)
119ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
120InBlock.gif            int editIndex = SelectionStart;         //获取当前编辑位
121InBlock.gif
122InBlock.gif            if (e.KeyChar == (char)Keys.Back) return;   //放行"退格"键
123InBlock.gif
124InBlock.gif            if (e.KeyChar.Equals('.'|| Char.IsNumber(e.KeyChar)) //过滤非数字与非小数点
125ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
126InBlock.gif                if (Text.IndexOf("."> -1)     //是否存在小数点
127ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
128InBlock.gif                    //禁止重复输入小数点
129InBlock.gif                    if (e.KeyChar.Equals('.'))
130ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
131InBlock.gif                        e.Handled = true;
132InBlock.gif                        return;
133ExpandedSubBlockEnd.gif                    }

134InBlock.gif                    else
135ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
136InBlock.gif                        if (SelectedText.Length > 0)
137ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
138InBlock.gif                            return;
139ExpandedSubBlockEnd.gif                        }

140InBlock.gif
141InBlock.gif                        integerLength = Text.IndexOf(".");
142InBlock.gif                        decimalLength = Text.Length - integerLength - 1;
143InBlock.gif
144InBlock.gif                        //控制最大小数位数
145InBlock.gif                        if (decimalLength >= maxDecimalLength && editIndex > Text.IndexOf("."))
146ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
147InBlock.gif                            e.Handled = true;
148InBlock.gif                            return;
149ExpandedSubBlockEnd.gif                        }

150InBlock.gif
151InBlock.gif                        //控制最大整数位数
152InBlock.gif                        if (integerLength >= maxIntegerLength && editIndex <= Text.IndexOf("."))
153ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
154InBlock.gif                            e.Handled = true;
155InBlock.gif                            return;
156ExpandedSubBlockEnd.gif                        }

157ExpandedSubBlockEnd.gif                    }

158ExpandedSubBlockEnd.gif                }

159InBlock.gif                else
160ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{  
161InBlock.gif                    //控制最大整数位数
162InBlock.gif                    integerLength = Text.Length;
163InBlock.gif                    if (integerLength == maxIntegerLength && !e.KeyChar.Equals('.'))
164ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
165InBlock.gif                        e.Handled = true;
166ExpandedSubBlockEnd.gif                    }

167ExpandedSubBlockEnd.gif                }

168ExpandedSubBlockEnd.gif            }

169InBlock.gif            else
170ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
171InBlock.gif                e.Handled = true;
172ExpandedSubBlockEnd.gif            }

173InBlock.gif
174InBlock.gif            base.OnKeyPress(e);
175ExpandedSubBlockEnd.gif        }

176InBlock.gif
177InBlock.gif
178InBlock.gif        protected override void OnLeave(EventArgs e)
179ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
180InBlock.gif            if (Text == null || Text == ""return;
181InBlock.gif
182InBlock.gif            Text = Text.TrimStart('0');
183InBlock.gif
184InBlock.gif            //取整数位数与小数位数
185InBlock.gif            if (Text.IndexOf("."== -1)
186ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
187InBlock.gif                integerLength = Text.Length;
188InBlock.gif                decimalLength = 0;
189ExpandedSubBlockEnd.gif            }

190InBlock.gif            else
191ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
192InBlock.gif                integerLength = Text.IndexOf(".");
193InBlock.gif                decimalLength = Text.Length - integerLength - 1;
194InBlock.gif                
195InBlock.gif                //验证小数位数是否符合最小值(不足补零)            
196InBlock.gif                if (decimalLength < minDecimalLength)
197ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
198InBlock.gif                    Text = Text.PadRight(integerLength + minDecimalLength + 1'0');
199ExpandedSubBlockEnd.gif                }

200ExpandedSubBlockEnd.gif            }

201InBlock.gif
202InBlock.gif            //整数未输自动补零
203InBlock.gif            if (integerLength == 0)
204ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
205InBlock.gif                Text = "0" + Text;
206ExpandedSubBlockEnd.gif            }

207InBlock.gif
208InBlock.gif            //验证整数位数是否符合最小值
209InBlock.gif            if (integerLength < minIntegerLength)
210ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
211InBlock.gif                Focus();
212InBlock.gif                Select(0, integerLength);
213ExpandedSubBlockEnd.gif            }

214InBlock.gif
215InBlock.gif            //验证整数位数是否符合最大值
216InBlock.gif            if (integerLength > maxIntegerLength)
217ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
218InBlock.gif                Focus();
219InBlock.gif                Select(0, integerLength);
220ExpandedSubBlockEnd.gif            }

221InBlock.gif            base.OnLeave(e);
222ExpandedSubBlockEnd.gif        }

223ExpandedSubBlockEnd.gif        #endregion

224ExpandedSubBlockEnd.gif    }

225ExpandedBlockEnd.gif}

226 None.gif

  

转载于:https://www.cnblogs.com/Nelson/archive/2006/06/25/435022.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值