|
|
|
|
|
|
Excel
|
Multiline TextBox: Restrict accidental exit while DownArrow/UpArrow through the text
|
|
Ease of Use
|
Easy
|
Version tested with
|
2003
|
Submitted by:
|
tstav
|
Description:
|
To exit the multiline TextBox and move to another textbox, you need to TAB/ShiftTAB. Arrows will be stopped at top/bottom lines of text.
|
Discussion:
|
When TextBoxes have many lines of text, we usually move up and down using the Up/Down Arrows, in order to either edit or add text.
But as we reach the top or bottom line intending to add some text, it often happens that we press one click too many and the cursor jumps to the next/previous textbox (if there is one).
If you need to halt the cursor from exiting your multiline textbox, use the code I'm submitting (you don't need to use the ThisWorkbook code though, this is only for test purposes).
|
Code:
|
instructions for use
|
Option Explicit
Private Sub Workbook_Open()
Load UserForm1
UserForm1.Show
End Sub
Private Sub TextBox_KeyDown _
(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyDown Then
With Me.TextBox
If .LineCount > 1 And _
.CurLine = .LineCount - 1 Then
Beep
.SetFocus
.CurLine = .LineCount - 2
End If
End With
End If
If KeyCode = vbKeyUp Then
With Me.TextBox
If .LineCount > 1 And _
.CurLine = 0 Then
Beep
.SetFocus
.CurLine = 1
End If
End With
End If
End Sub
|
How to use:
|
- Choice No1:
- Download the TextBox.zip file I have submitted as an attachment.
- Unzip it and you will have the TextBox.xls file. Double click it to run.
- Choice No2:
- Create a new XLS file. Press Alt-F11 to get into the VBEditor.
- Copy the code to the specific modules I'm mentioning in my code's comments.
- Close the VBE.
- Save the file (call it whatever you like). Open it.
|
Test the code:
|
- Move the cursor up and down.
- When you reach the top or bottom of the middle textbox, you will hear a beep. The cursor will refuse to exit the textbox.
- Of course if you don't like the beeping, erase the 'beep' row from the code.
|
Sample File:
|
TextBox.zip 7.17KB
|
Approved by mdmackillop
|
This entry has been viewed 183 times.
|
|