C# Textbox AutoComplete: Limit to ~50 suggestions - c#

I'm working in C# with a textbox that acts as input for a database (Access SQL) record lookup by id number. I want to be able to use AutoComplete on the text box but with some limitations.
The big issue is that the number of ids in the system is on the order of thousands so instead of filling the AutoComplete box once with all of them, I need to monitor what is in the textbox and only show the autocomplete suggestions when there are ~50 or fewer choices.
Currently, I am doing this query on each KeyDown: SELECT count(*) FROM Table WHERE id LIKE 'textbox.text%'
When the count is less than 50 I fill the autocomplete with the results from a SELECT id version of the above statement. This has led me to several problems, most seem to be C# quirks I don't understand.
1) When I clear or add to the AutoCompleteCustomSet within a single KeyDown event, the actual key pressed does not get added to the string (i.e. normal text box input behavior does not occur).
2) I tried separating the AutoCompleteCustomeSet update to a different event (KeyPress or KeyUp) but this either resulted in a crash, or the autocomplete display would only show briefly before being hidden.
I feel like this problem must be common and I am just going about it the wrong way. Can anyone offer some advice? Thanks!
EDIT: this is Windows Forms
EDIT2: A top 50 select doesn't fix the problem that as the user types (and potentially backspaces and re-types) the top 50 will change.

Did you try the TextChanged event instead ?
I would expect that event to be fired AFTER the textbox has been updated, thus avoiding the quirks you mention.

Can't you just SELECT TOP 50 FROM Table WHERE ...?

Would just limiting your suggestions to 50 work for you?
When your selecting your suggestions you can use a query like this:
SELECT TOP 50 * FROM YourTable WHERE.....

The reason you get crash may be that you get a race condition when replacing the autocomplete source, which is used by a background thread to compute candidates while you are typing (or you put null instead of String.Empty in the candidate list)
Before Windows Vista, the Autocomplete object match candidates with prefix only, so don't populate candidate strings not beginning with the typed string.
Use IAutoCompleteDropDown::ResetEnumerator to reset the candidate list.

Related

Winforms, can I use a text box here instead of a combobox? (dropdown list with over 15k options)

For the last few weeks I've been building a product demo for work which includes a winform to enter new customer enquiry information. One of the form elements is a text box which, for the sake of ease, I haven't imposed any validation on so far. However, I now need to make it so that the user can only enter a valid location from an sql database table (containing around 15k streets).
I'm still quite new to C# programming. My first thought was that I should change my text box to a combobox but I seem to remember that when you click on a combobox all the options in the list appear before you've typed anything. Since our computers are slow and there's so many options, I don't really want to flood the screen so I was wondering if there was a way I could continue using my text box and onkeypress (probably the tab key) a dialogue pops up with all the closest matches from the list, prompting the user to select a valid option?
If not, is there a way to stop my combobox from showing the option list until prompted?
I would not think a combobox is not well suited for that many items.
The way I have approach this is to use a separate list view to show matches. You could probably put matches in a drop-down style borderless window, but I find that more complex and may be difficult to make the interaction work well.
I would just have the streets in a separate list view control and apply a filter to that.
Make sure the view is resizable, I find it very frustrating when working with old window controls where the list is tiny due to it being written for 640x480 screens, and does not allow resizeing.
Keep performance in mind, when searching with each key-press you might want to fetch all records and do the search in memory rather than making a sql query for each key.

Winforms performance issue with Numeric up down and list box

In my Win forms app, i have a Numeric up down and a list box.
when i increase/change value in numeric up down, that many rows are added in list box.
for ex. when i have value '1' in my numeric up down and i change the value to a larger number say '300', it adds 300 lines in the list box but it takes hell lot of time to do it.
is there any way by which i can reduce this time.
any help would be appreciated. thanks.
You can try creating the necessary data (or controls) that will be added first, and maybe do it in a Parallel.ForEach and afterwards add all of them.
Otherwise, only something like data virtualization available in WPF would be of help but i don't know if that exists in WinForms.
Try adding them "on demand". Add only the necessary few that are visible plus a few more and when the use scrolls down, add some more and so on.

Elegant Way For This UI to Work?

