textbox won't accept ENTER in winforms

I have a few controls in a Form, so I read and create a Event in the form that allows me to press ENTER to change the focus to another control. Here's the code:

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);
                    }
                }
            }
        }
    }

In the textbox where it's not working, I have code for only numbers. Here's the code:

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;
            }
        }      
    }

My problem is when I press ENTER in this control nothing happens, like ENTER was never pressed, because the event keypress for this control doesn't fire.

I deleted the control and remade it and the problem remains.

I don't know what is wrong.

Upvotes: 0

Views: 2906

Answers (5)

Wanabrutbeer
Wanabrutbeer

Reputation: 697

If you want to commit your changes with the enter key, assuming you want to update your data bindings, but stay in the control you can use the following code

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;
   }
}

The important part there is WriteValue() which commits the changes before the databind event fires. If you want other controls to update on your form after committing the changes, the ResetCurrentItem() will do this. Also, this code just commits the first binding value, but if you have multiple bindings you can step through them with a foreach loop and WriteValue() all of them or whatever. The TextBox.Select() line just keeps your cursor where you left it after pressing enter.

Upvotes: 0

King King
King King

Reputation: 63377

Your first code is too long and we don't need it. If you want to turn Enter keypress into a Tab switch, using SelectNextControl method of your form. I can see your txtLocal_KeyPress does nothing with switching tab, so how you knew what happened or not. e.Handled only helps suppress the key press event or not. I guess your TextBox has Multiline = true; and you want to suppress the Enter keypress, instead switch to the next control, otherwise you don't have to suppress the Enter keypress in TextBox with Multiline=false. This short code will help you:

private void txtLocal_KeyPress(object sender, KeyPressEventArgs e){
    if(e.KeyChar == 13)//Enter{
        e.Handled = true;
        SelectNextControl(txtLocal, true, true, true, true);
    }
}

That's all to solve your problem. I'm not sure if you know how to register a KeyPress event handler with your TextBox.KeyPress event, so I add this here to help you in that case:

txtLocal.KeyPress += txtLocal_KeyPress;

Upvotes: 1

matzone
matzone

Reputation: 5719

Try this ......

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;

            }
        }      
    }

Upvotes: 0

Imamul Karim Tonmoy
Imamul Karim Tonmoy

Reputation: 584

please use this code on form key down this will give you better performance

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

please make sure you have set tab index on all of your control.

and then unchanged your text changed event code. it will be better if you use a custom text box instead of writing this code on all of your form.

Upvotes: 0

Kenneth Nissel
Kenneth Nissel

Reputation: 151

Try this:

 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;
        }
    }

Upvotes: 0

Related Questions