Re: Can I implement an autofill feature with a textbox?
corbinamman_at_gmail.com
Date: 01/14/05
- Next message: corbinamman_at_gmail.com: "Re: Looking for tutorial that explains how to build good looking UI"
- Previous message: bryan: "Re: When doing layouts on a Form...why do controls know thier location"
- In reply to: Warmrain: "Re: Can I implement an autofill feature with a textbox?"
- Next in thread: Warmrain: "Re: Can I implement an autofill feature with a textbox?"
- Reply: Warmrain: "Re: Can I implement an autofill feature with a textbox?"
- Messages sorted by: [ date ] [ thread ]
Date: 14 Jan 2005 12:57:29 -0800
Thanks to Warmrain for the help. I don't want the user to have to hit
the arrow keys, though. In my component, each time a character is
typed, the Text property is evaluated against an array of possible
values, and if one is found then the rest of the suggested text is
appended to the already-typed text and the appended part is highlighted
so that if the user hits yet another key the appended suggestion will
be overwritten and the process starts over. Here's my user control. I
left out the code that was automatically generated by VS.NET. There is
nothing in there special. I would like to make this work so that if
there are multiple matches, you can cycle through them with the arrow
keys. But I didn't have time to work on this feature. If anyone wants
to post an edited version of this class that would do that, I would
appreciate it.
Public Class AutofillTextbox
Inherits System.Windows.Forms.TextBox
Private mAutoFill As New ArrayList
Private mblnLockout As Boolean
Private mintLastKeystroke As Integer
Public Property AutoFillArray() As ArrayList
Get
AutoFillArray = mAutoFill
End Get
Set(ByVal Value As ArrayList)
mAutoFill = Value
'sort the items in order that the items are matched
alphabetically
If Not mAutoFill Is Nothing Then mAutoFill.Sort()
End Set
End Property
Private Sub AutofillTextbox_TextChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles MyBase.TextChanged
Dim iLen As Integer = Len(Me.Text)
Try
If mblnLockout = False And iLen > 0 And mintLastKeystroke
>= 30 And Not mAutoFill Is Nothing Then
'avoid callilng this method recursively by setting this
flag to true.
mblnLockout = True
'cycle through the ArrayList in search of an item that
matches the typed text.
Dim myEnumerator As System.Collections.IEnumerator =
mAutoFill.GetEnumerator()
While myEnumerator.MoveNext()
If
UCase(myEnumerator.Current).StartsWith(UCase(Me.Text)) And
Len(myEnumerator.Current) > iLen Then
'a match is found, and there is text in the
match that needs to be appended and highlighted.
Me.Text = myEnumerator.Current
Me.Select(iLen, Len(myEnumerator.Current) -
iLen)
Exit While
End If
End While
Else
'at least one of the following 4 conditions are true,
thus no action is called for.
'1. mblnLockout is true: skip code to avoid infinite
recursion.
'2. there is nothing in the textbox to match with
'3. the keystroke that fired this event was backspace
or some other non-character ascii code.
'4. the AutoFillArray property is not set: there is
nothing to match the typed text with.
End If
Catch ex As Exception
'ex handlers to be written later.
Finally
mblnLockout = False
End Try
End Sub
Private Sub AutofillTextbox_KeyPress(ByVal sender As Object, ByVal
e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
mintLastKeystroke = AscW(e.KeyChar)
End Sub
End Class
- Next message: corbinamman_at_gmail.com: "Re: Looking for tutorial that explains how to build good looking UI"
- Previous message: bryan: "Re: When doing layouts on a Form...why do controls know thier location"
- In reply to: Warmrain: "Re: Can I implement an autofill feature with a textbox?"
- Next in thread: Warmrain: "Re: Can I implement an autofill feature with a textbox?"
- Reply: Warmrain: "Re: Can I implement an autofill feature with a textbox?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|