Introduction

We all use VisualStudio's autocomplete menu, aka IntelliSense. This is very useful, is not it? Unfortunately, .NET FW does not contain built-in component for autocomplete menu. The proposed control fills this gap.

AutocompleteMenu allows you to easily embed a drop-down hints into any TextBox or RichTextBox on your form.

Implementation

The component contains several classes. Below summarizes the main classes and their functions:

AutocompleteMenu - main component of which contains the basic functions. It subscribes to the events of TextBox, looks for suitable variants, shows a drop-down menu and inserts new text into textbox.

MinFragmentLength - Minimum fragment length for menu appearing. AutocompleteMenu appears only if current fragment around caret has no less then MinFragmentLength symbols.

SearchPattern - Regex pattern for serach fragment around caret.

AutocompleteMenuHost - visual component that derived from ToolStripDropDown. This control allows you to display the menu without losing focus on the main form.

AutocompleteListView - visual component that inherited from UserControl. With GDI+, it draws the items of the drop-down menu. This component is similar to ListView but it allows you to display a large number of elements with a good performance.

AutocompleteItem - menu item. This class contains all the necessary information about menu item. You can extend menu features, inheriting your elements from AutocompleteItem and overriding its virtual methods. Below are basic properties of AutocompleteItem:

Text - text for inserting into textbox.

MenuText - this text will displayed in popup menu.

ImageIndex - index of image for this item.

ToolTipTitle - title for tooltip. If ToolTipTitle is null, tooltip will not appear for this item.

ToolTipText - text of tooltip.

Tag - you can store any data here.

Below are some methods you can override:

GetTextForReplace - returns inserting text. You can dynamically change inserting text. For example, you can insert current date.

Compare - this method defines will this item visible in menu or not. By default this method make visible only items begin with given fragment. But you can override this behaviour. For example, you can compare by substring, or make fuzzy comparison.

OnSelected - this method is called when text was inserted in textbox. You can make here some additional operations with text. For example, you can move caret into some position.

The library also contains a few useful classes derived from AutocompleteItem: SnippetAutocompleteItem (can be used for inserting of code snippets), MethodAutocompleteItem (can be used for inserting of method name after dot), SubstringAutocompleteItem (compares text by substring), MulticolumnAutocompleteItem (draws multicolumn menu).

Using the code

Easy usage:

1) Throw AutocompleteMenu component on your form.

2) Type text of the menu items in AutocompleteMenu.Items:

3) Set the AutocompleteMenu property of your TextBox:

4) That's all folks

Advanced usage:

1) Throw AutocompleteMenu component on your form.

2) Create a list of items, and add to the menu, using the methods SetAutocompleteItems() or AddItem(). For example:

Shortcuts:

Enter, Tab, DblClick on the item - inserts selected item into text (Tab works only if AllowTabKey is true).

Esc - closes menu.

Note that these keys are working, despite the fact that the focus is in the textbox.

Item's tooltip appears if you click on the item.

Custom ListView

You can use the custom controls for displaying AutocompleteMenu (like ListView, ListBox, DataGridView, TreeView, etc). For this, create own control (derived from Control) and implement interface IAutocompleteListView. More details see in CustomListViewSample.

Dynamic context menu

It is frequently necessary that the menu display not a fixed set of items, but was changing dynamically, depending on the text. This task can be solved.

Note that method SetAutocompleteItems() of menu takes IEnumerable as collection of displayed items.

So, you can do not generate a list of items at the start of the program, and generate it dynamically at the time of the call the enumerator from the menu.

Good stuff 5* ... but I can't seem to get it working the way that I want it

... My list items all start with "[" ... for eg: "[Title]"
... so my pattern match is: [[\w.] ..
but it only prompts me if there is a work break character before hand eg:
These work:Title:[
Title [
Where this does not (no prompt):Title[

How can I get it prompting whenever the user types [ regardless of what is before it?

I have a suggestion though, I had a hard time adding a support for "Append" feature which default autocomplete provides. It'd be nice if you can take care of it. I can send you code snippet if you're interested. Please may write to me @ rmp|AT|outlook|DOT|in.

I think we should make it work without the timer as well. It is kind of cranky with timer sometimes.

The mouse scrollwheel is not responding in an autocomplete list. Or rather: it is possible to use the scrollwheel if you first click on the scrollbar's down arrow and then press keyboard arrow down. It would be great to be able to use the wheel directly once the autocomplete list is shown.