Module: TextBox in Category Microsoft Visual Basic 6 : Controls from Total Visual SourceBook

Routines to extend the functionality of the standard Visual Basic 6 (VB6) TextBox control.

Some of these procedures expose features which are inherent in the Windows control, but are not exposed by VB6 (such as the ability to set a bounding rectangle, or to force input to a certain case). Other procedures add entirely new functionality such as filtering keystrokes, or selecting text.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the modTextBox module.
CanUndoTextBox Procedure Determine whether or not there is something to 'undo' in the specified text box control. Useful for disabling Undo buttons or menu choices if there is nothing to undo.
CRToTab Procedure Allow the user to press the Return key to tab between text boxes. Use this function to move the focus from one control to another by pressing the Return key rather than the Tab key. Call this procedure from the KeyPress event of each text box to which you wish to give this behavior. Assign the return value of this function to the KeyAscii argument of the xx_KeyPress() event.
Note: There must not be a button with the 'Default' attribute on the form in order for this to work.
FilterKeyPressAlpha Procedure Test passed key value to see whether it is an alphabetic character. Use this function to trap keystrokes so that only alphabetic characters (a-z and A-Z) may be entered. Call this function from the KeyPress event of a text box. If the keystroke entered is valid, the function returns the original value; otherwise it returns a zero (which means that the keystroke is discarded).
FilterKeyPressNumeric Procedure Test a key value to see whether it is a numeric character. Use this function to trap keystrokes so that only numbers may be entered. Optionally allow the "." and "-" for decimal points and negative numbers. Call this function from the KeyPress event of a text box. If the keystroke entered is valid, the function returns the original value; otherwise it returns a zero (which means that the keystroke is discarded).
FilterKeyPressValues Procedure Test passed key value and a list of valid key codes to see whether the character is in that list. Use this function to trap keystrokes so that only selected characters may be entered. Call this function from the KeyPress event of a text box. If the keystroke entered is valid, the function returns the original value; otherwise it returns a zero (which means that the keystroke is discarded).
GetTextBoxLine Procedure Return a line of text from a multi-line text box. The number specified must be between 0 and one less than the number of lines in the text box, because the lines are numbered starting with zero.
GetTextBoxLineCount Procedure Determine the number of lines in a multi-line text box. This function does not count carriage return/linefeed pairs, but instead returns the number of lines actually displayed in the text box, accounting for line wraps.
Note: This function is often used in conjunction with the GetTextBoxLine function.
SelectText Procedure Select all text in the passed text box. This makes it easier for a user to change existing data, since any keystroke replaces the entire selection. This function should be called in the GotFocus event of the text box.
SetTextBoxCase Procedure Force all new entry into the text box to be either uppercase or lowercase. This is useful to normalize the format of a text box, such as a State abbreviation field or other formatted data.
SetTextBoxRect Procedure Create a bounding rectangle for a text box that is different from its actual dimensions. Use this function to specify logical dimensions of a text box which differ from the actual dimensions. You can use this function to control line wrapper, or when scrollbars appear. Windows treats the text box as if it were the size you specify, instead of its size as set at design time.
SetTextBoxTabStops Procedure Set tab stops for a text box. The standard VB6 text box uses the default tab stop positions. With this procedure, you can create an array of tab stop positions to control the tab stops in a text box.
TextBoxFromDisk Procedure Load the contents of a text file into a text box. The file is presumed to exist and contain data that can be loaded into a text box. Normally this is a file containing only ASCII characters, such as a .TXT file. If the file is longer than can fit into a text box, it is truncated. If the file does not exist, an error is generated.
TextBoxToDisk Procedure Write the contents of a text box to a disk file. If the file specified already exists, it is overwritten.
UndoTextBox Procedure Undoes the last typing action in a text box.
' Example of modTextBox
'
' To try this example, do the following:
' 1. Create a new form
' 2. Create a text box named 'txtTest'
'    Set the following properties
'    Multiline      true
' 3. Create a text box named 'txtTabs'
'    Set the following properties
'    Multiline      true
' 4. Create a text box named 'txtAlpha'
' 5. Create a text box named 'txtNumeric'
' 6. Create a text box named 'txtValues'
' 7. Create a command button 'cmdUndo'
' 8. Create a command button 'cmdTest'
' 9. Create a command button 'cmdToDisk'
' 10. Create a command button 'cmdFromDisk'
' 11. Paste all the code from this example to the new form's module.

