"Original" enables. Net CF to implement the AutoComplete Function

Source: Internet
Author: User

Preface

Recently, we have been developing Winform. There is a scenario for querying bus information. You can enter some site names in the Start station input box, and the system will automatically give him suggestions and prompts, similar to auto-completion and smart prompts in Google, in Winform or WebForm ,. net provides a good solution, and you can even directly use the AutoComplete control in Ajax.

However, the problem arises. This function has to be transplanted to the WM platform. We usually know it when using WM mobile phones. Inputting is a very troublesome task, I always like dot, slide, drag ...... I just don't like input ...... Generally, you can search for public transit sites. In Google, you can enter several keywords to "be" completed. Maybe, I sometimes don't remember the full name of the site >_<||.

I wanted to use Combox directly and found that it didn't have the auto-complementing function. Google Baidu did not find a great "Wheel". So I only had to create a Combox with the auto-complementing function myself.

Body

Although you have not officially studied the AutoComplete function, you can also copy it on your own. My ideas are as follows:

  1. First fill in the site list and wait for the keywords entered by the user
  2. Users enter keywords. Each time they enter a word, they can traverse the site list, search for qualified sites, and add them to the "alternate list"
  3. You can use the drop-down button of Combox to expand Items and view the "Suggestions" site provided by the system.
  4. The user modifies keywords, and the system re-compares and fills in the "alternate list"
  5. The more detailed the user keywords, the more accurate the alternate list

With the idea, it seems pretty good, so start Coding. Like all WM projects, on the design interface, drag and drop a Combox control to the form and set the Combox Dock to Top. That's easy. Of course, here we keep everything simple. I didn't load the site database into the program (in fact, after the customer downloaded the program, it came with an SQLite database to store the site information ), after reading my code, you can understand it.

Define two generic variables to store List information:

 

Code

 1 /// <summary>
2 // This List is used to store the site List.
3 // The site may be string or integer, depending on the actual situation
4 /// </summary>
5 private List <string> liStops;
6 /// <summary>
7 // This List is used to store candidate sites, that is, the recommended items for Automatic completion.
8 // similar to google candidate projects
9 /// </summary>
10 private List <string> liSuggest;

 

 

Key AutoComplete code (I personally think it is not possible to copy the code ...... CbInput is a Combox Control ):

AutoComplete code

 1 /// <summary>
2 // This method is used to automatically complete the Items of the Combox, so that the project and user input of the Items
3 // match the keyword
4 /// </summary>
5 /// <param name = "text"> keywords/words entered by the user </param>
6 private void FillListBox (string text)
7 {
8
9 foreach (string item in liStops)
10 {
11 if (item. IndexOf (text)>-1) // if the site contains keywords, add the site to the alternate Items
12 {
13 liSuggest. Add (item );
14}
15}
16
17 foreach (object obj in liSuggest) // fill the items in the alternate List in Combox, making the user feel that Combox is automatically filled
18 {
19 cbInput. Items. Add (obj. ToString ());
20}
21}

 

Then, add the following code for the Combox KeyUp event:

 

Code

 1 private void cbInput_KeyUp (object sender, KeyEventArgs e)
2 {
3 liSuggest = new List <string> ();
4 liSuggest. Clear (); // Clear the last alternate Project
5
6 cbInput. Items. Clear ();
7 string text = null;
8 switch (e. KeyCode) // obtain the code of the button pressed by this user
9 {
10 case Keys. Left:
11 case Keys. Up:
12 case Keys. Right:
13 case Keys. Down:
14 case Keys. Delete:
15 return;
16 default:
17 text = cbInput. Text. Trim ();
18 FillListBox (text );
19 break;
20}
21}

 

Note: to simplify the operation, fill in the site list in the Form_Load event:

 

Form_Load code

 1 private void Form1_Load (object sender, System. EventArgs e)
2 {
3 liStops = new List <string> ();
4 for (int I = 0; I <299; I ++) // when I define a form Load, fill in the site List. In reality, we need to select the best time to fill this List
5 {
6 liStops. Add (I. ToString ());
7}
8}

 

OK. So far, A Combox with AutoComplete function in. Net CF has been implemented. The effect is as follows. It feels pretty good ......

 

Okay. The demo is finished. In fact, it is found in use,It is an interesting question about how to use the site list and how to put it into the Items of Combox. It will affect the user experience. For example, there will be significant latency during dynamic loading, all for discussion ......Jack writes it here for the time being.

Finally, I recommend Lao Zhao's article "Does generics really reduce performance?" Although it's a bit old, it's worth a product!

Maybe you should also look at this article from dalelane's friend here, maybe, this is the right AutoComplete: http://dalelane.co.uk/blog? P = 166

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.