文本框不接受winforms中的ENTER

时间:2013-06-25 16:16:10

标签: c# winforms

我在Form中有一些控件,所以我在表单中读取并创建一个Event,允许我按 ENTER 将焦点更改为另一个控件。这是代码:

private void frSeleccionEL_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            Control nextControl = this.GetNextControl(ActiveControl, !e.Shift);
            if (nextControl != null)
            {
                e.Handled = true;
                if (!(nextControl is Label))
                {
                    nextControl.Focus();
                }
                else //the next control is currently a label
                {
                    nextControl = this.GetNextControl(ActiveControl, true);
                    while (nextControl != null)
                    {
                        nextControl.Focus();
                        if (!(nextControl is Label))
                        {
                            break;
                        }
                        nextControl = this.GetNextControl(ActiveControl, true);
                    }
                }
            }
        }
    }

在不起作用的文本框中,我只有数字代码。这是代码:

private void txtLocal_KeyPress(object sender, KeyPressEventArgs e)
    {
        //Para obligar a que sólo se introduzcan números
        if (Char.IsDigit(e.KeyChar))
        {
            e.Handled = false;
        }
        else
        {
            if (Char.IsControl(e.KeyChar)) //permitir teclas de control como retroceso
            {
                e.Handled = false;
            }
            else
            {
                //el resto de teclas pulsadas se desactivan
                e.Handled = true;
            }
        }      
    }

我的问题是当我在此控件中按 ENTER 时没有任何反应,例如 ENTER 从未被按下,因为此控件的事件按键不会触发。

我删除了控件并重新制作,问题仍然存在。

我不知道出了什么问题。

5 个答案:

答案 0 :(得分:1)

您的第一个代码太长,我们不需要它。如果要使用表单的SelectNextControl方法将Enter按键转换为Tab开关。我可以看到你的txtLocal_KeyPress对切换标签没有任何作用,所以你怎么知道发生了什么。 e.Handled仅有助于抑制按键事件。我猜您的TextBoxMultiline = true;,并且您想要取消Enter按键,而是切换到下一个控件,否则您不必在TextBox中使用{{来禁止输入按键1}}。这个简短的代码将帮助您:

Multiline=false

这就是解决问题的方法。 我不确定你是否知道如何用TextBox.KeyPress事件注册private void txtLocal_KeyPress(object sender, KeyPressEventArgs e){ if(e.KeyChar == 13)//Enter{ e.Handled = true; SelectNextControl(txtLocal, true, true, true, true); } } 事件处理程序,所以我在这里添加它来帮助你:

KeyPress

答案 1 :(得分:0)

试试这个:

 private void txtLocal_KeyPress(object sender, KeyPressEventArgs e)
    {
        //Para obligar a que sólo se introduzcan números
        if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) )
        {
            e.Handled = true;
        }
    }

答案 2 :(得分:0)

请在表单键上使用此代码,这样可以提供更好的性能

private void frSeleccionEL_KeyDown(object sender, KeyEventArgs e)
{
  if (e.KeyCode == Keys.Enter)
    {
       ProcessTabKey(true);
     }
}

请确保您已在所有控件上设置了标签索引。

然后将文本更改为事件代码。如果您使用自定义文本框而不是在所有表单上编写此代码,那将会更好。

答案 3 :(得分:0)

试试这个......

private void txtLocal_KeyPress(object sender, KeyPressEventArgs e)
    {
        //Para obligar a que sólo se introduzcan números
        if (Char.IsDigit(e.KeyChar) || asc(e.KeyChar)==13)
        {
            e.Handled = false;
        }
        else
        {
            if (Char.IsControl(e.KeyChar)) //permitir teclas de control como retroceso
            {
                e.Handled = false;
            }
            else
            {
                //el resto de teclas pulsadas se desactivan                
                e.Handled = true;

            }
        }      
    }

答案 4 :(得分:0)

如果要使用回车键提交更改,假设您要更新数据绑定,但保留在控件中,则可以使用以下代码

private void TextBox_KeyPress(object sender, EventArgs e)
{
   if(e.KeyCode == (char)Keys.Enter)
   {
      TextBox.DataBindings[0].WriteValue();
      dataBindingSource.ResetCurrentItem();
      TextBox.Select(TextBox.Text.Length, 0);
      e.Handled = true;
   }
}

WriteValue()的重要部分是在数据绑定事件触发之前提交更改。如果您希望在提交更改后在表单上更新其他控件,则ResetCurrentItem()将执行此操作。此外,此代码只提交第一个绑定值,但如果您有多个绑定,则可以使用foreach循环和WriteValue()全部或其他任何步骤来逐步执行它们。按下回车键后,TextBox.Select()行只会将光标保持在离开它的位置。

相关问题