Incremental Search

  • Thread starter Thread starter Christopher Weaver
  • Start date Start date
C

Christopher Weaver

What's the best way to go about an incremental search within a ComboBox?
I've looked at the FindString method, but before I go down that road, I'm
wondering if there's a component out there that would serve the same purpose
more easily.
 
Do you mean autocompletion, where you move down the combo box as the
user types successive characters until they find the correct entry?

Search this newsgroup for "combobox" and "autocomplete". You should
find pointers to some implementations.
 
"autocompletion" is another way of puting it, yes. I've just finished
downloading over 9,000 messages from this newsgroup and couldn't find a
single one that contained combobox and autocomplete. Seemed like a good
idea to me too.
 
I'm using Google Groups Beta, and searching on "combobox autocomplete"
gets me 18 hits. You'll probably get even more in
microsoft.public.dotnet.framework.windowsforms and
microsoft.public.dotnet.framework.windowsforms.controls.
 
I didn't find any.

But perhaps some out there would know how to iterate the list within a
ComboBox. I'm having trouble referencing it.
 
Here is a post by Jeffrey Tan, from one of the other newsgroups:

I am not sure of your meaning of "searchable combobox", can you show us

more information of your request? If you want to implement a
AutoComplete
combobox, you may refer to the 2 articles below:
"AutoComplete ComboBox in VB.Net"
http://www.codeproject.com/vb/net/autocomplete_combobox.asp
"ComboBox with AutoComplete 1.1"
http://www.csharphelp.com/archives3/archive502.html


Also, based on my knowledge, in .Net 2.0, Whidbey will introduce a new
AutoComplete feature for the build-in combobox control.
=======================================================
Thank you for your patience and cooperation. If you have any questions
or
concerns, please feel free to post it in the group. I am standing by to
be
of assistance.


Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no
rights.
 
This looks like what I'm looking for. I will study it and let you know if I
have more questions.

Thanks
 
I've just looked over the first example you referred me to and find that it
uses ComboBox.FindString. I didn't know such a thing existed! Don't know
how I missed it.


Thanks.
 
Hang on! I spoke to soon. This doesn't answer my problem completely.

I'm still having trouble with the ComboBox internal behavior that causes it
to move to the first item it can find in which the first character matches
the character last typed. The displayed item jumps as the ComboBox moves to
the item that it has selected then on to the one that my code selects. Is
there any way around this?
 
I can't seem to visualize your problem... can you explain what's
happening in more detail?
 
I've tried this with a DropDownStyle set to DropDown and DropDownList.

DropDown works well in that I'm able to use the SelectionStart and
SelectionLength properties to show the user which characters have been
acknowledged. Unfortunately, there is a visible jitter to the search
process. It appears that the component is implementing its default
behavior, which is to find the first item that begins with the character
just entered. It also appears as thought the highlighting of the selected
text is being removed and replaced with each key.

Also, if the user types too fast, characters are skipped in the search
process. I've tried implementing the solution to this expressed on the site
that you referenced in one of your earlier posts
(http://www.codeproject.com/vb/net/autocomplete_combobox.asp)
but it did not solve the problem.

All of the following processing takes place in KeyUp. I've omitted the code
that handles the backspace and excape keys.

private void cbTaskLookUp_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
string cbText = cbTaskLookUp.Text.ToUpper();
string cbItemText;
int cbTLUL = cbTaskLookUpInput.Length;
ComboBox cb = (ComboBox)sender;
try
{
if ((e.KeyValue < 65 || e.KeyValue > 90) & e.KeyValue != 32)//other keys
are handled elsewhere
return;
cbTaskLookUpInput += (char)e.KeyValue;
lblSoFar.Text = cbTaskLookUpInput;//for debugging
cbTLUL = cbTaskLookUpInput.Length;
LocateCBOItem(cb, cbTaskLookUpInput, cbTLUL);
}

finally
{
e.Handled = true;
}
}

private void LocateCBOItem(ComboBox cb, string InputString, int isLength)
{
int iFindString;
iFindString = cb.FindString(InputString);
cb.SelectedIndex = iFindString;
cb.SelectionStart = 0;
cb.SelectionLength = isLength;
lblSoFar.Text += " " + iFindString.ToString();//for debugging
}

Thanks for your help.
 
I think I see the problem.

The first link, the vb.net one, inexplicably implements the search
functionality as an event handler that can be attached to any combo
box. While this is nice and general, the event handler code runs _in
addition to_ any other code that would run as a part of a KeyUp event.
In particular, the combo box's normal KeyPress event code runs, then
your KeyUp event handler gets called. That's what is causing the
behaviour you're seeing.

I like the csharphelp version much better (the other link:
http://www.csharphelp.com/archives3/archive502.html ). It implements a
special kind of ComboBox by inheriting from ComboBox and _overriding_
KeyPress. This will have the effect of _replacing_ the normal ComboBox
KeyPress behaviour, which is more likely what you want.

By the way, .NET 2.0, which is due out next month, will include an
autocomplete feature for combo boxes built into the Framework, if you
can wait a month... :)
 
I checked out the component. This is what I was looking for. It doesn't do
everything I wanted it to do, but I can fix that. I see now how to override
an event handler. I downloaded it and installed the DLL assembly in my
toolbar. I also changed one of the ComboBoxes on my form to the ACComboBox
class. Unfortunately, even though it compiled and ran, after I shut it
down, almost everything on the form that was not inherrited from the
superclass was gone. I was using a TabControl with four pages. The
TabControl is gone and most of the code associated with it is gone.

Do you have any idea what would cause this or how to recover the previous
version of the form?
 
I've had this happen before. It happens when there's "something wrong"
with the code inside the "Windows Form Designer" generated section of
code, which is why they till you never to touch that.

In particular, I get this effect when I make a mistake in the
constructors of one of my derived controls (like the special-purpose
combo box), so the Designer can't create one, or an error in one of the
properties that the Designer is trying to read.

No, there's no way to get the code back, unless you have a version
control system. Debugging custom control is a pain. If you don't have
source code control, keep a back-up copy of your form somewhere.
 
Alright. Looks like I need hourly backups or a version control system.

Back to the component itself. It needs a little work, especially in the UI;
if the ComboBox isn't wide enough to display all of itscontents, the user
can't see what's happening because it defaults to showing the right side
instead of the left. Also, it doesn't stay in the dropdown mode as the user
is typing in, so even if the dropdown list is wide enough the user is still
lost.

I'm thinking that it may not be worth the trouble to fix it up if there's a
better version coming out in another month. But then on the other hand, I
need addtional functionality -- like 'fetch another batch of records if you
can't find what you're looking for here' -- so I might work with this
version anyway.

Thanks for the tip.
 
Back
Top