I'm using C# TextBox with AutoComplete via a Custom Source of a collection of names, e.g. John Smith, Mary Jane, etc .. The problem is that if I type in 'John', I do see 'John Smith' in the dropdown. But if I start typing 'Smith', I no longer see 'John Smith' in the dropdown. I'd like to change this, and I think I would have to extend the TextBox class and provide my own function for matching data. I just don't know which TextBox function would need to be overwritten.
Secondly, another related issue I'd like to solve this is: The dropdown text is always left aligned. How can I make it center aligned ?
There is no directly way to do it because AutoCompleteCustomSource only allows prefix matching based on your input string.
From TextBox.AutoCompleteCustomSource Property
Use the AutoCompleteCustomSource, AutoCompleteMode, and
AutoCompleteSource properties to create a TextBox that automatically
completes input strings by comparing the prefix being entered to the
prefixes of all strings in a maintained source.
Best option seems to override your OnTextChanged event of your TextBox. The question below has a good example about it;
C# AutoComplete
Related
I have a textbox which sends some commands to an instrument. I added the AutoComplete feature to this textbox and things are going easier now.
What I am thinking to improve this, is to add a possibility that when user enters a command (just a text) while AutoComplete finds a match it also shows a description for that command.
At the moment, I have all the AutoComplete strings in a text file and I load it when the application starts. The textfile contains lines like this:
*IDN? #Query the instrument for identification
*RST #Resets the instrument
So what is happening in my application is that because AutoComplete is in SuggestAppend mode, the description of command also gets into the textbox (this will be the same if I only put it in Suggest mode)
What I need to know is how to force the AutoComplete to append the text while its
Does not add any text starting from # char while appending the suggested text
Trim() the text to avoid that spaces you see in the textfile source
UPDATE 1
Ok, I think the only way is to make a new class and inherit from AutoCompleteStringCollection And in this new class, somehow override the reponsible method for returning (appending) suggested text. I really have no idea what should I do:
class MyAutoCompleteCollection : AutoCompleteStringCollection
{
//How to override Get function of AutoCompleteStringCollection class?
//It is not avilable to override :(
}
UPDATE 2
I found out that methods in AutoCompleteStringCollectionare not overridable. I am looking for a way to change the way the [] method (to be honest I do not know what to call it!) works. Does someone have any idea about this?
UPDATE 3
When the text without #DESC goes into the textbox, I have a event handler for KeyDown which will transfer the command to the instrument.
Rather than trying to battle the autocomplete functionality that Microsoft has implemented, I strongly suggest that you use a multi-column combobox instead.
All of the ones that we have used support auto-completion, so you can store your command in the first column and have it be your value and then store your description in the second column.
There are a tremendous number of controls available for purchase (Infragistics, Intersoft, Syncfusion, etc) and you can probably find free or self-built versions on various sites such as CodeProject.
Going this route should save you a lot of time.
Staying with using the SuggestAppend method and loading your text file as a Custom Source for the Auto Complete feature of the TextBox.
You could use the Leave event of the TextBox to remove all text after # and trim the result:
private void textBox1_Leave(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.Remove(textBox1.Text.LastIndexOf("#")).Trim();
}
This way the description stays in the auto complete list, but as soon as you tab out or leave the textbox, only the command remains.
Here is the link which has the complete example for Autocomplete
http://technet.microsoft.com/en-us/query/chff42zw
As an answer to your update nr. 2:
The [] is actually an Index Property.
Your class implements an collection. You can get item at index X by using the collowing code:
var thirdItem = myAutoCompleteCollectionInstance[3];
Index properties can be created manually by using the code below, which you can use to customize the order by returning the items you want at the specified indexes.
public string this[int index] {get ...} {set...}
The AutoComplete property of Textbox is set SuggestAppend which means it adds both the text and the description. So you need to set AutoComplete property of Textbox to "Suggest" value only.
According to MSDN the Autocomplete property can take four enum values namely
The following are the values of AutoCompleteMode:
Append : Appends the remainder of the most likely candidate string to the existing characters, highlighting the appended characters.
Suggest : Displays the auxiliary drop-down list associated with the edit control. This drop-down is populated with one or more suggested completion strings.
SuggestAppend : Appends both Suggest and Append options.
None : Disables automatic completion. This is the default.
Try these values instead of subclassing the AutoCompleteStringCollection.
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.
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.
I need some help with a textbox:
The textbox is only allowed to contain letters.
how do I do this?
Check here : How to make Textbox only accept alphabetic characters, there are some different approached to choose from.
Edit: I see on the tags that we are talking asp.net, which the question I mentioned discusses the problem from a WinForms perspective. Might be that you want to do something similar using the onKeyPress event in JavaScript instead.
This article on w3schools.com contains an example code that may do exactly what you are looking for.
You can use regular expression validation for this and use following regular expression
"^[a-zA-Z]+$"
this is the easiest way to do this.
You could do this with javascript to filter out which charaters you accept, but if you can use Asp.net AJAX then you can add a FilteredTextBoxExtender to the textbox and only allow upper case and/or lower case letters.
To add a FilteredTextBoxExtender in Visual Studio 2008 you can do the following:
view the page in design mode and find the textbox
click the arrow on the right side of the textbox to open the TextBox Tasks menu and select Add Extender.
Then select the FilteredTextBoxExtender and click OK
Now your textbox should have a new property available in the designer. Make sure the textbox is selected and click F4 to open the properties designer.
Find the new property in the property designer. It should be named YOURTEXTBOXNAME_FilteredTextBoxExtender. Find the FilterType property and select UppercaseLetters or LowercaseLetters.
If you want both upper case and lower case letters you must edit the markup directly and set its value to "UppercaseLetters, LowercaseLetters"
Javascript/JQuery is your friend to make the UI only accept letters. However, you must make sure you validate the contents of the textbox on postback otherwise you can have bad data where people have bypassed the javascript.
I'm working with an Autocomplete box from the Silverlight Tookit (December release). As the user types, I use a webservice to return an ItemsSource containing a lookup of only the word that the user is currently typing into the AutoCompleteBox (as oppossed to the entire phrase, which is the default behavior). What I'd now like to do is if the user selects an option from the dropdown, I'd like to APPEND that option to the AutoComplteBox, NOT replace it as is happening now.
For example, if the final item should read as "John Smith". Currently, as the user types J-O-H-N, a list containing John will appear and they can select John as needed. As they move on to typing S-M-I-T-H, I've handled the Populating Event to pass only the final word in the .Text property to the Web Service and they will get a list that includes smith. So far, so good. However, when "Smith"is selected from the DropDown, the Contents "John" are REPLACED by the contents "Smith", leaving you with simply "Smith", not "John Smith" as we'd like.
I've attempted to deal with this by writing custom handlers for the DropDownClosing and/or SelectionChanged events. Neither of these appears to be the correct event to handle.
Can someone direct me where I might go to manage this behaviour?
Thanks
Seeing as you're already attaching to the on populating event and presumably kicking off requst to the server for the data, why not just append the 'John ' to all the items in the itemssource before you give it back? Then when you match it'll already be there.