頁面上有兩個textbox和一個button.
每當我按回車時就自動執行按鈕事件。
現在請問怎麼寫代碼實現如下功能:在第一個textbox按回車後。不執行按鈕事件而是輸入焦點到第二個textbox。在第二個textbox寫完後按回車再執行按鈕事件。

解决方案 »

  1.   

    第一个TextBox的PressKey事件,得到回车之后设置第二个TextBox的焦点,记得第一个的事件中e.Cancle = true。另外,你还需要TextBox能够接收回车,一般来说单行的好像不行,需要多行。
    还有一个办法是直接截获窗体的按键事件,同样是PressKey。
    我没有实际实验过,只是提个思路,不正确莫怪……
      

  2.   

    第一:把窗口属性的按钮==回车的属性设置为False
    第二:在第一个TextBox1中Leav事件中写TxtBox2.fou..()获取焦点的方法
    第三:在第二个TextBox2中的PassClick侦听是否按下回车,如果是,就调用按钮事件下的方法
      

  3.   

    private void textBox1_KeyUp(object sender, KeyEventArgs e)
            {
                if ((Keys)e.KeyValue == Keys.Enter)
                {
                    this.textBox2.Focus();
                }
            }        private void textBox2_KeyUp(object sender, KeyEventArgs e)
            {
                if ((Keys)e.KeyValue == Keys.Enter)
                {
                    button1_Click(null, null);
                }
            }        private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("akldfjalkfjdlk");
            }
      

  4.   

    顶一下吧.
    接点分
    aspx.cs页面
    页面加载时
    if(!IsPostBack)

       this.TextBox1.Attributes.Add("onkeydown", "SetTab();");
            this.TextBox2.Attributes.Add("onkeydown", "SetTab();");
    }aspx页面
    <script language="javascript">
    function SetTab()
    {
      if(event.keyCode == 13)
      {
        event.KeyValue = 9;
      }
    }
    </script>
      

  5.   

    lizhizhe2000(彬彬) 与  xiaohutushen(程序人生) 方法均可实现...
    支持一下....
      

  6.   

    楼主说的是WEB页面吧。那就得用
    xiaohutushen(程序人生)
    的方法。
      

  7.   

    彬彬說的是windows頁面。而我的是web頁面。
    xiaohutushen(程序人生)的方法我試了一下不好用啊。在第一個textbox按回車後還是執行按鈕事件
      

  8.   

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    <script type="text/javascript">
    function doone()
    {
     if(event.keyCode == 13)
      {
        document.all.textbox2.focus();
      }
    }
    function dotwo()
    {
    if(event.keyCode==13)
      {
      btnClick();
      }
    }
    function btnClick()
    {
      window.alert('Ok,it works!'); 
     }
    </script>
    </head>
    <body>
    <form name="form1" method="post" action="">
      <input name="textbox1" type="text" id="textbox1" onKeyDown="doone()">
      <input name="textbox2" type="text" id="textbox2" onKeyDown="dotwo()">
      <input type="button" name="btn" value="提交" onClick="btnClick()">
    </form>
    </body>
    </html>
      

  9.   

    function SetTab()
    {
      event.cancelBubble=true;
      if(event.keyCode == 13)
      {
        event.KeyValue = 9;
      }
    }
    你可以在这个事件中加上event.cancelBubble=true;