脚踏实地,一步一个脚印,前进......

不抛弃,不放弃

导航

TextBox输入到3位数字就直接加个点

今天CSDN上有个朋友问了这样的一个问题
"如果输入的是非数字或“.”,则不让他输入。如果输入到3位数字就直接加个点"
而且要求是在Win下面的,很多朋友会说用到正则表达式,但是本人就正则实在是太菜了,所以就用自己的办法来实现:
1:先定义一个类级别的变量 private int _node =1;
                                                 private int _lostV=1;
2:然后直接在TextBox的KeyPress事件里面来实现,代码如下
public void TextBox1.KeyPress(object sender,System.Windows.Forms.KeyPressEventArgs e)
{
      if(e.KeyChar >=48 && e.KeyChar<=57)
      {
         e.Handled =false;
         int _nodeCount= TextBox2.Text.Split('.').Lenght;
         _node = _nodeCount;
         _lostV = _node -1 ;
         if(TextBox1.TextLenght>0 && TextBox1.TextLenght % (3*_node + _lostV) ==0)
        {
            TextBox1.Text = TextBox1.Text + ".";
            TextBox1.SelectionStart = TextBox1.TextLenght;
         }
      }
}



哈哈,不知道还有没有其他好的办法呢????

posted on 2008-06-17 16:01  沉默的人  阅读(265)  评论(0编辑  收藏  举报