in VS .NET 4.0 :
So I have some ComboBoxes that query a server for data that match the text in the ComboBox, when the ComBobox text is changed(event).
I want the ComboBox to drop down when the query is done so that the user can see the results and perhaps use them.
The problems :
Rapid-fire queries => Rapid-fire dropdown menu (not very helpful)
Changing the contents of the dropdown causes the text input cursor to reset to the first position in the field. Typing more than 3 characters before the cursor is forced to the first position is very hard. - this is the default behaviour of ComboBox as far as I know
Pressing 'down' to scroll through the list of suggestions causes the very first entry at the top of the dropdown to be put into the text field. Preferably that doesn't happen unless the user hits Return or clicks it.
What I would like :
Overcoming the dropdown-cursor repositioning issue. (this above all)
Scroll list with arrow keys without being forced to select anything.
Limit the rate at which the server is queried.
I have my own solutions in place, but they look terrible at the moment.
I'm handling dropdown and query timings with Timer objects, but my implementation prevents a query from going on while the user is typing, and the dropdown doesn't always occur at the right time if the user types too fast or too slow.
Thank you to those that try to help!
I'm not sure if Iunderstand exactly what you want but personally I'd consider skipping the Combobox and use a ListView instead. Possibly a hidden one that I'd only show at the end of the query and then a Textbox that the user can type in.
Feels like you're trying to use the Combobox for something it's not meant for.

How to select specific combobox item with multiple keystrokes? First few characters of item

Windows explorer in XP will allow you to make a file selection based on typing a few characters. I would like to know if there is any simplistic .net feature I can use to mimic this behaviour in a combobox? I think I've seen this happen in comboboxes before, and would like to know if there is a property I can use?
I know I could develop code around the "Key" events, but can't justify spending the time on it.
For example: In a folder which contains "Apple.doc, banana.doc, cherry.doc, cranberry.doc" then typing "b" will select "banana.doc", typing "c" will select "cherry.doc" but typing "cr" will select "cranberry.doc"
Thanks in advance
G
Have a look at ComboBox.AutoCompleteMode.
Thanks to Daniel for the above answer.
I would also like to point others with a similar queries to AutoCompleteMode which explains the details of each AutoCompleteMode value.
In summary:
None - Disables the automatic completion feature for the ComboBox and TextBox controls.
Suggest - Displays the auxiliary drop-down list associated with the edit control. This drop-down is populated with one or more suggested completion strings.
Append - Appends the remainder of the most likely candidate string to the existing characters, highlighting the appended characters.
SuggestAppend - Applies both Suggest and Append options.

Find as you type in C#

Im trying to emulate a 'find as you type' function like that of the address bar ("awesome bar") in FireFox. I want a suggestion box to appear below a textbox, and the suggestion box contains strings that contain what is in the textbox. I looked at the autocomplete feature of a normal WinForms textbox, but it seems to only search the beginning of the strings.
Has anyone here built or have experience with implementing something like this?
edit:
Some clarification- It is a WinForms project.
It needs to search inside a string, not just the beginning (which is what a normal textbox does if i recall correctly). And the suggestions should be displayed in a popup like a textbox autocomplete.
You need to handle the TextChanged event for your text entry field, and when the text changes, start a new thread running that will apply the new search. If the text changes before you get your results back, just kill the thread. If the thread returns results in time, display them.
You can get slightly more advanced (e.g. wait for a short time after the text changes so that the user can type a word without you triggering off loads of useless threads) but essentially that's it.
There was a discussion earlier on this topic where the author concluded that you are better off doing the whole thing yourself.
How can I dynamically change auto complete entries in a C# combobox or textbox?
I did something vaguely similar, but more like the iTunesĀ® search box than the Awesomebar. My control used the textbox to actively filter a grid; so it wasn't for autocompletion.
...but... basically I had a DataView of all eligible items, whenever the TextBox's Text changed I'd update the Filter to hide all non-matching items. It worked well and might suit your needs for filtering the data--but not sure how to go about using it as an AutoComplete source for the textbox.
I have done such a thing for an app of mine not too much time ago.
What I did is make my search function in a new thread, so every time I typed a new letter, it called the search function in another thread, so I could keep on typing.
I can post some code if you need, but this should be enough to get you started. :)
Hemmed and hawed about deleting this after I noticed the OP edit mentioned winforms, but I think it'll be useful to anyone who comes here looking for the same but for asp.net apps.
Just because nobody has mentioned it yet, for a webforms app you absolutely want to do this with ajax (.net controls or pure JS, your choice). The feature is often called "autocomplete" and the one thing you don't want it to be breaking the seamlessness by making server round trips at the page level.
I suggest you look at this and this.
I've used Search As You Type in C# and How do I make a Textbox Postback on KeyUp?
Basically you use the keyup action to call a postback thats attached to the trigger to the update panel. then you do your update in the textbox_changed event with the dataview or whatever your backend looks like.

Categories

Resources