Click here to Skip to main content
Click here to Skip to main content

DIY Intellisense

By , 12 Jan 2004
 

Sample Image - DIY-Intellisense.gif

Quick start

Download the project, compile and run it. Go to the menu and load %systemroot%\Microsoft.NET\Framework\v1.xxx\system.dll. Type System then a "." in the richtextbox. A listbox of classes/namespaces will appear.

Note: The namespace icon is used for both namespaces and types with no icons, this is laziness on my part.

Introduction

If you're a programmer and not familiar with intellisense, the chances are you've been hiding in a cave on a dialysis machine for the past 5 years. Intellisense is a Microsoft trademark, but the concept is available by dozens of code editors. In simple terms, it's a drop down list that appears when you type a recognized word into your code editor. For example, typing in System, then a "." inside Visual Studio .NET will show a drop down list of types for the System namespace. Another example can be found in Macromedia Dreamweaver - typing in a particular HTML tag and then a space will cause a list of attributes for that HTML tag to appear.

A fuzzy discussion about the history of intellisense can be found here.

In this article and the accompanying code, I'll demonstrate how to implement an intellisense-like system (I'll call it auto completion from now on) for a RichTextBox, triggered by the user pressing the "." key.

The complete project has the following features:

  • Auto complete from a drop down list
  • Easy-to-customize storage of the items that appear in the drop down listbox

I also threw the following in for good measure:

  • Icons display in the listbox, next to each item
  • Simple no thrills tooltip appears with a parameter list when you type a method name and a "("
  • Populating the autocompletion lookup tree through loading an assembly.

All of this doesn't look too much on virtual-paper, but the source code is almost 900 lines (including the form component layout code). I'll go through the code bit-by-bit, but first, the requirements.

The requirements

The project is a simple form with a RichTextBox and a main menu. The RichTextBox control hasn't been subclassed in any way, it's had its font changed to Courier and that's it.

The basic requirement is that when a user types a word, and then a ".", and the word is a known word, a list of items appear in a ListBox, underneath and to the right of the "." they just typed. From this, they can select an item from the ListBox, whose text is then pasted onto the RichTextBox, completing the word they were typing.

The form also has a TreeView, used for storing the items to be looked up, a ListBox, to display the items, and a text field which is used as cheap and cheerful tooltip for the method parameter lists.

Pressing the magic "." key

The RichTextBox's OnKeyDown event checks for an OemPeriod (I'm assuming this works on all machines being used). When a dot is pressed, the previous word is retrieved using the getLastWord() method.

This word is then checked to see if it exists inside the tree that we use for storing our lookups (described next). If it's found, the ListBox is populated with these items. populateListBox() manages this, returning a boolean for whether it populated any items or not. After this, the ListBox is positioned.

