日期:2014-05-18  浏览次数:20967 次

怎样屏蔽键盘的单撇号,禁止在一个TextBox中输入单撇号!
各位大虾帮帮忙,
怎样屏蔽键盘的单撇号,禁止在一个TextBox中输入单撇号!谢谢!!!

------解决方案--------------------
用正则表达式
------解决方案--------------------
C# code

///   <summary>   
  ///   只能对数值操作的TextBox。   
  ///   </summary>   
  public   class   TextBoxNumEx   :   System.Windows.Forms.TextBox   
  {   
  public   TextBoxNumEx()   
  {   
  //   
  //   TODO:   在此处添加构造函数逻辑   
  //   
  }   
  protected   override   void   WndProc(ref   Message   m)   
  {   
  switch   (m.Msg)   
  {   
  case   0x0102:   
  bool   isSign   =   ((int)m.WParam   ==   45);   
  bool   isNum   =   ((int)m.WParam   >=   48)   &&   ((int)m.WParam   <=   57);   
  bool   isBack   =   (int)m.WParam   ==   (int)Keys.Back;   
  bool   isDelete   =   (int)m.WParam   ==   (int)Keys.Delete;   
  bool   isCtr   =   ((int)m.WParam   ==   24)   ||   ((int)m.WParam   ==   22)   ||   ((int)m.WParam   ==   26)   ||   ((int)m.WParam   ==   3);   
    
  if   (isNum   ||   isBack   ||   isCtr)   
  {   
  base.WndProc(ref   m);   
  }   
  if   (isSign)   
  {   
  if   (this.SelectionStart   !=   0)   
  {   
  break;   
  }   
  base.WndProc(ref   m);   
  break;   
  }   
  if   (isDelete)   
  {   
  if   (this.Text.IndexOf(".")   <   0)   
  {   
  base.WndProc(ref   m);   
  }   
  }   
  if   ((int)m.WParam   ==   1)   
  {   
  this.SelectAll();   
  }   
  break;   
  case   0x0302:   
  IDataObject   iData   =   Clipboard.GetDataObject();//取剪贴板对象   
    
  if   (iData.GetDataPresent(DataFormats.Text))   //判断是否是Text   
  {   
  string   str   =   (string)iData.GetData(DataFormats.Text);//取数据   
  if   (MatchNumber(str))   
  {   
  base.WndProc(ref   m);   
  break;   
  }   
  }   
  m.Result   =   (IntPtr)0;//不可以粘贴   
  break;   
  default:   
  base.WndProc(ref   m);   
  break;   
  }   
  }   
  private   bool   MatchNumber(string   numberText)   
  {   
  if   (string.IsNullOrEmpty(numberText))   
  {   
  return   false;   
  }   
    
  int   index   =   0;   
  string   strNum   =   "-0.123456789";   
    
  index   =   numberText.IndexOf(strNum[0]);   
  if   (index   >=   0)   
  {   
  if   (numberText.Length   ==   1   ||   numberText.IndexOf(strNum[0],   index   +   1)   >   0)   
  {   
  return   false;   
  }   
  }   
  index   =   numberText.IndexOf(strNum[2]);   
  if   (index   !=   -1)   
  {   
  index   =   numberText.IndexOf(strNum[2],   index   +   1);   
  if   (index   !=   -1)   
  {   
  return   false;   
  }   
  }   
  return   true;   
  }   
  }
//参考一下

------解决方案--------------------
HTML code
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script>
        function only()
            {
                    if(event.keyCode==222)event.returnValue=false;
            }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Text1" type="text" onkeydown="only()" /></div>
    </form>
</body>
</html>

------解决方案--------------------
用keyCode值或者码值进行判断
------解决方案--------------------
在validting事件里判断text是否含有'
有的话就执行e.cancel=true;直到输对了否则不许跳出text。
------解决方案--------------------

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ( e.KeyChar == 39)
{
e.Handled = true;
}
}