How to use Keypress Properly?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • frankleggett
    New Member
    • Oct 2006
    • 22

    #1

    How to use Keypress Properly?

    Hi

    I am using Keypress(keyAsc ii as integer) like this

    Private Sub txtBox_keypress (KeyAscii As Integer)
    txtBox.SelStart = 0
    txtBox.SelLengt h = Len(txtBox.Text )
    If KeyAscii = 13 Then
    Call SaveContents

    but the problem is I can only use this if the contents of txtbox are 1 character long otherwise it resets itself when it checks if I have pressed the return key.
    Also is the an Ascii specific code for the Enter key on the NumberPad.

    Frank
  • albertw
    Contributor
    • Oct 2006
    • 267

    #2
    Originally posted by frankleggett
    Hi

    I am using Keypress(keyAsc ii as integer) like this

    Private Sub txtBox_keypress (KeyAscii As Integer)
    txtBox.SelStart = 0
    txtBox.SelLengt h = Len(txtBox.Text )
    If KeyAscii = 13 Then
    Call SaveContents

    but the problem is I can only use this if the contents of txtbox are 1 character long otherwise it resets itself when it checks if I have pressed the return key.
    Also is the an Ascii specific code for the Enter key on the NumberPad.

    Frank
    hi
    your selstart / sellength statements make the textbox reset itself.
    better use these statements on getfocus

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by frankleggett
      Hi

      I am using Keypress(keyAsc ii as integer) like this

      Private Sub txtBox_keypress (KeyAscii As Integer)
      txtBox.SelStart = 0
      txtBox.SelLengt h = Len(txtBox.Text )
      If KeyAscii = 13 Then
      Call SaveContents

      but the problem is I can only use this if the contents of txtbox are 1 character long otherwise it resets itself when it checks if I have pressed the return key.
      Also is the an Ascii specific code for the Enter key on the NumberPad.

      Frank
      If the SaveContents routine is depending on the text to be selected, then you should do so either in the routine itself, or inside the If...End If structure. For example...
      Code:
      Private Sub txtBox_keypress(KeyAscii As Integer)
        If KeyAscii = 13 Then
          txtBox.SelStart = 0
          txtBox.SelLength = Len(txtBox.Text)
          Call SaveContents
      ...
      It might also be nice to put the textbox back the way you found it. This might work...
      Code:
      Private Sub txtBox_keypress(KeyAscii As Integer)
        Static HoldStart As Long, HoldLength As Long
        If KeyAscii = 13 Then
          [B]HoldStart = txtBox.SelStart
          HoldLength = txtBox.SelLength[/B]
          txtBox.SelStart = 0
          txtBox.SelLength = Len(txtBox.Text)
          Call SaveContents
          [B]txtBox.SelStart = HoldStart
          txtBox.SelLength = HoldLength[/B]
      ...

      Comment

      • frankleggett
        New Member
        • Oct 2006
        • 22

        #4
        Thanks for your replies

        Frank

        Comment

        Working...