Adding intellisense to a textbox

So i was assigned to do this task so i though i should write about it for my future refernece as well.

So to start with i created a simple form where you need to enter the name and the country of a person so the UI will look something like this.

form_design

Here i have added two textboxes along with two list boxes that we will use as intellisence. If you notice two tiny rectangles by the end of the text boxes these are the list boxes so i made them super tiny so that they dont mess my design view and i set them to be hidden when the form loads. You can place them any where in the deisgn view as they will be resized later on.

After that you need this class to be included in you project i found it online but cant remember its reference so if you know it mail me and i will add the reference

class ClsIntelliSense
{
[DllImport("user32")]
private extern static int GetCaretPos(out Point p);

public static void AutoCompleteTextBox(TextBox txtControl, ListBox lstControl, List<string> lstAutoCompleteList, KeyEventArgs e)
{
Point cp;
GetCaretPos(out cp);
List<string> lstTemp = new List<string>();
lstControl.SetBounds(cp.X + txtControl.Left, cp.Y + txtControl.Top + 20, 150, 50);

var TempFilteredList = lstAutoCompleteList.Where(n => n.StartsWith(GetLastString(txtControl.Text).ToLower())).Select(r => r);

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

if (e.KeyCode == Keys.Down)
{
lstControl.SelectedIndex = 0;
lstControl.Focus();
e.Handled = true;
}
else if (e.KeyCode == Keys.Up)
{
lstControl.SelectedIndex = lstControl.Items.Count - 1;
lstControl.Focus();
e.Handled = true;
}

txtControl.KeyPress += (s, kpeArgs) =>
{

if (kpeArgs.KeyChar == (char)Keys.Enter)
{
if (lstControl.Visible == true)
{
lstControl.Focus();
}
kpeArgs.Handled = true;
}
else if (kpeArgs.KeyChar == (char)Keys.Escape)
{
lstControl.Visible = false;
kpeArgs.Handled = true;
}
};

lstControl.KeyUp += (s, kueArgs) =>
{
if (kueArgs.KeyCode == Keys.Enter)
{
string StrLS = GetLastString(txtControl.Text);
int LIOLS = txtControl.Text.LastIndexOf(StrLS);
string TempStr = txtControl.Text.Remove(LIOLS);
txtControl.Text = TempStr + ((ListBox)s).SelectedItem.ToString();
txtControl.Select(txtControl.Text.Length, 0);
txtControl.Focus();
lstControl.Hide();
}
else if (kueArgs.KeyCode == Keys.Escape)
{
lstControl.Hide();
txtControl.Focus();
}
};

}
private static string GetLastString(string s)
{
string[] strArray = s.Split(' ');
return strArray[strArray.Length - 1];
}

}

 

So when this is done i have added these line to the KeyUp event of textboxes.

So for country the code will be like this

var DictionaryList = new List<string>(new string[] { "INDIA", "PAKISTAN", "UK", "USA","FRANCE" }.ToList());
DictionaryList = DictionaryList.Select(x => x.ToLower()).ToList();
ClsIntelliSense.AutoCompleteTextBox(textBoxCountry, listBoxCountry, DictionaryList, e);

and for name the code will be like this

var DictionaryList = new List<string>(new string[] { "ASAD", "ALI", "IRFAN", "MIRZA"}.ToList());
DictionaryList = DictionaryList.Select(x => x.ToLower()).ToList();
ClsIntelliSense.AutoCompleteTextBox(textBoxName, listBoxName, DictionaryList, e);

now when you execute the code you will be able to see the intellisence and you should be able to navigate as well.

name_inteli   country inteli