Luckily, the RichTextBox features a method that makes this task simple. Using the RichTextBox's GetPositionFromCharIndex(), we can get a Point struct for the current caret position. Using this, we position the ListBox's X coordinate according to the .'s and the Y is the same, but the height of the font is added (I tweaked it slightly, removing an extra 2 pixels for Courier); it is then positioned to appear underneath the ".". Visual Studio .NET positions the box above or below the current line, depending on the line position you're at in the text editor (the box appears above if you're on the last line of the text editor). I haven't added this feature, but I imagine it wouldn't be too hard to do.

The lookup tree

As I mentioned, all the items that the autocomplete looks for are stored in a TreeView. For the type of code completion I'm implementing - Namespaces/classes/methods - storing the items in a tree makes perfect sense. For something like HTML code completion, this would also be the ideal candidate, the root nodes could be the core set of HTML tags, and the nodes underneath would be the attributes. For a flat list, the TreeView could possibly be got rid of, and the ListBox used on its own.

When I started the project, I started to create my own tree implementation for storing the items. I then realized there was no point doing this as .NET provided the structure for me in the form of a TreeView. It's got the overhead of a being a control, however it allows you to easily add and edit items inside an IDE like Visual Studio .NET, Borland C# builder, #develop, etc., making the job quicker.

Code completion behavior

Once the "." is pressed and the drop down list of items appear, the following logic is applied:

  • Focus is sent back to the RichTextBox, which traps the up and down key. These keys are then used to move up and down the ListBox.
  • If an item is a selected, and the user presses the Return, Space or Tab key, then the ListBox of items hides, and auto completion of the word is fired.
  • When the ListBox is visible, any alpha-numerical key can be pressed. Any other key except Backspace or Shift hides the ListBox.
  • Pressing the delete key hides the ListBox if the character being deleted is a "." .
  • When any alpha-numerical key is pressed, a string is concatenated from the typed characters.
  • The ListBox is searched to see if any of its items begin with the combination typed, if they do, then this item is selected.
  • Double clicking on an item in the ListBox fires the autocompletion.

All of this is checked in the RichTextBox's OnKeyDown event.

Finding an item in the tree

Once the previous word that was typed is found, the TreeView is searched to see if the word can be found. As the word can contain a full namespace, it is broken down according to the dots, and the FullPath property of the node is checked against a concatenated string. This is done inside a recursive function. When a node is found, this is stored in a member variable, which is then used to populate the ListBox with all the child nodes. The nodes are sorted by their text by creating an array of a custom type, which implements IComparable. This array is populated from the TreeNode, then sorted and the ListBox is populated using it.

Obviously, sorting the items each time they're populated is quite a slow way around, a preferred method would be to sort the tree when it's populated. A sorteable TreeView would be a whole new article though (has anyone implemented one?!), this suffices for now.

Autocompleting the text

This is a fairly simple procedure - everything before the dot is stored in a string, and everything after the dot is stored in another string, and a new string has the selected ListBox item's text appended in the middle of the two. The RichTextBox's text is then replaced with this new text. I haven't tested this method with large amounts of text, it may require customizing the RichTextBox if flickering starts to occur.

Extras

To demonstrate the autocompletion, additional 2 methods are in the source, for reading an assembly and populating the tree with its types. The readAssembly() method loads an assembly in and cycles through all its types, adding nodes to the tree for namespaces, classes, methods, fields and events. Each type uses addMembers() to have its fields, methods and events added as nodes.

Each ListBox item features an icon to indicate what type it is - this type is stored inside each TreeNode's Tag property. The Tag property also stores a method's parameter list as a string, so we assume the node is a method if the Tag property is a string. The display of icons in the ListBox is done using the GListBox class.

As mentioned above, when a method is typed and the left bracket '(' key is pressed, a tooltip is displayed with the parameter list for the method. I couldn't find any free custom tooltip implementations for this, so I used the displaying of a TextBox with the details in, positioning it the same way the ListBox of items is positioned.

Conclusion

I don't want to over-bloat this article by going into great depths about each method, so I'll stop here. Hopefully it's useful to people. I want to point out that it's proof of concept code rather than any finished product; I haven't tested the TreeView to see how well it weathers with 1000s of nodes, and there are minor things I could improve in it (given a team of developers, a big paycheck and the knowledge that the biggest company in the world was backing me!)

Known Bugs

  • You may get an index out of bounds error when you go to view the form designer. This is a problem with the GListBox, I didn't fix the error. It's only at design time, the project still compiles fine.
  • Using the tab for autocomplete adds a tab to RichTextBox (even though e.Handled = true...something for me to look at on a rainy day).
  • Tooltip doesn't stay in front of the form.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

yetanotherchris
Web Developer
United Kingdom United Kingdom
Member
London based C# programmer.
 
I maintain my own pet C# site http://www.sloppycode.net in my spare time.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHave you looked at re-implementing this using the June 2012 CTP of Roslyn?memberChris Sells23 Jun '12 - 14:18 
It would seem to be easier to implement your algorithms with support for the C# compiler team and it would be useful to have a good Intellisense implementaiton build on top of Roslyn.
 
Roslyn June 2012 CTP: http://msdn.microsoft.com/en-US/roslyn[^]
Chris Sells
http://www.sellsbrothers.com/

SuggestionFix for design viewmemberStimphy21 May '12 - 18:52 
In design view I got the error you reported in the bugs section.
 
Alter line 45 of the control to the following to fix the problem.
 
if (e.Index != -1 && this.Items.Count != 0)
 
Thanks for your contribution!
Regaurds,
Stimphy

GeneralIntellisense helpmemberMember 443453629 Mar '11 - 4:12 
Hello
 

The intellisence tool is very good and give more thoughts to develop
 
When i use this intellisence tool to use all namespace dlls(system.dll,mscorlib.dll....etc) and try to use the namespace in the editor like System.IO.Compression, I am not getting this Compression after System.IO. but it is appearing at System.Compression
 
Actually in the VS2008 or in VS2010 it is like this System.IO.Compression
 
How to solve this problem , please help me out
GeneralBreaks out of Intellisense on "shift" keypressmemberG2Chris5 Jan '11 - 12:15 
Is anyone else having this problem? When the intellisense menu is open and you hit shift, it hides the menu. I've tried a few coding solutions so far but nothing has worked... anyone else getting this bug?
 
Thanks!
GeneralRe: Breaks out of Intellisense on "shift" keypressmemberMatthew Rhoden5 Jan '12 - 8:41 
Yeah I noticed that too, if you haven't fixed it already, go to line 385 and paste this below the comment "Hide the member list view"
if(e.KeyCode != Keys.ShiftKey)
That way it conditionaly hides the intellisense window.
GeneralTo fix the font width issue for the textboxmemberRaidenFlyboy7 May '10 - 9:09 
Graphics graphics = this.CreateGraphics();
tb.Width = (int)Math.Ceiling(graphics.MeasureString(node.Tag.ToString(), tb.Font).Width);
 
This will give a reasonable length textbox for whatever font or point size....
GeneralHi,Some Data Base Issues When i try to bind the dataset to the listboxmemberpuja1618 Apr '10 - 20:09 
Dear Author,
Your work on this code is outstanding.It Helped me a lot.But I have an issue.As per my knowledge of the code,u have overiden the listbox control,u have also overriden the drawitem event of the listbox when uses listboxitem to write the text onto the listbox.But when i use the datasource to bind to the listbox,i get an exception.For time being i have added the items from dataset to the listbox by itterating through the dataset.but if there are thousands of data,then this code will be time consuming to read data from dataset and then add it to listbox,so please suggesst how can i overcome this problem.please help.Urgent
 
Thanks & regards,
Puja Badamikar.
GeneralThis is an amazing feat!memberDiamonddrake6 Apr '10 - 9:00 
I appreciate your work here. I am working on an IDE for assembler language and i have been working on an Intellisense Myself. but this helps tremendously. using the treeview to hold the data is genius. i can scrap my list of classes with lists of classes that holds lists of classes and go with a treeview Smile | :)
 
