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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add "backspace" to character just typed

Status
Not open for further replies.

lizray

Programmer
May 14, 2008
126
AU
I have a form with 8 textboxes, each storing 1 char ( a "0" or a "1"). When the user enters a char, I want to overtype the existing char in any of the textboxes that has the focus. I monitor the Keydown event for the form and when I see a valid char typed I want to add a backspace to it . Can anyone please help ?
 
Why add a backspace? Couldn't you just set the textbox.text property to the new character?
 
Possibly - I think it would have to be done at each textbox. I am monitoring the "keydown" event for the form and only allowing an ascii "0" or "1" to be passed. I thight wanted to add the ascii for backspace to the "just typed" character, and have them both passed to the textbox, so that however many times a 1 or 0 was typed, there would only show a single char whcih would be the last one typed
 


Code:
Private Sub TextBox1_Key[b][highlight]UP[/highlight][/b](ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    With TextBox1
        Select Case Left(.Text, 1)
            Case "0", "1"
                .Text = Left(.Text, 1)
            Case Else
                .Text = ""
        End Select
    End With
End Sub

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks for your input Skip. I think that would have to go into each of the 8 textboxes. I was hoping to use the Keydown event for the FORM . It works ok and I am able to reject all chars except "1" or "0". I would then like to add some code that would let me send the Keycode pressed (say 48 if it was a "0"), and then s backspace character to reposition the cursor, ready for the possibility another key would be pressed. ie something like Keycode = Keycode & 9
 
At most, one textbox will have the focus. If you are catching keypress events on the form, it sounds like the form has the focus. If the form has the focus, how do you know which textbox(es) to send the entered text to?
 
Thanks jges, I am monitoring the key pressed in The Keydown event for the form. with Preview set to yes, you can see the keypress contents before it is sent to the control. It does seem a hard task, so I have solved it by checking the character on each textbox
 
You can set properties for a bunch of controls at once... Have you considered using a Validation rule or input mask on all the controls?
 
Thanks Lameid, I will look into that suggestion
 
How are ya lizray . . .

It can be done as you require using the forms [blue]On Key Down[/blue] event ...
[ol][li]To identify the fields enter a question mark (no quotations please) in the [blue]Tag[/blue] property of the 8 controls.[/li]
[li]Copy/paste the following to the forms [blue]On Key Down[/blue] event:
Code:
[blue]   Dim actName As String
   
   actName = Screen.ActiveControl.Name
   
   If Me(actName).tag = "?" Then
      Select Case KeyCode
         Case vbKey0, vbKey1 [green]'allow 0 & 1[/green]
            Me(actName) = Null
         Case vbKeyReturn, vbKeyTab [green]'allow Tab & Return[/green]
            [green]'[/green]
         Case Else
            KeyCode = 0 [green]'disallow all other keys[/green]
      End Select
   End If[/blue]
[/li]
[li]Save & perform your testing. Note: be sure to rem out or remove other code you have to handle this to prevent interaction ...[/li][/ol]

[blue]Your Thoughts . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Excellent suggestion Aceman!, your name says it all! Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top