Kapalic

How can I move the focus to a button when I press the enter key in the Text Box

Thnx!




Re: Windows Forms General Tapping the Enter Key

Sean Hederman

In your KeyPress handler, when the button has been pressed just call:

MyButton.Focus();






Re: Windows Forms General Tapping the Enter Key

Dav? S. A???????

Make sure your forms KeyPreview property is set to true, then create a KeyPressed event for your textbox.

if (e.KeyChar == (char)Keys.Enter)
{
MyButton.Focus();
}





Re: Windows Forms General Tapping the Enter Key

rHasan

Dave, what will be the vb version of the code I use the following code -

If e.KeyChar = Char(Keys.Enter) Then

btnSave.Focus()

End If

and it says - "Char is a type, and can not be used as an expression.

Thnx!






Re: Windows Forms General Tapping the Enter Key

Dav? S. A???????

VB is not good with type casts, so try using the conversion class.

if e.KeyChar = Convert.ToChar(Keys.Enter) Then
btnSave.Focus()
End If





Re: Windows Forms General Tapping the Enter Key

Kapalic

Hi,

At last I've got a way to move focus between the text boxes when the enter key is pressed -

Private Sub KeyPressEvent(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress

Dim T As TextBox

T = CType(sender, TextBox)

If e.KeyChar = ControlChars.Cr Then

Select Case T.Name

Case "TextBox1"

TextBox2.Focus()

Case "TextBox2"

TextBox1.Focus()

End Select

End If

End Sub

 Thank you guys for your co-operation!

Kapalic