Results 1 to 10 of 10

Thread: [RESOLVED] KeyUp event on TextBox ?

  1. #1

    Thread Starter
    Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    32

    Resolved [RESOLVED] KeyUp event on TextBox ?

    Hi friends,

    I try modifiy my program and I discovered a problem.

    Code:
    Private Sub serialnumber_KeyPress(KeyAscii As Integer) 
    If KeyAscii = vbKeyReturn Then KeyAscii = 0
    If Len(serialnumber.Text) < 8 Then
    serialnumber.BackColor = vbRed
    Else
    serialnumber.BackColor = vbWhite
    End If
    End Sub
    It doesn't as I would like.
    Because the TextBox value has not yet receveid the character I pressed when I count its length.

    I would try with KeyUp event but I get a compilation error when I try to run the project.

    Any idea ?
    Thanks
    Couin
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone)

  2. #2
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,187

    Re: KeyUp event on TextBox ?

    Try the Change event instead of KeyPress perhaps...

  3. #3

    Thread Starter
    Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    32

    Re: KeyUp event on TextBox ?

    Hi,

    Change if ok only if I remove the parameter KeyAscii As Integer (and making the Return key "Ding" because vb6 see it as field validation, and I don't want it).
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone)

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,469

    Re: KeyUp event on TextBox ?

    huh... if only there was a property or something to prevent users from entering more than 6 characters into a text .... something like a MaxLength property or something...

    Set the .MaxLength to 6... get rid of all that code, and only handle the enter key press.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    32

    Re: KeyUp event on TextBox ?

    Hi

    I minized the code but in fact, it's a a field where the user enter the serial number of a machine.

    If need that the field background is red and some commands buttons are disabled if the serial number lenght is under 8 characters.
    If the entered SN lenght is 8 or more , the field background pass to white and the command buttons are enabled.

    The serial number can be enterer from an handheld scanner for barcodes, that sends the "Return" key code at end of scan. I would this "Return" key code ignored and not trig the "Ding" Windows sound.
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone)

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,469

    Re: KeyUp event on TextBox ?

    put this in the keyPRess event:
    Code:
    If KeyAscii = vbKeyReturn Then KeyAscii = 0
    put this in the changed event
    Code:
    If Len(serialnumber.Text) < 8 Then
    serialnumber.BackColor = vbRed
    Else
    serialnumber.BackColor = vbWhite
    End If
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,673

    Re: KeyUp event on TextBox ?

    Hmm it seems odd to me that you set the key to 0 but do not appear to be doing any processing. Typically when I process data from a scanner I intercept the enter key and have it move to the next field or process the record or save. In some cases there may only be one field so I may process the entry then blank the field for the next scan but the code there does not appear to do anything other than suppress the ding and change the color.

  8. #8
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,210

    Re: KeyUp event on TextBox ?

    Quote Originally Posted by DataMiser View Post
    Hmm it seems odd to me that you set the key to 0 but do not appear to be doing any processing.
    The only reason for suppressing vbKeyReturn with KeyAscii = 0 is that if there is no default button on the form the intrinsic TextBox control produces a "ding!" sound on Enter which is mildly irritating.

    I'm not sure if the RichTextBox in single-line mode does this too.

    cheers,
    </wqw>

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,994

    Re: KeyUp event on TextBox ?

    An example comprised of comments from above (and mine as well included):

    Code:
    Private Sub Form_Load()
        'if not already done so in properties, set starting properties of a button and the textbox
        Command1.Enabled = False
        Text1.MaxLength = 8
        Text1.BackColor = vbRed
        Text1.ForeColor = vbWhite
    End Sub
    
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii = vbKeyReturn Then KeyAscii = 0  'per guidance from above thread
    End Sub
    
    
    Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
        If Len(Trim(Text1.Text)) < 8 Then
            Text1.BackColor = vbRed
            Text1.ForeColor = vbWhite  'black on red bg is hard to read, change forecolor to white
            Command1.Enabled = False
        Else
            Text1.BackColor = vbWhite
            Text1.ForeColor = vbBlack  'change forecolor back to black from white
            Command1.Enabled = True  'enables one of your buttons
        End If
    End Sub
    Sam I am (as well as Confused at times).

  10. #10

    Thread Starter
    Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    32

    Re: KeyUp event on TextBox ?

    Hi friends,

    Thanks for your answers , the problem has been solved with the tachgnome one's :
    Quote Originally Posted by techgnome View Post
    put this in the keyPRess event:
    Code:
    If KeyAscii = vbKeyReturn Then KeyAscii = 0
    put this in the changed event
    Code:
    If Len(serialnumber.Text) < 8 Then
    serialnumber.BackColor = vbRed
    Else
    serialnumber.BackColor = vbWhite
    End If
    -tg
    See ya !

    Couin !
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width