|
-
Jul 18th, 2008, 08:13 AM
#1
Thread Starter
Member
[RESOLVED] [2008] Quick question about pressing enter event
If a user presses enter in a textbox, i want to activate a button that's next to it. What's the easiest way to do this?
-
Jul 18th, 2008, 08:20 AM
#2
Re: [2008] Quick question about pressing enter event
Handle the KeyDown event of the TextBox and call the PerformClick event of the Button.
-
Jul 18th, 2008, 08:50 AM
#3
Re: [2008] Quick question about pressing enter event
Or assign that button to your form's AcceptButton property.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 18th, 2008, 10:51 AM
#4
Re: [2008] Quick question about pressing enter event
Check to see if "Enter" was pressed, then do whatever you want.
vb Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
MsgBox "Enter"
Button1.SetFocus
Else
MsgBox "Not Enter"
End If
End Sub
-
Jul 18th, 2008, 12:32 PM
#5
Re: [2008] Quick question about pressing enter event
 Originally Posted by wes4dbt
Check to see if "Enter" was pressed, then do whatever you want.
vb Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
MsgBox "Enter"
Button1.SetFocus
Else
MsgBox "Not Enter"
End If
End Sub
That's VB6 code and it will not work on VB.Net application.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 18th, 2008, 02:17 PM
#6
Thread Starter
Member
Re: [2008] Quick question about pressing enter event
Thanks all. Well this is my solution
Code:
Private Sub TextBoxUsersName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxUsersName.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Windows.Forms.Keys.Return) Then
ButtonUsersName_Click(TextBoxUsersName, e)
End If
End Sub
But I was hoping that such a common function would be supported as something that's precoded as an option, like the way you can set a combobox to suggest completions. AcceptButton is a good idea, but I have many textboxes and buttons on the same form. If I suggest Microsoft to add this functionality to the next VS would they do it?
Thanks all.
Last edited by Daarklord; Jul 18th, 2008 at 02:21 PM.
0xABADA55D00D
-
Jul 18th, 2008, 02:20 PM
#7
Re: [2008] Quick question about pressing enter event
You should replace this line
Code:
ButtonUsersName_Click(TextBoxUsersName, e)
With this
Code:
ButtonUsersName.PerformClick()
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 18th, 2008, 02:23 PM
#8
Thread Starter
Member
Re: [2008] Quick question about pressing enter event
Any reason why PerformClick is better?
-
Jul 18th, 2008, 02:37 PM
#9
Re: [2008] Quick question about pressing enter event
Sorry I thought I was in the VB6 forum.
I guess I'll have to get my eyes checked.
-
Jul 18th, 2008, 02:43 PM
#10
Thread Starter
Member
Re: [2008] Quick question about pressing enter event
 Originally Posted by wes4dbt
Sorry I thought I was in the VB6 forum.
I guess I'll have to get my eyes checked.
Haha no worries. I recognized the VB6-ishness of it .
-
Jul 18th, 2008, 02:46 PM
#11
Re: [2008] Quick question about pressing enter event
 Originally Posted by Daarklord
Any reason why PerformClick is better?
It's the proper way to trigger a button click in code. While calling the method directly and suppy it with parameters (as you're doing) does work, it's generally considered to be bad practice.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 18th, 2008, 02:55 PM
#12
Thread Starter
Member
Re: [2008] Quick question about pressing enter event
And that concludes my quick question about pressing enter.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|