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;
                }
            }
  • 相关阅读:
    我们的路该如何走?-序言
    [转贴]给想立志入行网络或已经初入行的朋友的建议(三)
    [转贴]给想立志入行网络或已经初入行的朋友的建议(二)
    为应用程序池defaultAppPool提供服务的进程在于world wide web publishing服务通信时遇到致命错误 进程id为1356. 数据字段包含错误号
    this.get_element .style为空或不是对象
    将linq查询转换为DataTable对象——学习笔记
    ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限。
    常用改变选中行颜色
    DataTable写入Excel中 用Excel标准格式
    导出Excel出错:检索 COM 类工厂中 CLSID 为 {0002450000000000C000000000000046} 的组件失败
  • 原文地址:https://www.cnblogs.com/MFxxk/p/1528645.html
Copyright © 2011-2022 走看看