Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keypress Event Doesn't Work Until Second Press

Status
Not open for further replies.

bigmelon

MIS
Sep 25, 2003
114
US
I programmed an event so that when the user press the ' it put in the current time. On the first press of the key, it just places a blank row below the one i am starting and remains null. When the key is pressed a second time, it successfully inserts the current time. Does anyone know how to get this to work the first time the key is pressed? Also, if the datagrid has 2 blank rows initially, instead of just the one, then the keypress works the first time.

Here is a little code:

Private Sub HandleKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)

If e.KeyChar = "'" Then
Me.TextBox.Text = FormatDateTime(TimeOfDay, DateFormat.ShortTime)
e.Handled = True
End If

End Sub

Thanks in advance,
Melon
 
try it on keydown instead of keypress

The events fire in this order
keydown
keypress
keyup


Sweep
...if it works dont mess with it
 
Sweep

I can't use the e.keychar event with the keydown because the keydown doesn't use the keypressevent handler, its uses the keyevent handler. So I don't believe this is possible unless you know a workaround?

Thanks in advance,

Melon
 
If you're trapping for a single quote I think you can use the following

Code:
Private Sub comTextBoxBase_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

Select Case e.KeyCode
      case Keys.OemQuotes
         'Your code here
end select




Sweep
...if it works dont mess with it
 
Sweep

Your code does in fact work, however it allows the singe quote to show up in the textbox, and still takes 2 keypresses to enter the current time :-(

Thanks a lot for the ideas though! You don't have any other do you?

Thanks again,
Melon
 
Code:
'Public Event QuotePressed()

Select Case e.KeyCode
      case Keys.OemQuotes
         'RaiseEvent QuotePressed() 
         'Your code here
         e.Handled = True
end select

You could also try raising a QUOTEPRESSED Event
Then you have a custom Event in your Control where to put your code for populating the text box.


Sweep
...if it works dont mess with it
 
Sweep

I think what i need to do is create a blank row when the datagrid loads, and then it would work. Do you know how this could be done?

Thanks,

Melon
 
Im gonna go nuts!!!

Help people!! please..
What im trying to di its that.. whenever i type a text at a textbox and press Enter, some other textbox gets focused
and then... that other textbox once i press a # and press Enter... fires a function...
here is the code:

Private Sub txtfrecval_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtfrecval.KeyUp
If e.KeyCode = 13 Then
txtfrecval.Focus()
e.Handled = True
End If
End Sub

Private Sub txtvalfrec_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtvalfrec.KeyUp
If e.KeyCode = Keys.Enter Then
Me.frecuencias()
e.Handled = True
End If
End Sub

Any clues dudes? thanks a lot!
 
try using the KeyDown event. If you wait for KeyUp that means the [enter] has already been processed by the form(Default button, or in your case, the beep).

-Rick

----------------------
 
Didnt work though...

The second textbox does the function.. but the first one wont give focus to the second textbox...
i have no clue what to do folks.. :°(

here is the final code... keeps beeping at both textboxes when i press Enter... :(

Private Sub txtfrecval_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtfrecval.KeyDown
If e.KeyCode = Keys.Enter Then
txtfrecval.Focus()
e.Handled = True
End If
End Sub
Private Sub txtvalfrec_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtvalfrec.KeyDown
If e.KeyCode = Keys.Enter Then
Me.frecuencias()
txtfrecval.Clear()
txtvalfrec.Clear()
txtfrecval.Focus()
e.Handled = True
End If
End Sub
 
try switching e.handled=true and txtfrecval.focus(). I'm not sure, but it could be some sort of order of opperations where focus is getting changed but focus returns to the initial sub afterwords.

-Rick

----------------------
 
haha omg dudes... seems like little things like to mess with our lifes...

i got it going.. sort of...
i was focusing to the SAME TEXTBOX... DUUUUUUUUUUUH!! hehehe that happens when you use similar names between the textboxes... duuh

but it keep beeping whenever i press Enter at both textboxes.. dunno why

if anyone knows... please... :)
thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top