Cheers. 5*s from me Smile | :)
GeneralUse of codememberszb_szb6 Aug '09 - 20:39 
Can I use your code in my commercial software?
GeneralUsing IntellisensememberMr. Badr CHOUFFAI15 Jul '09 - 4:37 
Hi.
Can you give us an exemple how to use Intellisense feature.
 
thanks in advance.
Generalthis is awesome!memberfirehak16 Apr '09 - 16:11 
this is great, I have to manage different router configurations at school, and creating an "intellisense" editor seemed like a great idea to me. Big Grin | :-D
QuestionCommercial use?membersanme9831 May '08 - 6:13 
Hi Marschills,
 
May I know is it I can use your code as source code editor for my commercial software?
 
Thank you.
GeneralI wonder if it would be OK...membertherearefartoomanybens11 Dec '07 - 12:28 
I'd like to borrow inspiration and a little code from your GListBox... if that would be alright?
 
I'm steadily expanding some more usable WinForm components, I will opensource and codeproject it when its finished - but an autocompleting RichEdit is a so-darn-useful component to have in the toolbox it'd be criminal not to elaborate on your work.
 
You should have P/Invoked GetCaretPos too silly!
 
... Roll eyes | :rolleyes: but I have done that for you hahaha...
 
CSharp-on-Rails here we go!
QuestionDo not work !memberYeurl_200719 Mar '07 - 23:53 
I m trying to play with the demo, after load the System.dll, nothing append after "."
 
Sample i wrote: System.
:/
 
I hva try this demo project on tree computer:
vista professional with vs2k5 sp1
xp professional whith vs2k3
windows 2k pro whtih only runtime of .net framework 1.1 and 2.0
 
The problem still here after compiling the project... please help !
 
Yeurl
AnswerRe: Do not work !memberWouter Demuynck3 May '07 - 23:30 
If the "." on your keyboard requires the shift key to be pressed, try just hitting the key without shift. I think the code assumes qwerty layout.
QuestionHow to fix the tab key not being handled..memberillium13 Dec '06 - 20:26 
regarding the known bug the author lists, it's a simple matter to fix it..
 
in richTextBox1_KeyDown, add the line:
 
e.SuppressKeyPress = true;
 
after
 
e.Handled = true;
 
and bingo no more annoying extra tab.
 
kudos for a nice looking demo.
 
_illium

 
-----
"It's 5:50 a.m., Do you know where your stack pointer is?"

AnswerRe: How to fix the tab key not being handled..memberVegio2 Feb '09 - 3:01 
Or even
 
if (e.KeyCode == Keys.Tab)
{
    if (this.listBoxAutoComplete.Visible)
        e.SuppressKeyPress = true;
}
 
