TxtBox enter event

JohnM

Well-known member
Joined
Jul 2, 2004
Messages
116
Location
Massachusetts
Programming Experience
1-3
TxtBox enter event [RESOLVED]

I would appreciate your help on this. I can't believe I can't see the error in this code. I'm new at VB but I didn't think I would miss this. I have been looking at this for a few hours.. I just can't see the error.
I have a button that when clicks sends the focus to this textbox (txbMPG). But when I enter a value in this txbMPG, it acts as if the ENTER event has already happened. It allows me to enter a value, but when I hit enter, I get a beeping sound. I put in that Messagebox to see the timing of the ENTER routine. The message box turns on when the focus is put on the TxbMPG, so it looks like the ENTER event of the txbMPG is triggered when the textbox txbMPG gets focus.

I need it to trigger the enter when I hit the ENTER button for the txbMPG not when the focus on the txbMPG is triggered.

I appreciate very much your time on looking at this.


Private
Sub txbMPG_Enter(ByVal sender AsObject, ByVal e As System.EventArgs) Handles txbMPG.Enter
y8 =
CInt(Val(txbMPG.Text))

If y8 <> 0 Then

btnCalculateGas.Focus()

Else

MessageBox.Show("ENTERING Y8 equals..." & y8, "value of y8")

txbMPG.Focus()

EndIf

EndSub

 
Last edited by a moderator:
Yes the Enter event occurs when the user 'Enters' the textbox (when the user clicks into it).
As the project attached above shows, you need to handle one of the key events (keyDown, keyUp, or KeyPressed) and check to see if the key pressed is the Enter key.
 
Paszt and Kulrom,

Thank you very much for your quick answers. I will try this code.

Please bear with me, I still don't understand why when I put the focus on the textbox that the textbox acted as if the user had hit "enter".

Here's what I mean:
When the focus was sent to the textbox, right, the messagebox immediately popped up with that message ENTERING Y8 equals..." & y8, "value of y8".

It appeared to do it because the value of Y8 was "0". But how could this be when it never gave me the chance to enter anything in the textbox. After the message popped up, I hit enter, the message went away and the focus was on the textbox. I then entered a value but when I hit "enter", it made a "beep" sound and wouldn't allow me to enter it.

How is this possible? Thank you for your patience.

John M




 
i think you should be using the LostFocus event if you're checking to see if it's contents equals a particular value or not
 
Back
Top