Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on windows applications. I have a text box with autosuggestion on. i have added few values in autosuggestioncollection. if i type first letter of the value,it is suggesting the added values but if i type last few letters of the value. I need it to show the suggestions even if i enter the last few letters of the value.

What I have tried:

C#
AutoCompleteStringCollection mycollection = new    AutoCompleteStringCollection();
mycollection.Add("TEST12345");
mycollection.Add("TEST13456");
mycollection.Add("TEST22222");
mycollection.Add("TEST99999");
textBox1.AutoCompleteCustomSource = mycollection;
Posted
Updated 7-Mar-18 21:48pm
v2

1 solution

Well... This is normal behaviour of control with AutoCompleteCustomSource[^], because it automatically completes input strings by comparing the prefix being entered to the prefixes of all strings in a maintained source.

If you would like to change it, you have to create textBox1_Click event and there you can implement a method which compare entered string with a string of custom source. For example:
C#
TextBox txt = (TextBox)sender;
if(txt.Text.Length<2) return;
string s = txt.Text;
s = txt.AutoCompleteCustomSource.OfType<string>.FirstOrDefault(x=> x.Contains(s));
if(s != string.Empty) txt.Text = s;


It works as expected, when below properties of TextBox are set as shown:
C#
AutoCompleteMode = SuggestAppend;
AutoCompleteSource = CustomSource;
 
Share this answer
 
Comments
Kishore yasoju 12-Mar-18 1:58am    
I got an error like: System.Linq.Enumerable.OfType<tresult>(System.Collections.IEnumerable)' is a 'method', which is not valid in the given context
Maciej Los 12-Mar-18 3:01am    
Very strange... It works form me (a framework in project is set to 4.5).
Try to replace this:
s = txt.AutoCompleteCustomSource.OfType<string>.FirstOrDefault(x=> x.Contains(s));

with:
s = txt.AutoCompleteCustomSource.Cast<string>.FirstOrDefault(x=> x.Contains(s));

Let's see if it helps.
What version of framework you use in your project?
Kishore yasoju 12-Mar-18 3:08am    
I tried that Maciej, but i got the same error
"System.Data.EnumerableRowCollectionExtensions.Cast<tresult>(System.Data.EnumerableRowCollection)' is a 'method', which is not valid in the given context". By the way I am using Visual Studio 2008 and I am working on a windows CE application.
Maciej Los 12-Mar-18 3:14am    
VS 2008 supports .NET framework 3.5. What is yours?
Both methods are available in VS 2008 for WinCE. See:
Enumerable.OfType(TResult) Method (System.Linq)[^]
Enumerable.Cast(TResult) Method (System.Linq)[^]
Kishore yasoju 12-Mar-18 3:17am    
Mine is.NET framweork 3.5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900