if ( e.KeyData == Keys.OemPeriod )
{
	// The amazing dot key

	if ( !this.listBoxAutoComplete.Visible)
	{
		// Display the member listview if there are
		// items in it
		if ( populateListBox() )
		{
			//this.listBoxAutoComplete.SelectedIndex = 0;

			// Find the position of the caret
			Point point = this.richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
			point.Y += (int) Math.Ceiling(this.richTextBox1.Font.GetHeight()) + 2;
			point.X += 2; // for Courier, may need a better method

			this.statusBar1.Text = point.X + "," + point.Y;
			this.listBoxAutoComplete.Location = point;
			this.listBoxAutoComplete.BringToFront();
			this.listBoxAutoComplete.Show();
		}
	}
	else
	{
        if (this.listBoxAutoComplete.SelectedIndex > -1)
        {
            selectItem();
            this.listBoxAutoComplete.Visible = false;
            richTextBox1_KeyDown(sender, e);
        }
		typed = "";
	}
	else if ( e.KeyCode == Keys.Up )
	{
 
in the beginning of the _KeyDown handler. This way, intended tabs don't get suppressed. Also, when pressing dot again while a listitem is highlit, it inserts it via the recursive call...
 

Vegio
QuestionClippingmemberCyclonicEvent10 Nov '06 - 23:44 
Hi. Thanks for sharing a great piece of work. Have you seen any clipping of the icons to the left of the text within the auto complete boxes? Any ideas on how best to solve?
 
Thanks
AnswerRe: ClippingmemberCyclonicEvent11 Nov '06 - 4:03 
Okay, so I've found a way around this. Probably not the best method, but I have simply added a line to the GListBox constructor increasing the list box line height as below:-
 
this.ItemHeight = this.ItemHeight + 5
 

Generalintellisense for dbmemberrizomatosa16 Oct '06 - 7:43 
great works! thanks to share!
Answare about db usage:
Can i think to use this code with a special dll where i stored database informations (table, columns etc..)?
an idea how build this dll?
 
thanks&ciao
General=)memberChris Nevill14 Jan '06 - 6:19 
This is a really interesting project.
Any news of that control?!
 
I'm currently writing a C# script plugin for a media center application (www.jrmediacenter.com)
I'm very short of time, so don't have much time to look into this
but it would be very useful if the plugin could do intellisense!
 
I've just tried to run your sample, however it falls over
at line 223 of Form Main with a security Exception?
Any ideas?
Cheers
Chris
GeneralRe: =)membersmallguy7814 Nov '06 - 0:23 
I'm afraid I've really busy doing an MCAD and CS degree at present (and a full time job) so I've had to put my pet coding projects on hold for now.
 
Can you quote the security exception?
GeneralRe: =)memberChris Nevill14 Nov '06 - 0:39 
Thanks for your reply even if it was 10 months late =)
It's a long time since I've looked at this.
I'll have to look into it again. I've got a feeling I did get
it up and running eventually.
 
Cheers
Chris
GeneralCapturing tabmembersmallguy7824 Mar '05 - 6:09 
I'll update this article and my others over the summer months when it's raining(sorry, too busy at the moment), to try to make this into a control. In the meantime, here's how you can implement tab autocomplete with the richtextbox:
 

// Change richTextBox1 so it is a TabCaptureRichTextBox
public class TabCaptureRichTextBox : RichTextBox
{
public event CancelEventHandler TabPressed;
private const int WM_KEYDOWN = 0x100;

protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, Keys keyData)
{
if ( this.TabPressed != null )
{
if ( msg.Msg == WM_KEYDOWN )
{
if ( keyData == Keys.Tab )
{
CancelEventArgs e = new CancelEventArgs();
this.TabPressed(this,e);

// Will this work when asynchronously called?
return e.Cancel;
}
}
}

return base.ProcessCmdKey (ref msg, keyData);
}
}
 

// Inside FormMain class definition...
 
// Inside InitializeComponent()
this.richTextBox1.TabPressed += new System.ComponentModel.CancelEventHandler(this.toBoxTo_TabPressed);
 

private void richTextBox1_TabPressed(object sender, System.ComponentModel.CancelEventArgs e)
{
if ( this.listBoxAutoComplete.Visible )
{
this.selectItem();
e.Cancel = true;
}
}

GeneralThanksmemberdanielprc22 Mar '05 - 4:00 
I do need this.

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 13 Jan 2004
Article Copyright 2004 by yetanotherchris
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid