Re: How to avoid double entries inside a text box?



Private Sub ***text_box_name****KeyPress(KeyAscii As Integer)
'make sure only numbers are entered
'and allow the backspace key to be pressed
If KeyAscii = 8 Then
Else
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0 ' Cancel the character.
Beep ' Sound error signal.
Else
End If
End If


Add the other keys you want to allow after: If KeyAscii = 8
and before Then
Else.

Note ***text_box_name**** above.

--

Steve Easton



End Sub
"Jack" <replyto@it> wrote in message news:uHPKy$ViIHA.1944@xxxxxxxxxxxxxxxxxxxxxxx
Mike, you are the closest to my problem.
What I want to have is:
simple textbox on the form.
It should allow all the normal operations textbox has when it is in focus
plus:
it should append (or insert depending on .SelText value) all numeric (not letters) keys (0-9) coming from
Form_KeyDown event plus respond to Backspace, Left & Right Arrow keys and Delete, Home, End, Insert

The problems I am having now:
1.
Backspace eats 2 characters at the time
2.
I have tried your suggestions.
If I include in Sub KeyDown:
Case 48 To 57 '0 to 9

these 2 lines:
txtDial.SelStart = Len(txtDial.Text)
txtDial.SelText = Chr(KeyCode)
I have not double but triple repeat of the same character!
Below is the relevant code:
===================
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Call KeyDown(KeyCode, Shift)
If Me.ActiveControl = txtDial Then KeyCode = 0
End Sub

Public Sub KeyDown(KeyCode As Integer, Shift As Integer)
Select Case Shift
Case 0
Select Case KeyCode
Case 8 'Backspace
If Len(txtDial.Text) > 0 Then
If txtDial.SelStart = Len(txtDial.Text) Then
txtDial.Text = Mid(txtDial.Text, 1, Len(txtDial.Text) - 1)
txtDial.SelStart = Len(txtDial.Text)
Else
lpString = Mid(txtDial.Text, 1, txtDial.SelStart - 1)
txtDial.Text = lpString & Mid(txtDial.Text, txtDial.SelStart + 1)
txtDial.SelStart = Len(lpString)
End If
End If
Case 48 To 57 '0 to 9
txtDial.Text = txtDial.Text & Chr(KeyCode)
''' txtDial.SelStart = Len(txtDial.Text)
''' txtDial.SelText = Chr(KeyCode)
Case 39 'RIGHT ARROW
If Screen.ActiveForm.ActiveControl.Name <> "txtDial" Then
hsbTabs.Value = hsbTabs.Value + 1 ' hsbTabs_Change
Else
If txtDial.SelStart < Len(txtDial.Text) Then txtDial.SelStart = txtDial.SelStart + 1
End If
Case 37 'LEFT Arrow
If Screen.ActiveForm.ActiveControl.Name <> "txtDial" Then
hsbTabs.Value = hsbTabs.Value - 1 ' hsbTabs_Change
Else
If txtDial.SelStart > 0 Then txtDial.SelStart = txtDial.SelStart - 1
End If
Case 46 'DELETE
If txtDial.SelStart = 1 Then
txtDial.Text = Right(txtDial.Text, txtDial.SelStart + txtDial.SelLength)
Else
txtDial.Text = Mid(txtDial.Text, 1, txtDial.SelStart) & Mid(txtDial.Text,
txtDial.SelStart + txtDial.SelLength + 1)
End If
End Select
End Select
End Sub




"Mike Williams" <gagamomo@xxxxxxxxxxxx> wrote in message news:%238IVYGUiIHA.5152@xxxxxxxxxxxxxxxxxxxxxxx
"Jack" <replyto@it> wrote in message news:OT9HjuTiIHA.4436@xxxxxxxxxxxxxxxxxxxxxxx

The double entry is strange: one character appears at the end
(and this is according to the code) and a second character#
appears at the beginning of the text!!!.

It's not strange at all. It is exactly what your code is doing. You are dealing with the keystroke in the
Form's key event (presumably because Keypreview is True) and you are adding that character onto the end of
the existing TextBox text by concatenating the existing text and the key character and dumping the result
into the TextBox. Doing so causes the insertion point (SelStart) to automatically go to the start of the
TextBox. Then the same keystroke that your Form's key event has just dealt with will also be picked up by
the textBox (if the TextBox had the focus when the key was pressed), so that exactly the same key that was
previously appended at the end of the textBox (by your Form's key event code) will also be inserted at the
beginning (because by the time the keypress reaches the TextBox your Form key event code has already
placed the insertion point at the beginning!). So, you get one character at the end and one character at
the beginning! It is exactly what you would expect your code to do.

Perhaps it is about time you explained exactly what you are trying to achieve? In the meantime, try using
the Form's KeyDown event instead of its KeyUp event, and also try inserting the character at the end of the
current contents (if that's where you want it to be) using a SelText assignment, which will not cause a
change of the insertion point. Then try setting the key to zero so that the TextBox does not itself also
deal with the key, but only set it to zero if the currently active control actually is the TextBox,
otherwise you will prevent key entries in other controls. Something like this (with KeyPreview = True):

Private Sub Form_KeyPress(KeyAscii As Integer)
Text1.SelStart = Len(Text1.Text)
Text1.SelText = Chr$(KeyAscii)
If Me.ActiveControl = Text1 Then
KeyAscii = 0
End If
End Sub

The above code will append all key entries, wherever they occur on the Form (with some specific
limitations) onto the existing contents of Text1 while still allowing other controls on the Form to deal
with them in the normal way, and it will do that whether Text1 has the focus or not. Is that anything like
the result you are after?

Mike







.



Relevant Pages

  • Re: How to avoid double entries inside a text box?
    ... You are dealing with the keystroke in the Form's key event and you are adding that character onto the end of the existing TextBox text by concatenating the existing text and the key character and dumping the result into the TextBox. ... Then the same keystroke that your Form's key event has just dealt with will also be picked up by the textBox, so that exactly the same key that was previously appended at the end of the textBox will also be inserted at the beginning (because by the time the keypress reaches the TextBox your Form key event code has already placed the insertion point at the beginning!). ...
    (microsoft.public.vb.general.discussion)
  • Re: How to avoid double entries inside a text box?
    ... I need also to append characters when the textbox is not ... Form_KeyDown event plus respond to Backspace, ... I have not double but triple repeat of the same character! ... Private Sub Form_KeyDown ...
    (microsoft.public.vb.general.discussion)
  • Re: How to avoid double entries inside a text box?
    ... It should allow all the normal operations textbox has when it is in focus ... I have not double but triple repeat of the same character! ... Then the same keystroke that your Form's key event has just ... the insertion point at the beginning!). ...
    (microsoft.public.vb.general.discussion)
  • Re: How to avoid double entries inside a text box?
    ... It should allow all the normal operations textbox has when it is in focus ... I have not double but triple repeat of the same character! ... Private Sub Form_KeyDown ... Then the same keystroke that your Form's key event has just dealt with will also be picked up by ...
    (microsoft.public.vb.general.discussion)
  • Re: How to avoid double entries inside a text box?
    ... It should allow all the normal operations textbox has when it is in focus ... I have not double but triple repeat of the same character! ... Private Sub Form_KeyDown ... Then the same keystroke that your Form's key event has just dealt ...
    (microsoft.public.vb.general.discussion)