zoukankan      html  css  js  c++  java
  • TextBox输入限制

    1.只能输入数字:
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
                {
                    if (e.KeyChar == '.')
                    {
                        e.Handled = true;
                    }
                    else
                        e.Handled = true;
                }
                else
                {
                    if (e.KeyChar <= 31)
                    {
                        e.Handled = false;
                    }
                    else if ((e.KeyChar >= '0') && (e.KeyChar <= '9'))
                    {
                        if (((TextBox)sender).Text.ToString() != "")
                        {
                            if (Convert.ToDouble(((TextBox)sender).Text) == 0)
                            {
                                if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                                {
                                    e.Handled = false;
                                }
                                else
                                {
                                    e.Handled = true;
                                }
                            }
                        }
                        else
                        {
                            e.Handled = false;
                        }
                    }
                }
            }
    2.只能输入小数
            private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
                {
                    if (e.KeyChar == '.')
                    {
                        if (((TextBox)sender).Text.Trim().IndexOf('.') > -1 || ((TextBox)sender).Text.Trim() == "")
                            e.Handled = true;
                    }
                    else
                        e.Handled = true;
                }
                else
                {
                    if (e.KeyChar <= 31)
                    {
                        e.Handled = false;
                    }
                    else if ((e.KeyChar >= '0') && (e.KeyChar <= '9'))
                    {
                        if (((TextBox)sender).Text.ToString() != "")
                        {
                            if (Convert.ToDouble(((TextBox)sender).Text) == 0)
                            {
                                if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                                {
                                    e.Handled = false;
                                }
                                else
                                {
                                    e.Handled = true;
                                }
                            }
                        }
                        else
                        {
                            e.Handled = false;
                        }
                    }
                }
            }
    3.单引号用"`"来代替:
            private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == Convert.ToChar("'"))
                {
                    ((TextBox)sender).Text = ((TextBox)sender).Text + "`";
                    ((TextBox)sender).SelectionStart = Convert.ToInt32(((TextBox)sender).Text.Length);
                    e.Handled = true;
                }
            }
  • 相关阅读:
    集合-ArrayList 源码解析
    >>《配色设计原理.pdf》
    >>《[美国视觉设计学院用书:完成设计(从理论到实践)].(萨马拉).温迪等.扫描版.pdf》
    《Photoshop智能手机APP界面设计.pdf》
    《贴心设计:打造高可用性的移动产品》——5.平板电脑和电子书阅读器
    3H Buy a Ticket —— Dij
    最短路板子
    3E A Simple Problem —— 数论
    3D 城池攻占 —— 左偏树
    3A Least Cost Bracket Sequence —— 贪心
  • 原文地址:https://www.cnblogs.com/MFxxk/p/1528645.html
Copyright © 2011-2022 走看看