Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

Intellisense: Home-Made - Based on Visual Studio

Rate me:
Please Sign up or sign in to vote.
3.22/5 (30 votes)
24 Feb 2006CPOL3 min read 80.4K   2.9K   94   5
Re-create Microsoft's intellisense technology to use in your own applications
Sample image

Introduction

This article shows how to re-create Microsoft's Intellisense technology used in Visual Studio.
Intellisense has been around in source-editors for years now, and with it developers can not only type faster, but use it as a quick reference to the members and methods of a class. This replaces 1000+ page API reference books, making programming much simpler.

Background

I first used this source with my HTMLEditor program. I obtained reference foam SmallGuy78's article on "DIY Intellisense". I then adapted it for my own needs.

The source you will find here has various differences, the largest of these is that it overrides the default behaviour of listboxes where they search for the character typed in, then when you press another, it searches for that character. Instead it adds them all into a buffer and searches for the whole string ( i.e. "acr" instead of "a" "c" and "r").
It is also more closely modelled to the Visual Studio 2005 intellisense system.

Using the Code

The sample is a very simple text editor using the intellisense feature. It uses a list of HTML tags in this case, and you can easily edit it to suit your needs.

Points of Interest

A lot of the code is fairly straightforward, however there are a few obscure methods not many will have used before (such as richTextBox.GetPositionFromCharIndex). The actual system takes place within 3 event handlers, two of which are only very short.

The first event is to capture a key press which will trigger the intellisense listbox to open (in this case, I have chosen C#'s class accessor (".")). This goes in the richTextBox.PreviewKeyDown event like so:

C#
if (e.KeyCode == Keys.OemPeriod && e.Shift == false)
{
    CurrentTagStart = rtbInput.SelectionStart;
    Point p = rtbInput.GetPositionFromCharIndex(rtbInput.SelectionStart);
    p.Y += (int)rtbInput.Font.GetHeight()*2;
    lbIntelli.Location = p;
    lbIntelli.Show();
    ActiveControl = lbIntelli;
}

Here's a quick run-down:

CurrentTagStart saves the position of the class accessor. This will be used later when we insert the selected intellisense item and add it to our code starting from this position.

We then make a point object, and call GetPositionFromCharIndex. This takes the selected text (just after the class accessor in this case) and returns a position in pixels. This is basically the position of the carat (blinking line thing which denotes where your text will appear). We then modify it a little, because having the listbox go there would cover our text as we type. To make it work for any font, we take the richTextBox's font height and multiply it by two. This can, of course, be touched-up to suit your individual needs. You then simple assign the listbox's position from our point and show it.

At this stage, we want the user to be able to type normally, and our home-made intellisense will search for the best item based on what has been typed. For this reason, there is a buffer to hold what has been typed since the listbox was opened. It is global to our form class and called intellisenseBuffer.

The next event which will be used to do this will be the listbox's KeyUp event, as follows:

C#
Object ObjToSelect = new Object();

if (e.KeyCode != Keys.OemPeriod)
  if (e.KeyCode != Keys.Back)
  {
    bool startswith = false;
    intellisenseBuffer += e.KeyCode;
    foreach (object obj in lbIntelli.Items)
    {
            string str = obj.ToString();
           if (str != "")
          {
                startswith = str.StartsWith(intellisenseBuffer, true, null);
                  if (startswith == true)
                {
                    ObjToSelect = obj;
                    break;
                }
            }
    }

if (startswith == false)
{
    intellisenseBuffer = "";
    lbIntelli.Hide();
}

lbIntelli.SelectedItem = ObjToSelect;
}

if (e.KeyCode == Keys.Back) { rtbInput.Text.Remove(rtbInput.SelectionStart-1,1); }

if (e.KeyCode == Keys.Return)
{
    rtbInput.Select(CurrentTagStart+1, rtbInput.SelectionStart);
    rtbInput.SelectedText = lbIntelli.SelectedItem.ToString();
}

This is by far the longest event handler in this sample.

In the first part, you create an object which we will use to represent the listBox item we should choose based on the user's input. We then perform some basic ifs to check for regular text.

We then update our intellisenseBuffer with the latest key input, and search all items of the listbox, and see if they start with the text in our buffer. If we hit an item which does, we select that item. If there are no items starting with our buffer, then we close the listbox.

The last few lines are simple enough, if backspace is pressed then remove a character from the rtb, if enter is pressed then add the selected value to our rtb starting from where the class accessor was pressed.

The last event handler is incredibly simple, the listbox's keypress event simply passes input it captures to our rtb:

C#
private void lbIntelli_KeyPress(object sender, KeyPressEventArgs e)
{
   rtbInput.SelectedText = e.KeyChar.ToString();
}

History

  • 25/02/06: Submitted sample to CodeProject

License

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


Written By
CEO Synap
United Kingdom United Kingdom
Founder & CEO of Synap, an online education platform that uses machine learning to help people learn more in less time.

Software developer, main languages currently are Objective-C, MySQL and Javascript, though I got started on C++, C# and PHP.

Comments and Discussions

 
GeneralHas some issues. Pin
Diamonddrake6-Apr-10 7:30
Diamonddrake6-Apr-10 7:30 
GeneralIts working but not correctly Pin
Umaid126-Sep-09 5:12
Umaid126-Sep-09 5:12 
QuestionUse with Ctrl + spacebar Pin
leminh048818-Jun-09 21:24
leminh048818-Jun-09 21:24 
AnswerRe: Use with Ctrl + spacebar Pin
wuchen_net7-Jun-11 16:06
wuchen_net7-Jun-11 16:06 
AnswerRe: Use with Ctrl + spacebar Pin
Awesh Vishwakarma10-Mar-16 16:19
professionalAwesh Vishwakarma10-Mar-16 16:19 

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.