Read-Only TextBox
by Dave Healy - Independent Developer
I’ve recently had occasion to create a large, read-only text box which a user might want to scroll through.
Setting the text box’s ScrollBars property appropriately is fine for mouse users, but I wanted keyboard users to be able to achieve the same effect. The following sub-routine does the job:
Sub txtView_KeyDown (Keycode As Integer, Shift As Integer)
On Error Resume Next
' Keycodes are as follows:
' 33 = PageUp
' 34 = PageDown
' 35 = End
' 36 = Home
' 37 = Left arrow
' 38 = Up arrow
' 39 = Right arrow
' 40 = Down arrow
If Keycode >=33 And Keycode <=40 Then
KeyOK = True
Else
KeyOK = False
End If
If Err Then MsgBox ("Error" & Err & "in VIEWER, txtView_KeyDown:"& Error$(Err))
End Sub
The text box's KeyPress routines testsKeyOK, registering the key press only
if KeyOK is true. KeyPress can't sample the codes directly because they're not
ASCII. This routine also allows CTRL-HOME and CTRL-END to function as you'd
expect in a Microsoft application. Its only limitation is that
PageUp isn't registered at all if the cursor can't actually move up a whole "page".