在一个TextBox 中,当按Enter键后,如何才能截获消息,让TextBox 中没有回车符

解决方案 »

  1.   

    this.textBox_Test.KeyDown +=new KeyEventHandler(textBox_Test_KeyDown);private void textBox_Test_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Enter)
      {
        // todo...
      }
    }
      

  2.   

    >>>irvine007(┣━┫Rvine)
    这样虽然能的到Enter键的处理事件,但回车符照样添加到了TextBox中,我想当回车按下后textBox中并不出现回车符
      

  3.   

    在irvine007(┣━┫Rvine)的方法中
    这样取值;;;textbox.text.substring(0,testbox.text.length-1);
      

  4.   

    >>>jiang8282 
    在KeyDown or KeyPress 事件中,Enter 键值还没有负给TextBox ,在TextBox中根本取不到
      

  5.   

    TextBox 所在Form:private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    if (e.KeyCode == Keys.Enter) e.Handled = true;
    }this.KeyPreview = true;
      

  6.   

    private void txtOther4_TextChanged(object sender, EventArgs e)
    {
    txtOther4.Text = txtOther4.Text.Replace("\r\n", "");
    }
    放在TextChanged事件中上面程序我试了,可以用,但有一个副作用,光标会跑到文本最前面,你改一下就好
      

  7.   

    如果是WINDOWS APPLICATION,上面提供的方法已经可以了,
    如果是在asp.net中,在一个文本框内按回车触发指定按钮的事件可以如下做:
    假设:
    <asp:TextBox id="TextBox1" runat="server" Width="240px"></asp:TextBox>
    <asp:Button id="ButtonOK" runat="server" BorderWidth="1px" BorderColor="Purple" BorderStyle="Solid" Text="Search Site"></asp:Button>  解决方法:
    在.aspx页面中添加:
    <SCRIPT LANGUAGE="javascript">
    function EnterKeyClick(button) 
    {    
     if (event.keyCode == 13) 
     {        
      event.keyCode=9;
      event.returnValue = false;
      document.all[button].click(); 
     }
    }
    </SCRIPT> 
    在Page_Load事件中添加:
    TextBox1.Attributes.Add("onkeydown","EnterKeyClick('ButtonOK');");
    可以在ButtonOK的事件中写你的处理程序,比如说弹出不能有回车的对话框啦等等。