Click here to Skip to main content
12,629,493 members (35,974 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

20.3K views
889 downloads
22 bookmarked
Posted

IntelliSense TextBox in C#

, 10 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "IntelliSense TextBox in C#"

Introduction

Taking as base from IntelliSense TextBox in C#, created by Anand Gunasekaran, I decided to create a new user control inherithed from TextBox that provides automatically an IntelliSense-Like function without calling any method.

Using the code

Building the control

The new control must have two important components: a list of strings for containing all the words we want for autocomplete and a ListBox that will show all the words in the list.

List<string> dictionary;
ListBox listbox;

Then, let's make a constructor for the class that allow us put the control in the UI designer and, of course, initialize the inner ListBox in the new IntelliSenseTextBox.

public IntelliSenseTextBox() : base()
{
    listbox = new ListBox();
    listbox.Parent = this;
    listbox.KeyUp += OnKeyUp;
    listbox.Visible = false;
    this.dictionary = new List<string>();
}

After, we will override some methods of the TextBox, specifically OnKeyPress and OnTextChanged

protected override void OnKeyPress(KeyPressEventArgs e)
{
    base.OnKeyPress(e);
    if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Down)
    {
        if (listbox.Visible == true)
        {
            listbox.Focus();
        }
        e.Handled = true;
    }
    else if (e.KeyChar == (char)Keys.Escape)
    {
        listbox.Visible = false;
        e.Handled = true;
    }
}

protected override void OnTextChanged(EventArgs e)
{
    base.OnTextChanged(e);
    Point cp;
    GetCaretPos(out cp);
    List<string> lstTemp = new List<string>();

    listbox.SetBounds(cp.X + this.Left, cp.Y + this.Top + 20, 150, 50);
    var TempFilteredList = dictionary.Where(
             n => n.StartsWith(GetLastString(this.Text).ToUpper())).Select(r => r);

    lstTemp = TempFilteredList.ToList<string>();
    if (lstTemp.Count != 0 && GetLastString(this.Text) != "")
    {
        listbox.DataSource = lstTemp;
        listbox.Show();
    }
    else
    {
        listbox.Hide();
    }
}

Finally we must create the method to handle the KeyUp of the ListBox

private void OnKeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        string StrLS = GetLastString(this.Text);
        int LIOLS = this.Text.LastIndexOf(StrLS);
        string TempStr = this.Text.Remove(LIOLS);
        this.Text = TempStr + ((ListBox)sender).SelectedItem.ToString();
        this.Select(this.Text.Length, 0);
        listbox.Hide();
    }
}

Using the control

Now our IntelliSenseTextBox is complete but, we need to specify which words will be in the dictionary

public partial class Form1 : Form
{
    List<string> ISList = new List<string>(new string[]{ 
       "SELECT", 
       "CREATE", 
       "TABLE", 
       "JOB", 
       "INFO", 
       "SOLUTIONS", 
       "GOOGLE", 
       "VISUAL STUDIO" 
    });

    public Form1()
    {
        InitializeComponent();
        this.intelliSenseTextBox1.Dictionary = ISList;
    }
}

Points of Interest

When I saw the work of Anand Gunasekaran I was very interested in the topic so I decided to create this control for my school projects, specifically for a compiler in which a friend and I are working.

License

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

Share

About the Author

Alain Peralta
Student
Mexico Mexico
Great things come from rare ideas. Wink | ;)

You may also be interested in...

Comments and Discussions

 
QuestionA screenshot would be nice! Pin
Dmitri Nеstеruk10-Mar-14 1:07
memberDmitri Nеstеruk10-Mar-14 1:07 
AnswerRe: A screenshot would be nice! Pin
thatraja10-Mar-14 1:21
protectorthatraja10-Mar-14 1:21 
AnswerRe: A screenshot would be nice! Pin
Alain Peralta10-Mar-14 11:27
memberAlain Peralta10-Mar-14 11:27 
Questionthere got a bug Pin
Cosmic_Spy9-Mar-14 7:16
memberCosmic_Spy9-Mar-14 7:16 
AnswerRe: there got a bug Pin
Alain Peralta9-Mar-14 7:30
memberAlain Peralta9-Mar-14 7:30 
GeneralRe: I found solution! Pin
zidanetan20-Aug-15 18:27
memberzidanetan20-Aug-15 18:27 

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.161205.3 | Last Updated 10 Mar 2014
Article Copyright 2014 by Alain Peralta
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid