XTreme Logo

If you appreciate these tips, please consider a small donation:

General Tip - Novice

Fun With Textboxes :
Quick Examples of Overtype Mode, Toggling Overtype, Highlighting Content on Entry, and More...
Tested with VB5 SP3
The completed project looks like the image below.  Click on a textbox to go right to the code for that functionality.

tboxfun.jpg (25765 bytes)

Overtype Mode:

The code for overtyping is the height of simplicity.  Set the SelLength property of the textbox to 1 on the KeyPress event.   That highlights the next character after the cursor position so the character you type overwrites it.

Private Sub txtOvertype_KeyPress(KeyAscii As Integer)
   ' Select the next character in the textbox so it gets overwritten
   txtOvertype.SelLength = 1
End Sub

Toggle Overtype Mode:

Toggling overtype mode has at its core the simple code for overtyping, but is complicated by the need to determine whether overtyping is currently desired.

Note: Set the KeyPreview property of the form to True.

This code goes in General Declarations of the form:

' Create a flag to track the state of insert key.
Private bOvertype As Boolean

Private Sub Form_Load()
   ' Initialize to default insert mode, no overtype.
   bOvertype = False
End Sub

At the form level check for the insert key being pressed and toggle overtype mode.  This is also where it's possible to toggle a visual indicator of whether insert mode is active, much the way Microsoft Word does on its status bar.  In this example the color of a label is toggled.

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
   ' Toggle overtype mode if insert key is pressed and released.
   If KeyCode = vbKeyInsert Then bOvertype = Not bOvertype
   ' You could provide a visual indicator of the state
   ' like this or in a status bar
.
   If bOvertype = True Then
      lblToggle.BackColor = vbGreen
   Else
      lblToggle.BackColor = Form1.BackColor
   End If
End Sub

Finally, the code that does the overtyping in the KeyPress of the textbox, but only if overtype mode is true.

Private Sub txtToggle_KeyPress(KeyAscii As Integer)
   ' Overtype only if the correct state has been established.
  
If bOvertype = True Then txtToggle.SelLength = 1
End Sub

Highlight Textbox Contents On Entry:

This is another task that is the height of simplicity once you see how it's done.  It is worth being aware that GotFocus and LostFocus events should be used cautiously, especially together.  However, in my experience this code is reliable in and of itself.

Private Sub txtHighlight_GotFocus()
   ' Make sure insertion point is at start even if you click in.
   txtHighlight.SelStart = 0
   ' Highlight based on length of existing text.
   txtHighlight.SelLength = Len(txtHighlight.Text)
   ' This could go in a common function called using
   ' "ActiveControl" so the code doesn't have to be 
   ' written in every textbox where it's needed.

End Sub

Reverse Text As You Type It:

This one is mostly for fun, as a demonstration of things that can be done easily with textbox code.

Private Sub txtReverse_KeyPress(KeyAscii As Integer)
   ' Put the insertion point at the beginning on
   ' every key press
.
   txtReverse.SelStart = 0
End Sub

Use Default Text As Non-Blankable Entry Instruction "Label":

I used this trick in a small application in order to save the space that would be occupied by a label accompanying each textbox.   It involves making the default text of the textbox the "label" indicating what to enter or similar information to give the user a clue why the textbox exists, then making it so the default text cannot be blanked out.  When collecting data in the end, not shown here, I simply treated a textbox containing the default text as if it contained nothing.  Whether a UI designed this way rather than with labels is appropriate is another question....

Note: Set the Tag property of the textbox and the default Text property to exactly the same value.

Private Sub txtLabel_GotFocus()
   ' Highlight the current text on entry.
   ActiveControl.SelStart = 0
   ActiveControl.SelLength = Len(ActiveControl.Text)
End Sub

Private Sub txtLabel_LostFocus()
   ' Check for blank or spaces.
   ' If nothing has been entered, restore
   ' the default text from the tag property
.
  
If Trim(txtLabel.Text) = "" Then
      txtLabel.Text = txtLabel.Tag
  
End If
End Sub

Restrict Entry to Numeric Characters:

This allows only numbers to be entered.   It could easily be modified to allow certain other characters such as the period, to enter decimal numbers.  Other code could prevent there from being more than one decimal point, to ensure it it a valid decimal number if that is the goal.  However, this gets the process started on the key point.

Private Sub txtNumbers_GotFocus()
   'These two lines highlight the text on entry.
   txtNumbers.SelStart = 0
   txtNumbers.SelLength = Len(txtNumbers.Text)
End Sub

Private Sub txtNumbers_KeyPress(KeyAscii As Integer)
   'Allow only characters in the numeric range.
   If KeyAscii < 48 Or KeyAscii > 57 Then
      KeyAscii = 0
   End If
End Sub

If you would really like to see the entire sample project (done in VB5), download it here.   Enjoy!

 


[ Back To The Top ]

Contact: web@xtremecomp.com
All contents copyright XTreme Computing unless noted otherwise.