zoukankan      html  css  js  c++  java
  • 2019-3-22c# TextBox只允许输入数字,禁用右键粘贴,允许Ctrl+v粘贴数字

    TextBox 禁止复制粘贴 ShortcutsEnabled =false

    TextBox只允许输入数字,最大长度为10

    复制代码
           //TextBox.ShortcutsEnabled为false  禁止右键和Ctrl+v
            private void txtNumber_KeyPress(object sender, KeyPressEventArgs e)
            {
                //只允许输入数字,粘贴数字
                if (!(Char.IsNumber(e.KeyChar) || e.KeyChar == (char)8))
                {
                    e.Handled = true;
                }
            }
            //允许Ctrl+v粘贴数字
            private void txtNumber_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.KeyData == (Keys.Control | Keys.V))
                {
                    if (Clipboard.ContainsText())
                    {
                        try
                        {
                            Convert.ToInt64(Clipboard.GetText());  //检查是否数字
                            ((TextBox)sender).SelectedText = Clipboard.GetText().Trim(); //Ctrl+V 粘贴  
                            if (((TextBox)sender).TextLength > 10)
                            {
                                ((TextBox)sender).Text = ((TextBox)sender).Text.Remove(10); //TextBox最大长度为10  移除多余的
                            }
                        }
                        catch (Exception)
                        {
                            e.Handled = true;
                            //throw;
                        }
                    }
                }
            }
    复制代码

    第二种办法:

    1、先创建一个ToolStripMenuItem,添加 菜单列表 “复制” 和 “粘贴” 。

    2、设置同一事件txtToolStripMenuItem_Click

    复制代码
           //复制粘贴处理
            private void txtToolStripMenuItem_Click(object sender, EventArgs e)
            {
                string txtname = ((ToolStripMenuItem)sender).Text;
                if (txtname.Equals("复制"))
                {
                    SendKeys.SendWait("^c");  //Ctrl + C
                }
                if (txtname.Equals("粘贴"))
                {
                    SendKeys.SendWait("^v");  //Ctrl + V
                }
            }
    复制代码

    3、TextBox 属性 ContextMenuStrip 绑定 ToolStripMenuItem

    4、TextBox添加事件txtOrderID_KeyPress

    复制代码
           //只允许输入数字,粘贴数字
            private void txtOrderID_KeyPress(object sender, KeyPressEventArgs e)
            {
                //只允许输入数字,粘贴数字
                if (!(Char.IsNumber(e.KeyChar) || e.KeyChar == (char)8 || e.KeyChar == (char)3 || e.KeyChar == (char)22))
                {
                    e.Handled = true;
                }
                try
                {
                    if (e.KeyChar == (char)22)
                    {
                        Convert.ToInt64(Clipboard.GetText());  //检查是否数字
                        Clipboard.SetText(Clipboard.GetText().Trim()); //去空格
                    }
                }
                catch (Exception)
                {
                    e.Handled = true;
                    //throw;
                }
    复制代码

    5、ToolStripMenuItem 菜单 “复制” 和 “粘贴” 创建 同一Click事件。

    复制代码
           //复制粘贴处理
            private void txtToolStripMenuItem_Click(object sender, EventArgs e)
            {
                string txtname = ((ToolStripMenuItem)sender).Text;
                if (txtname.Equals("复制"))
                {
                    SendKeys.SendWait("^c");  //Ctrl + C
                }
                if (txtname.Equals("粘贴"))
                {
                    SendKeys.SendWait("^v");  //Ctrl + V
                }
            }
    复制代码
  • 相关阅读:
    bind(),live(),delegate(),on()绑定事件方式
    3 《锋利的jQuery》jQuery中的DOM操作
    表格功能
    5.2 《锋利的jQuery》jQuery对表格的操作(选项卡/换肤)
    4.1 《锋利的jQuery》jQuery中的事件
    5.1 《锋利的jQuery》jQuery对表单的操作
    4.2 《锋利的jQuery》jQuery中的动画
    2 《锋利的jQuery》jQuery选择器
    回流(reflow)与重绘(repaint)
    BOX2d绘制曲线
  • 原文地址:https://www.cnblogs.com/liuqifeng/p/10577751.html
Copyright © 2011-2022 走看看