Home » Category » DotNet Visual Basic

DotNet Visual Basic: Yet another question

104| Tue, 04 Dec 2007 00:46:00 GMT| kidz14| Comments (13)
how would i make it so that when i press enter in a txt box it dose lets say LoadStats()

this is what i have dosen't work though

Private Sub txtname1_Click(ByVal sender As System.Object, ByVal e As__
System.EventArgs) Handles txtname1.Click
If txtname1.Pressed = ("{ENTER}") Then
LoadStats()
Else
End If
End Sub

Keywords & Tags: dotnet, visual basic, vb, .net

URL: http://dotnet.itags.org/dotnet-visual-basic/172/
 
«« Prev - Next »» 13 helpful answers below.
You need to handle a keyboard event instead of a mouse event if you want to check for a particular key. Handle the KeyDown event and compare the e.KeyCode property to Keys.Enter. Intellisense will help you and you obviously already know how to create an event handler in the first place.

jmcilhinney | Tue, 04 Dec 2007 14:32:00 GMT |

I think you're going to want to use the KeyPress event rather than the click event.

Some might suggest using the LostFocus event, depending on what you are doing, but I would stay away from that event entirely, it is unreliable in my experience.

shaggyhiker | Tue, 04 Dec 2007 14:33:00 GMT |

Oh yeah, KeyDown, that's it.

shaggyhiker | Tue, 04 Dec 2007 14:34:00 GMT |

this is what i have tried

Private Sub txtname1_Enter(ByVal sender As Object, ByVal e As__
System.EventArgs) Handles txtname1.Enter
LoadStats()
End Sub

and

Private Sub txtname1_KeyDown(ByVal sender As Object, ByVal e As__
System.Windows.Forms.KeyEventArgs) Handles txtname1.ModifiedChanged
LoadStats()
End Sub

i think keydown don't work because it's if i hold down the key i want it so that as soon as i press enter it dose the LoadStat() sub

and it don't work little more detail plz

kidz14 | Tue, 04 Dec 2007 14:35:00 GMT |

Your code indicates that you are handling the ModifiedChanged event and not the KeyDown event. It doesn't matter what the procedure is called, only what's in the Handles clause. Also, I've already said that you need to test the e.KeyCode property to make sure that ENTER was the key that was depressed and there's no sign of that in your code.

jmcilhinney | Tue, 04 Dec 2007 14:36:00 GMT |

Look that one up, it has nothing to do with keypresses, but is the first event sent to the textbox when it gets focus.

What you want is to handle the KeyDown event. If you put the cursor in the sub you posted, you should be able to pick it off a list in the dropdown on the right above the code page. The keypress event receives a KeyPressEventArgs argument that has a KeyCode member. That's what you need.

KeyDown is also an option, but if the user were to hold down the key, the KeyDown event should fire every time the key repeats (according to MSDN).

shaggyhiker | Tue, 04 Dec 2007 14:37:00 GMT |

Hey, no fair, you edited your post as I was writing.

shaggyhiker | Tue, 04 Dec 2007 14:38:00 GMT |

okay for all you people that waisted my time here the anwser if you all needed it

Private Sub txtname1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtname1.KeyPress
If e.KeyChar = Chr(13) Then
LoadStats()
End If
End Sub
End Class

by the way i was just kidding i love you all for all your help by the way jmcilhinney i think you actually tried to tell me to do that but i didn't quit understand you should try pasting code every now and then

kidz14 | Tue, 04 Dec 2007 14:39:00 GMT |

And yet you still chose to do it differently: Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
LoadStats()
End If
End SubIf you use the KeyPress event then if the user holds the Enter key down the code will be executed multiple times, whereas with the KeyDown event it will be executed only once. I choose not to post code in many situations that aren't too complex because I want to encourage people to think for themselves a little. I did say handle the KeyDown event and compare e.KeyCode to Keys.Enter. That seems pretty explicit to me.

jmcilhinney | Tue, 04 Dec 2007 14:40:00 GMT |

i know but i didn't quite know how to use the e. thingy untill now but thanks learned something new cause of you :D

kidz14 | Tue, 04 Dec 2007 14:41:00 GMT |

This seems surreal. I was reading something on MSDN about what I thought was KeyPress, but when I went back to confirm it, I have a different set of text. The current thing I am reading says that KeyPress only receives character keys, so Enter would not be used. KeyDown is the ONLY one that will work. I wonder what I was reading before that showed that KeyDown would repeat, and KeyPress would not?

I believe Chr() is in the VisualBasic namespace, which some people here are opposed to. It is also ASCII, while .NET is fundamentally Unicode (though you can do either one, the emphasis is on Unicode). For those reasons, jmcilhinney (whoes name I will butcher in the spelling, no doubt) has a slightly better solution.

shaggyhiker | Tue, 04 Dec 2007 14:42:00 GMT |

keypress actually dose work so ya check b 4 you talk

kidz14 | Tue, 04 Dec 2007 14:43:00 GMT |

I don't recall posting that the KeyPress event wouldn't work. What I said was that it will be raised multiple times if the user holds the key down, whereas KeyDown will only be raised once each time the key is depressed. If it is acceptable or desirable that your LaodStats method be called multiple times in that circumstance then using the KeyPress event is fine. If not then you should be handling the KeyDown event as I suggested. Also, to stick with Unicode you should use ChrW or, preferably, Convert.ToChar.

jmcilhinney | Tue, 04 Dec 2007 14:45:00 GMT |

DotNet Visual Basic Hot Answers

DotNet Visual Basic New questions

DotNet Visual Basic Related Categories