|
-
May 16th, 2013, 12:21 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Correct ComboBox filtering
Here's a code which should filter and show ComboBox items according to our input,
Code:
Public Sub CboxAutoComplete(ByVal cbo As ComboBox, ByVal e As System.Windows.Forms.KeyEventArgs)
Dim iIndex As Integer
Dim sActual As String
Dim sFound As String
Dim bMatchFound As Boolean
If e.KeyCode = Keys.Back Then
'cbo.Text = Mid(cbo.Text, 1, Len(cbo.Text) - 1)
cbo.Text = ""
End If
If ((e.KeyCode = Keys.Left) Or _
(e.KeyCode = Keys.Right) Or _
(e.KeyCode = Keys.Up) Or _
(e.KeyCode = Keys.Down) Or _
(e.KeyCode = Keys.PageUp) Or _
(e.KeyCode = Keys.PageDown) Or _
(e.KeyCode = Keys.Home) Or _
(e.KeyCode = Keys.End)) Then
Return
End If
Do
sActual = cbo.Text
iIndex = cbo.FindString(sActual)
'if index > -1 then a match was found.
If (iIndex > -1) Then
sFound = cbo.Items(iIndex).ToString()
cbo.SelectedIndex = iIndex
cbo.SelectionStart = sActual.Length
cbo.SelectionLength = sFound.Length
bMatchFound = True
Else
If sActual.Length = 1 Or sActual.Length = 0 Then
cbo.SelectedIndex = 0
cbo.SelectionStart = 0
cbo.SelectionLength = Len(cbo.Text)
bMatchFound = True
Else
cbo.SelectionStart = sActual.Length - 1
cbo.SelectionLength = sActual.Length - 1
cbo.Text = Mid(cbo.Text, 1, Len(cbo.Text) - 1)
End If
End If
Loop Until bMatchFound
End Sub
Unfortunately, this code have some bugs inside which blocks VS under some circumstances. This simply doesn't work well enough as we'd expect from filtered combo box.
Of course, this code have to be called from an event like this:
Code:
Private Sub cbox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboINtervencija3.KeyDown
Call CboxAutoComplete(cbox1, e)
I've tried with debugging but without success. Any idea?
-
May 16th, 2013, 12:26 PM
#2
Re: Correct ComboBox filtering
With out really studying your code, I can see right off hand what's your most probable cause for the flickering... The Do...Loop...Until loop. Is there any way that you can restructure it to For...Next loop? Because what if bMatchFound never happens? It will stay in an infinite loop. Also, I noticed that you're using Mid(), that should be replaced by getting the SubString.
-
May 16th, 2013, 12:51 PM
#3
Thread Starter
Fanatic Member
Re: Correct ComboBox filtering
 Originally Posted by dday9
With out really studying your code, I can see right off hand what's your most probable cause for the flickering... The Do...Loop...Until loop. Is there any way that you can restructure it to For...Next loop? Because what if bMatchFound never happens? It will stay in an infinite loop. Also, I noticed that you're using Mid(), that should be replaced by getting the SubString.
Yes... the code probably stays trapped into a loop.
About for...next loop, I dunno, Theoretically, possible but it asks to be combine with If...End if, I think. For...Next is "one-dimensional" loop and it wouldn't be so easy to implement.
-
May 16th, 2013, 01:49 PM
#4
Re: Correct ComboBox filtering
Might one ask why you're not using the perfectly serviceable autocomplete options built in to the combobox rather than attempting this rather strange and ever so slightly bonkers method? I can't see how the loop you're using ever resolves itself as it simply reads exactly the same values over and over again (to say nothing of the fact that it appears to be seeking whole texts but is activated by a single key press.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 16th, 2013, 01:53 PM
#5
Thread Starter
Fanatic Member
Re: Correct ComboBox filtering
Because I don't know that it exists! After a lot of googling I didn't find anything smarter.
BTW, this is .NET v2.0, VS05
-
May 16th, 2013, 01:57 PM
#6
Re: Correct ComboBox filtering
Might be helpful to start with that next time. You'll need someone with either a very good memory or a degree in Ancient History to be definitive about solutions for that! Is there no prospect of joining us in this decade code wise?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 23rd, 2013, 05:31 AM
#7
Thread Starter
Fanatic Member
Re: Correct ComboBox filtering
VB Code:
Public Sub AutoComplete2(ByVal cbo As ComboBox, ByVal e As KeyEventArgs)
Dim sTypedText As String
Dim iFoundIndex As Integer
Dim oFoundItem As Object
Dim sFoundText As String
Dim sAppendText As String
Select Case e.KeyCode
Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down
Return
End Select
sTypedText = cbo.Text
iFoundIndex = cbo.FindString(sTypedText)
If iFoundIndex >= 0 Then
oFoundItem = cbo.Items(iFoundIndex)
sFoundText = cbo.GetItemText(oFoundItem)
sAppendText = sFoundText.Substring(sTypedText.Length)
cbo.Text = sTypedText & sAppendText
cbo.SelectionStart = sTypedText.Length
cbo.SelectionLength = sAppendText.Length
End If
End Sub
VB Code:
Public Sub AutoCompleteCombo_Leave(ByVal cbo As ComboBox)
Dim iFoundIndex As Integer
iFoundIndex = cbo.FindStringExact(cbo.Text)
cbo.SelectedIndex = iFoundIndex
End Sub
=====================
vb Code:
Private Sub cbox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
AutoComplete2(cbox1, e)
End Sub
=====================
This one works fine
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|