Talk2Tom11 Posted March 16, 2004 Posted March 16, 2004 I was wondering if there was a code to make this happen: when I am typing in a text box, then hit ENTER, the app acts as if I clicked a picturebox, and then the operation for if I clicked that picturebox is performed. Thanx. Quote
mskeel Posted March 16, 2004 Posted March 16, 2004 In the designer, you have to set KeyPreview property for the form to TRUE. This event will capture the key events: Private Sub myForm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown Console.WriteLine("code = " + e.KeyCode.ToString) Console.WriteLine("data = " + e.KeyData.ToString) Console.WriteLine("value = " + e.KeyValue.ToString) Console.WriteLine("-----------") End Sub This will catch all keys hit by the user. I have found, though, if you have a button selected (tabbed on for example) the form will not catch the enter key specifically. Quote
decrypt Posted March 16, 2004 Posted March 16, 2004 this will catch enter: Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal eventargs As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim KeyAscii As Short = Asc(eventargs.KeyChar) If KeyAscii = 13 Then 'what you want it to do in here.... (operation of when you click picturebox) End If If KeyAscii = 0 Then eventargs.Handled = True End If End Sub Quote
Talk2Tom11 Posted March 16, 2004 Author Posted March 16, 2004 thanks for the last code... it worked... the only problem I am having now is that when I click enter it does the operation but it still sends the cursor down a line in the text box... and I want to to stay there. Quote
techmanbd Posted March 16, 2004 Posted March 16, 2004 try putting the eventsargs.handled = true in the if statement like so below that way it does all the things you want it to but not perform the actual "enter" key and line feed down to the next line Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal eventargs As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim KeyAscii As Short = Asc(eventargs.KeyChar) If KeyAscii = 13 Then 'what you want it to do in here.... (operation of when you click picturebox) eventsarg.handled = true End If If KeyAscii = 0 Then eventargs.Handled = True End If End Sub Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
Talk2Tom11 Posted March 16, 2004 Author Posted March 16, 2004 The code worked great... thank you so much Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.