C
Corbin
I'm trying to improve data-entry efficiency for users of my program.
One idea we had was to make certain textboxes work like the To: box in
an Outlook email message, where it autofills a suggested recipient
from your address box in response to the first letter or two that you
type.
So far, I have met with limited success in doing this. My approach
has been to try to make a textbox respond to the textchanged event by
checking the typed text against a form-level array of strings called
mAutoFill(). If it finds a match, it puts the remaining text in the
box, and selects the text that has been filled while leaving the typed
text unselected. The code I've come up with to do this is ugly, long,
and does not work perfectly.
(For example, after it autofills, if you hit backspace, it should
remove the selected text and leave the rest--this happens, but then it
analyzes the remaining text and autofills the same suggestion, making
it seem to the user that nothing happens when he hits backspace.)
That's just one example. Before making my code even more complicated
in an effort to fix that problem, (not to mention whatever other
problems are there) I wondered if anybody out there might be able to
suggest a more elegant way to implement autofill. Am I even using the
right control?
Here is my ugly, somewhat embarrassing code.
Private Sub txtAssignee_TextChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles txtAssignee.TextChanged
Dim i As Integer, iLen As Integer
Static bLockout As Boolean
Try
If bLockout = False Then
bLockout = True
iLen = Len(txtAssignee.Text)
For i = LBound(mAutoFill) To UBound(mAutoFill)
If
UCase(mAutoFill(i)).StartsWith(UCase(txtAssignee.Text)) And
Len(mAutoFill(i)) > iLen Then
txtAssignee.Text = mAutoFill(i)
txtAssignee.Select(iLen, Len(txtAssignee.Text)
- iLen)
Exit For
End If
Next
End If
Catch ex As Exception
'ex handlers to be written later.
Finally
bLockout = False
End Try
End Sub
One idea we had was to make certain textboxes work like the To: box in
an Outlook email message, where it autofills a suggested recipient
from your address box in response to the first letter or two that you
type.
So far, I have met with limited success in doing this. My approach
has been to try to make a textbox respond to the textchanged event by
checking the typed text against a form-level array of strings called
mAutoFill(). If it finds a match, it puts the remaining text in the
box, and selects the text that has been filled while leaving the typed
text unselected. The code I've come up with to do this is ugly, long,
and does not work perfectly.
(For example, after it autofills, if you hit backspace, it should
remove the selected text and leave the rest--this happens, but then it
analyzes the remaining text and autofills the same suggestion, making
it seem to the user that nothing happens when he hits backspace.)
That's just one example. Before making my code even more complicated
in an effort to fix that problem, (not to mention whatever other
problems are there) I wondered if anybody out there might be able to
suggest a more elegant way to implement autofill. Am I even using the
right control?
Here is my ugly, somewhat embarrassing code.
Private Sub txtAssignee_TextChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles txtAssignee.TextChanged
Dim i As Integer, iLen As Integer
Static bLockout As Boolean
Try
If bLockout = False Then
bLockout = True
iLen = Len(txtAssignee.Text)
For i = LBound(mAutoFill) To UBound(mAutoFill)
If
UCase(mAutoFill(i)).StartsWith(UCase(txtAssignee.Text)) And
Len(mAutoFill(i)) > iLen Then
txtAssignee.Text = mAutoFill(i)
txtAssignee.Select(iLen, Len(txtAssignee.Text)
- iLen)
Exit For
End If
Next
End If
Catch ex As Exception
'ex handlers to be written later.
Finally
bLockout = False
End Try
End Sub