Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / C#

Word Completion Module In C#

Rate me:
Please Sign up or sign in to vote.
2.00/5 (2 votes)
14 Mar 2014CPOL2 min read 6.9K   3   1
How to implement word completion feature

Introduction

In this article I’ll explain how to implement word completion feature. This feature is very popular and very useful and comes with most good text editors (Don’t count the MS Notepad). You may also want to implement that feature in your application. If you do, then this article will help you in getting the simple logic behind this feature.

This article covers the following:

  1. About Word Completion
  2. Understanding the Logic
  3. Implementing the Logic
  4. Other Variants

About Word Completion

If you are familiar with Notepad++, then you must have used this feature. In word completion, we type a few characters of the beginning of the word and when we press "Cntrl + Enter", our word is completed automatically. This is the only thing that word completion deals with. Don’t mix word completion with intellisense of Visual Studio or code snippets, they are entirely different. So now, we know what word completion does.

Understanding the Logic

To create this module, we can use the following logic (not optimized):

  1. Create a list of keywords that we want to be completed.
  2. Monitor the user keystrokes to create a word that the user is typing into the text box.
  3. The moment the user presses space (or any other punctuation you want to set), do the following:
    1. Search the word in the keyword list
    2. If a match is found in the keyword list, then replace the text entered by the user with the keyword we found during the search.
    3. Move the input cursor to the end of the text.
    4. If search results do not match, then reset the word to empty.

That’s how our word completion will work. Let’s write a pseudo code for it.

  1. Start
  2. Create KEYWORD List and add all words in it.
  3. On Key Press in Text Area do
  4. word += character
  5. for each item in KEYWORD do
  6. if item.startsWith(word) then
  7. textBox.text = previous textbox text + item
  8. break
  9. else
  10. word=""
  11. End

Implementing the Logic

C#
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
 
namespace Demo
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            keywordList.Add("const");
            keywordList.Add("public");
            keywordList.Add("private");
            keywordList.Add("protected");
            keywordList.Add("extends");
            keywordList.Add("class"); 
        }
        string word = "";
        List<string> keywordList = new List<string>();
 
        private void txtArea_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Space)
            {
                foreach (var item in keywordList)
                {
                    if (item.IndexOf(word.ToLower())==0)
                    {
                        string temp = txtArea.Text;
                        txtArea.Text = 
                           temp.Substring(0, temp.Length – word.Length-1) + item + " ";
                        txtArea.Select(txtArea.Text.Length, 0);
                        word = "";
                        break;
                    }
                }
                word = "";
            }
            else if (!char.IsControl(char.ConvertFromUtf32(e.PlatformKeyCode),0))
            {
                word += char.ConvertFromUtf32(e.PlatformKeyCode);                
            }
        }
    }
}

Other Variants

  • In this version, I’m not considering the situation of multiple matches. But for real use, it is a must.
  • The search in the list is a costly operation so I’ll recommend you use a hashing technique for the search.
  • To make it more useful, you can generate a keyword list dynamically and update it with all the new words entered.

License

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


Written By
Student
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
majid hazari15-Mar-14 8:57
majid hazari15-Mar-14 8:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.