' This example assumes that the sample files are located in the folder named by the following constant.
Private Const mcstrSamplePath As String = "C:\Total Visual SourceBook 2013\Samples\"

Private Sub Form_Load()
  cmdUndo.Caption = "Undo"
  cmdTest.Caption = "Test"
  cmdToDisk.Caption = "To Disk"
  cmdFromDisk.Caption = "From Disk"

  txtTest = "Mary had a little lamb" & vbCrLf & _
            "Whose fleece was white as snow" & vbCrLf & _
            "And everywhere that Mary went" & vbCrLf & _
            "That lamb was sure to go"

  txtAlpha = "abc"
  txtNumeric = "123"
  txtValues = "AX9"

  ' Change the style of txtAlpha to automatically force all entries to be in lower-case.
  SetTextBoxCase txtAlpha, 0

  ' Make the text box twice as wide and twice as tall as its actual dimensions.
  ' Note that values are scaled to Pixels rather than Twips
  SetTextBoxRect txtTest, 0, 0, ScaleX(txtTest.Width * 2, vbTwips, vbPixels), ScaleY(txtTest.Height * 2, vbTwips, vbPixels)

End Sub

Private Sub cmdTest_Click()

  Dim lngLines As Long
  Dim strLine As String

  lngLines = GetTextBoxLineCount(txtTest)
  MsgBox "txtTest lines: " & lngLines

  strLine = GetTextBoxLine(txtTest, 1)
  MsgBox "Second line is: " & strLine

  SetTextBoxTabStops txtTabs, 100, 100, 110
  txtTabs = "a" & vbTab & "b" & vbTab & "c"

End Sub

Private Sub cmdUndo_Click()
  ' Example of UndoTextBox
  UndoTextBox txtTest
End Sub

Private Sub txtAlpha_KeyPress(KeyAscii As Integer)
  ' Allow only alphabetic characters to be entered into the text box.

  KeyAscii = FilterKeyPressAlpha(KeyAscii)

End Sub

Private Sub txtNumeric_KeyPress(KeyAscii As Integer)
  ' Allow only numeric characters to be entered into the text box.

  KeyAscii = FilterKeyPressNumeric(KeyAscii)

End Sub

Private Sub txtTest_Change()
  ' Disable or enable the cmdUndo button based on whether or not there is anything to undo in the Text1 text box.

  cmdUndo.Enabled = CanUndoTextBox(txtTest)

End Sub

Private Sub txtTest_GotFocus()
  ' Select text in the text box when the user sets focus to the field.

  SelectText txtTest

End Sub

Private Sub txtTest_KeyPress(KeyAscii As Integer)
  ' If user presses {Return}, move focus to next control in the tab order.

  KeyAscii = CRToTab(KeyAscii)
End Sub

Private Sub txtValues_KeyPress(KeyAscii As Integer)
  ' Allow only specified characters to be entered into the text box.
  ' In this case valid values are the characters 'A', 'X' or '9' or the BkSpc key

  KeyAscii = FilterKeyPressValues(KeyAscii, vbKeyA, vbKeyX, vbKey9, vbKeyBack)

End Sub

Private Sub cmdFromDisk_Click()
  Dim strInput As String

  strInput = mcstrSamplePath & "test.txt"

  TextBoxFromDisk txtTest, strInput

End Sub

Private Sub cmdToDisk_Click()
  Dim strOutput As String

  strOutput = mcstrSamplePath & "text2dsk.txt"

  TextBoxToDisk txtTest, strOutput

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS