tags:

views:

3196

answers:

7

Hello,

I've several textboxes. I would like to make the Enter button act as Tab. So that when I will be in one textbox, pressing Enter will move me to the next one. Could you please tell me how to implement this approach without adding any code inside textbox class (no override and so on if possible)?

Question is about C# and .NET

+4  A: 

I found this

http://www.surpluscode.com/2006/06/15/winforms-process-enter-key-press-as-tab-via-sendkeyssend/

Pharabus
This feels a bit hacky to me :)
arul
It sounds hacky, but this worked for me. GetNextControl() don't take into account that some controls on Form can be invisible or disabled. I've tried to do something like this, but it's not so easy. So I'll use this solution. Thank you Pharabus!
tomaszs
A: 

Taking a wild guess:

// on enter event handler
parentForm.GetNextControl().Focus();
arul
A: 

I would combine what Pharabus and arul answered like this:

private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == ‘\r’)
{
e.Handled = true;
parentForm.GetNextControl().Focus()
}
}

Let me know if this helps! JFV

JFV
GetNextControl() take 2 parameters...
Daok
+4  A: 

You can write on the keyDown of any control:

        if (e.KeyCode == Keys.Enter)
        {

            if (this.GetNextControl(ActiveControl, true) != null)
            {
                e.Handled = true;
                this.GetNextControl(ActiveControl, true).Focus();

            }
        }

GetNextControl doesn't work on Vista.

To make it work with Vista you will need to use the code below to replace the this.GetNextControl...:

System.Windows.Forms.SendKeys.Send("{TAB}");
Daok
+1  A: 

You don't need to make an "enter event handler"

All you need to do is make a "central" KeyDown event:

example

private void General_KeyDown(object sender, KeyPressEventArgs e)
 {
 if (e.KeyCode == Keys.Enter)
        {

            if (this.GetNextControl(ActiveControl, true) != null)
            {
                e.Handled = true;
                this.GetNextControl(ActiveControl, true).Focus();
            }
        }
}

Then all you have to do is go to designer select all textboxes you wish to cycle through with EnterKey (select them by holding down Ctrl and clicking on textbox with the mouse) then go to Events(thunder like button), search Keydown event and type inside General_KeyDown. Now all your selected Textboxes will have the same keydown event :) This makes everything muuuuch much easier, cause imagine a form with 100 textboxes and you want to cycle through all with enter.... making an apart event for each texbox is... well not a proper way to make a program, it ain't neat. Hope it helped!!

Blockquote

A: 

i need a smilar thing but instead of moving tabs i need to accept its contents like pressing a buttom(i already have a button)

If you have a follow up question you should post it asa new question, not as an answer to this old question.The "Ask Question" button is in the top right.
sth
A: 

For those of you that code in vb...

Public Class NoReturnTextBox
    Inherits System.Windows.Forms.TextBox

    Const CARRIAGE_RETURN As Char = Chr(13)

    ' Trap for return key....
    Private Sub NoReturnTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

        If e.KeyChar = CARRIAGE_RETURN Then
            e.Handled = True
            System.Windows.Forms.SendKeys.Send(vbTab)
        End If

    End Sub

End Class
Mac