Find as you type in C# - 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.

Related

How to use the drop side of DropButtons?

I'm having the hardest time using the dropdown list side of Krypton Dropbuttons -- specifically, I don't know how to register that I've actually selected something within the dropdown list.
In my project, I am using a dropbutton to apply a change over time, where simply clicking is immediate and the drop list has various increments of time -- I am using tweener-dotnet for the value change over time, as my calculus is awful and why reinvent the wheel?.
I am able to use the dropbutton as a regular button just fine. In addition to that, I've been able to set my ContextMenu in the dropbutton, so when I build the solution and click on the dropdown arrow, the list shows up. What I can't do, however, is figure out how to tell the code that I've actually selected something within the dropdown list.
I've spent a couple hours, collectively, staring at all three iterations of the DropButton examples, and none of them actually show what I'm looking for (seriously, Component Factory?!).
How do I use the dropdown side of the Krypton Dropbutton?
(For open source sake, I may just go back to a regular dropdown list and a regular button, but I really like the simplicity of the Krypton DropButton, and open source isn't really a priority with this project.)
I know that this is late, but I've been dealing with something similar.
For a KryptonDropButton you need to create a KryptonContextMenu, as #Roberto has stated.
Then I went to add a listener to the manu items that I've added to the assigned KryptonContextMenu and found out that the designer does not support the Name property (or maybe I was too blind to find it).
Anyway, I navigated to the designer file I found that those menu items were generated anyway, with default names.
Something like this:
I then went to rename those and added events like I would normally to respond to user selection, but I won't hide that this is overkill.
I'm about a year and a half late, but here's a radiobutton click event.
Just select the control and event at the top of the IDE.
Public Sub KryptonContextMenuRadioButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles KryptonContextMenuRadioButton1.Click
'do something
End Sub
Update 2018: the toolkit has been opensourced here: https://github.com/ComponentFactory/Krypton
Component factory hasn't been actively worked on since 2011. Its still closed source so that is an issue.
The Krypton Toolkit itself is a bit iffy in .net 4 and above. The KryptonContextMenu that you need with the KryptonDropButton is one of the bits that I've been having difficulty getting to work.

How to build a "word-by-word" autocomplete in WPF?

I already did some research and ended up with several autocomplete boxes which have one thing in common: they all match the whole expression which has been entered. Sometimes, they seem to be easily expandable, but at the end, they aren't.
However, I need a Textbox which allows the user to enter a word (e.g. "tag1"), displays a popup with some suggestions based on this single word, accept the suggestion with the return key and type in a new word (e.g "tag1 tag2") in the same textbox, with the popup popping up again. (I like the way CintaNotes handles this)
I need this for a tagging interface. It's often faster for the user to write the tags into a simple box, but sometimes, he needs assistance. This is what the autocomplete is for.
I've found some results, which don't work for my purpose (imho):
http://www.wpfpedia.com/item/details/743/wpf-autocomplete-textbox-control
http://www.codeproject.com/KB/WPF/WPF_Autocomplete.aspx
http://www.codeproject.com/KB/WPF/autocomplete_textbox.aspx
http://weblogs.thinktecture.com/cnagel/2011/01/autocomplete-textbox-with-wpf.html
Btw, I really like the way the Tag-Box for SO operates.
Does anyone have an idea? Is there a "out-of-the-box" - solution somewhere, which suits my needs but I didn't find? Or do I have to build one myself?
Thanks! :)
I think you mean a textbox which autocomplete for multiple words.
Like TokenizedTexbox on WPF Extended Toolkit.
this is the page: http://wpftoolkit.codeplex.com/wikipage?title=TokenizedTextBox&referringTitle=Home
Probably you would need to create your own Dictionary object of Key and Value pairs and hook that Dictionary to the textbox events and popup a suggestions dialog that displays the Value(s) from your Dictionary
Check this implementation out: http://code.google.com/p/kocontrols/downloads/list
You may inject your own Search algorithm and your own converter which converts the selected element to text, which you display in the TextBox. You will have to modify it slightly but I think that you might be able to solve your problem by basing your control on this implementation.
I never thought about this type of use case.
Can't you use different textboxes for the different tags? Something similar to how goole code does it?
If you have time, you can use the RichEditControl or TextBox and apply the same pattern used in Intellisense engine or Code Completation enabled editors: Hook the text changes events, Recogize context (last/current word) and display the options list on popup control. And optionally, on commit (detect acceptation or space key), apply the format to the word.

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.

Working with .net Combo Boxes

I have a form which has a Combo Box Control. I have selected the drop down style property to DropDown. I have also set the DropDown Width to 250.
I have set the auto complete mode to suggest and the auto complete source to listitems.
it works absolutely fine when i click on the drop down. but when i type in somethin, the auto complete mode activates a drop down which has a small width.
any help appreciate.
i wanna know how to increase the width of the auto complete drop down via code so that the list items are viewed properly.
I am using C#
Yes, this is by design. ComboBox uses the SHAutoComplete API function to implement the autocomplete feature. Note the declaration, the function takes a handle to the text box portion of the ComboBox. As such, it has no idea that it is actually providing autocomplete info for a ComboBox instead of a TextBox. Accordingly, there is nothing it can do to compensate for the non-standard dropdown width you use.
Well, that explains why it doesn't work. Fixing it is technically possible but quite ugly. You would have to run code in the KeyUp event and use EnumTheadWindows() to find the autocomplete window handle. Then you can use SetWindowPos() to make it larger. There is already code similar to this in ComboBox.cs (AutoCompleteDropDownFinder.FindDropDowns), use the Reference Source or Reflector to help you get this right. Good luck!
EDIT:
I removed my first suggestion to come up with a new link:
Actually its possible to control the width of the Autocomplete dropdown box, but its a little bit tricky and involves using win API extensively ...
Combobox too small when doing Suggest
Maybe this article that I wrote on Codeproject might help you in relation to the combo-dropdown boxes and a way that I hacked this...

Binding Textboxes

I have been binding textboxes in a winform with C# to a dataset. Whenever the data doesn't validate with what the database except it silently forces the focus to remain on the textbox. How can I catch the validation error and tell the user about it (and free the focus)? The BindingSource event OnDataError isn't fired.
I had a similar problem once. The focus remained in the textbox which was binded to some numeric database field when the user modified text in a textbox and then deleted it so the text property was an empty string. I solved it with something like:
textbox.DataBindings["Text"].NullValue = "";
It solved the problem for empty inputs. I don't know if it would be of any use in your case, but I'd be also interested in more general solution.
Here's also some related question on SO:
Data-bound TextBox: can't exit
Never rely on just what "Visual studio has done for me" if you don't fully understand what it's doing. I would strongly urge you to take the time and figure out how to do what it is you want to do all by yourself (meaning without designer generated code). To get you started, there are some events on the TextBox that can help you out. Start here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validated.aspx
Specifically the validating and validated events should be what you're looking for.

Categories

Resources