


Introduction
In this article we will create a Code Completion window in C#.
Intelligent code completion is a context-aware code completion feature in some programming environments that speeds up the process of coding applications by reducing typos and other common mistakes. Attempts to do this are usually done through auto completion popups when typing, querying parameters of functions, query hints related to syntax errors, etc. Intelligent code completion and related tools serve as documentation and disambiguation for variable names, functions and methods using reflection.
Intelligent code completion appears in many program environments, an example implementation being Visual Studio's IntelliSense. The term was originally popularized as "picklist" and some implementations still refer to it as such.
How hard it is to build a code completion in C#?
I searched on the internet, I found no code instead of developed editors.
I knew that it is the list that can be shown while typing the code, but how to display that list onto the RichTextBox?
Before working on this code I thought it is so difficult to develop this, it needs so much professionalism, but I was wrong, it depends on your idea.
How is it so easy, Just create a ListBox object and add it to RichTextBox and get selected item from list box and insert it to the RichTextBox and then remove that ListBox.
You just need to Add or Remove items from list box.
You can customize your code completion window (ListBox) on your own (as shown in above image 3).
We will also complete the brackets automatically further (see AutoCompleteBrackets function)
In this article we will create simple code completion in C#. Download the code to see the code completion using XML.
It is nothing but the adding or removing ListBox.
See next algorithm for better understanding.
For understanding, read all comments carefully in the code (CCRichTextBox).
Algorithm to create a Code COmpletion in C#
- Create the ListBox object.(I am using CodeCompleteBox object name).
- Read X-Y coordinates from RichTextBox.
- Declare the Boolean variable to identify whether the CodeCompleteBox is added or not.(I am using isCodeCompleteBoxAdded variable) & String variable to identify the complete string(i am using EnteredKey variable).
- Declare array list of keywords.(I am using keywordslist).
- Add following events to RichTextBox
1. Key Press event in C#
- Identify pressed key is alphabet or not.
- Remove all items from CodeCompleteBox.
- Read each item from keywordslist.
- If each item from keywordslist is starts with pressed key character then add that item into the CodeCompleteBox.
- Read each item from keywordslist.
- If each item from keywordslist is starts with pressed key then set that item to selected.
- Set Default cursor to CodeCompleteBox.
- Set Size to CodeCompleteBox.
- Set Location to CodeCompleteBox by reading (x,y) coordinates from Step-2.
- Add CodeCompleteBox to RichTextBox.
- Set isCodeCompleteBoxAdded to true.
2. Text Changed event in C#
If RichTextBox Text is null then remove CodeCompleteBox from RichTextBox.Before removing first check if isCodeCompleteBoxAdded is true.
3. Key Down event in C#
- Check if Space,Enter,Escape & Back key is down then go to next;
- If isCodeCompleteBoxAdded is true then remove CodeCompleteBox from RichTextBox.
4. Mouse Click event in C#
If isCodeCompleteBoxAdded is true then remove CodeCompleteBox from RichTextBox.
5. VScroll event in C#
If isCodeCompleteBoxAdded is true then remove CodeCompleteBox from RichTextBox.
Now, Add the following events to CodeCompleteBox.
1. Key Down in C#
- Check if isCodeCompleteBoxAdded is true then go to next.
- If Enter or Space key is down then go to next.
- Read Selected item from CodeCompleteBox and insert that item in RichTextBox at SelectionStart position.
- Remove CodeCompleteBox from RichTextBox.
2. Key Press
It is used to insert a pressed character into RichTextBox and also select item in CodeCompleteBox.
3. Mouse Click in C#
Same as above Key Down event Step-3.
Note. Here, to display a tooltip or to display information about selected item of CodeCompleteBox we will use label to display information and will add this label to a Panel and this panel will be added to RichTextBox at specific location or next location to CodeCompleteBox. In this article we will not create a tooltip but download the source it contain the whole code with tooltips. All operations are same as CodeCompleteBox only adding Key Up event to CodeCompleteBox.
Example
I am creating a class CCRichTextBox with super class RichTextBox.
Firstly, lets see some important functions.
I used a function ProcessCodeCompletionAction(String key) to call whenever the key press event is generated in the program. I am reading the getWidth() & getHeight() function to set Width & Height to CodeCompleteBox but you can set them as default. Concat the pressed character with EnteredKey, perform all steps defined in Key Press in above algorithm.
Here we will not see adding of tooltip to CodeCompleteBox, so if you get errors then remove all components of tooltips.
public void ProcessCodeCompletionAction(string key)
{
string EnteredKey = "";
// Concatenate the key & EnteredKey postfix
EnteredKey = EnteredKey + key;
char ch;
// Check if the pressed key on CCRichTextBox is a lowercase alphabet or not
for (ch = 'a'; ch <= 'z'; ch++)
{
if (key == ch.ToString())
{
// Clear the CodeCompleteBox items
CodeCompleteBox.Items.Clear();
// Add each item to CodeCompleteBox
foreach (string item in keywordslist)
{
// Check if the item starts with EnteredKey or not
if (item.StartsWith(EnteredKey))
{
CodeCompleteBox.Items.Add(item);
}
}
// Read each item from CodeCompleteBox to set SelectedItem
foreach (string item in keywordslist)
{
if (item.StartsWith(EnteredKey))
{
CodeCompleteBox.SelectedItem = item;
// Set default cursor to CodeCompleteBox
CodeCompleteBox.Cursor = Cursors.Default;
// Set size to CodeCompleteBox
// Width = this.Width & Height = this.Height + (int)this.Font.Size
CodeCompleteBox.Size = new System.Drawing.Size(this.Width, this.Height + (int)this.Font.Size);
// Set location to CodeCompleteBox by calling getXYPoints() function
CodeCompleteBox.Location = this.getXYPoints();
// Adding controls of CodeCompleteBox to CCRichTextBox
this.Controls.Add(CodeCompleteBox);
// Set focus to CodeCompleteBox
CodeCompleteBox.Focus();
// Set isCodeCompleteBoxAdded to true
isCodeCompleteBoxAdded = true;
break;
}
else
{
isCodeCompleteBoxAdded = false;
}
}
}
// Check if pressed key character is an uppercase letter or not
else if (key == ch.ToString().ToUpper())
{
// Clear the CodeCompleteBox items
CodeCompleteBox.Items.Clear();
// Add each item to CodeCompleteBox
foreach (string item in keywordslist)
{
// Check if the item starts with EnteredKey or not
if (item.StartsWith(EnteredKey))
{
CodeCompleteBox.Items.Add(item);
}
}
// Read each item from CodeCompleteBox to set SelectedItem
foreach (string item in keywordslist)
{
if (item.StartsWith(EnteredKey))
{
CodeCompleteBox.SelectedItem = item;
// Set default cursor to CodeCompleteBox
CodeCompleteBox.Cursor = Cursors.Default;
// Set size to CodeCompleteBox
// Width = this.Width & Height = this.Height + (int)this.Font.Size
CodeCompleteBox.Size = new System.Drawing.Size(this.Width, this.Height + (int)this.Font.Size);
// Set location to CodeCompleteBox by calling getXYPoints() function
CodeCompleteBox.Location = this.getXYPoints();
// Adding controls of CodeCompleteBox to CCRichTextBox
this.Controls.Add(CodeCompleteBox);
// Set focus to CodeCompleteBox
CodeCompleteBox.Focus();
// Set isCodeCompleteBoxAdded to true
isCodeCompleteBoxAdded = true;
break;
}
else
{
isCodeCompleteBoxAdded = false;
}
}
}
}
}


Faheem ChishtiPosted Mar 22, 2021, 12:36 PM
Very nice and awesome work
Pritam ZopePosted Feb 10, 2016, 7:07 AM
Thanks Vivek & Ehsan
Ehsan SajjadPosted Feb 8, 2016, 7:25 AM
Very good work, appreciated
Vivek KumarPosted Feb 4, 2016, 2:38 PM
Nice article
Pritam ZopePosted Feb 1, 2016, 2:07 AM
Thanks..........
Arul RPosted Jan 31, 2016, 11:06 AM
Nice share
Gowtham KPosted Jan 31, 2016, 12:20 AM
Great Work, Thanks for sharing:)
Raja TPosted Jan 29, 2016, 10:17 AM
Nice, Thanks for sharing
Santhakumar MunuswamyPosted Jan 29, 2016, 1:54 AM
Thanks for nice article
Shubham KumarPosted Jan 29, 2016, 1:36 AM
NICE KEEP SHARE