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

Keystroke counter

Status
Not open for further replies.

don2241

IS-IT--Management
Jun 1, 2001
57
AU
Hi!

I'm trying to have a counter that counts the number of keystrokes the user is making in a text field, in a seperate textbox

i.e
user press H textbox is showing 1
user press e textbox is showing 2
user press l textbox is showing 3
user press l textbox is showing 4
user press o textbox is showing 5
user press [space] textbox is showing 6

In the text field where the user is typing you would now have "Hello " and the counter shoud show 6
I have tried using a timer on the form to check lenght of textfield every 50milisecond but it does not work unless user leaves the text field where they are typing.

Any response helpful

/M
 
I think the timer would work, if you test for the length of the .Text property, something like this:

[tt]if (me.activecontrol.name="txtType") then
me("txtCount").value=len(me("txtType").text)
end if[/tt]

For other methods (keypress, keydown), you'd need to trap for all different keystrokes, disallowing ESC, function keys, subtract when backspace, delete - how to figure out if the user deletes the whole contents and not just one letter, or selects the whole control and starts typing (replace the contents)...

Roy-Vidar
 
this will do what you want.. except if the user selects a group of letters, and then hits the backspace or delete key. there are probably other ways this can display an incorrect count but you might be able to mess around with it and get your desired results. This example uses a form level variable intC and two events of a textbox the Enter and the Keypress and the Form's Key Preview is set to Yes.



Option Compare Database
Option Explicit
Dim intC As Integer

Private Sub txtData_Enter()
intC = Len(Trim(Nz(txtData, "")))
lblCount.Caption = "Textbox is showing " & intC
End Sub

Private Sub txtData_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 Then
intC = intC + 1
lblCount.Caption = "Textbox is showing " & intC
Else
intC = intC - 1
If intC < 0 Then intC = 0
lblCount.Caption = "Textbox is showing " & intC
End If
End Sub

HTH
PaulF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top