-
Jul 28th, 2021, 01:15 PM
#1
Thread Starter
Hyperactive Member
[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
-
Jul 28th, 2021, 01:49 PM
#2
Re: KeyUp event on TextBox ?
Try the Change event instead of KeyPress perhaps...
-
Jul 28th, 2021, 02:54 PM
#3
Thread Starter
Hyperactive Member
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).
-
Jul 28th, 2021, 04:25 PM
#4
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
-
Jul 28th, 2021, 05:46 PM
#5
Thread Starter
Hyperactive Member
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.
-
Jul 28th, 2021, 07:29 PM
#6
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
-
Jul 28th, 2021, 10:44 PM
#7
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.
-
Jul 29th, 2021, 03:08 AM
#8
Re: KeyUp event on TextBox ?
Originally Posted by DataMiser
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>
-
Jul 29th, 2021, 06:20 AM
#9
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).
-
Jul 30th, 2021, 11:04 AM
#10
Thread Starter
Hyperactive Member
Re: KeyUp event on TextBox ?
Hi friends,
Thanks for your answers , the problem has been solved with the tachgnome one's :
Originally Posted by techgnome
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 !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|