Click here to Skip to main content
12,625,016 members (39,028 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

20K views
1.6K downloads
28 bookmarked
Posted

IntelliSense TextBox in C#

, 3 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Auto Word Completion for Multiline Textbox (Minimal Intellisense)

Introduction

IntelliSense is nothing but a feature to predict the word when we are typing the starting letter of the word. We all are using Visual Studio, there we are typing the class name or namespace name, Visual Studio automatically shows the object list which holds the member & methods of that class / namespace.

This tip will definitely be useful to you to make your own IntelliSense TextBox in C#. This not a perfect IntelliSense but it has minimal ability to handle the auto word completion.

System Design

The system design of this application is very easily understandable. When we are entering text in TextBox, the popup listbox shows the list of starting letters of the last word in the string. The popup listbox items are loaded from the dictionary list which we created for the application. If the last word is not matching with list elements, the popup menu hides.

The popup menu should be shown the downside of the text line so here we need to get the text cursor position. For that, we need to call private extern static int GetCaretPos(out Point p) function of user32.dll assembly.

Using the Code

The AutoCompleteTextBox is the method for making IntelliSense for the given TextBox.

AutoCompleteTextBox Method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;

namespace IntellisenceTextBox
{
    class clsIntelliSense
    {
        [DllImport("user32")]
        private extern static int GetCaretPos(out Point p);

        /// <summary>
        /// <para>AutoCompleteTextBox is the method to popups when typing the keywords on TextBox to avoid the mistakes.</para>
        /// <para>Type 1:</para>
        /// <para>&#160;&#160;&#160;&#160;List&lt;string&gt; 
        ISList = new List&lt;string&gt;(new string[] { "SELECT", "CREATE", "TABLE" }); </para>

Calling the Method

The TextBox KeyUp Event is suitable for calling the above method. The following code is needed to be entered in the TextBox KeyUp Event.

txtInput.KeyUp += (s, e) => {
                List<string> DictionaryList = new List<string>(new string[] 
                { "AZEAL", "JOB", "INFO", "SOLUTIONS", 
                "CODE", "PROJECT", "FACEBOOK", "APPLE", 
                "MICROSOFT", "WINDOWS","SELECT","SET","COM"}.ToList());
                clsIntelliSense.AutoCompleteTextBox(txtInput, lstPopup, DictionaryList, e);
            }; 

Points of Interest

I figured this out while thinking about how Visual Studio's IntelliSense works.

History

This is preliminary. Later, we will think about the grammar & case sensitive listings to implement in this code.

Bug Fix: Listbox gets focus when pressing the up and down key on textbox while showing the listbox.

License

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

Share

About the Author

Anand Gunasekaran
Software Developer Azeal Groups
India India
Thinking Innovation. .. ... .... ..... ......

Technologies: .Net, Java, Php, Phython, Android, HTML

You may also be interested in...

Comments and Discussions

 
QuestionA couple of suggested fixes Pin
Mikaelg24-Feb-16 13:36
memberMikaelg24-Feb-16 13:36 
QuestionModify it to make it work when text is inserted in the middle Pin
Amit Hegde6-Dec-14 11:53
memberAmit Hegde6-Dec-14 11:53 
GeneralMy vote of 5 Pin
Jas 2415-Mar-14 2:28
professional Jas 2415-Mar-14 2:28 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161128.1 | Last Updated 3 Mar 2014
Article Copyright 2014 by Anand Gunasekaran
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid