Intellisense: An Example of How to Make It


Overview

Auto completing is one way to offer a productive and programmer friendly user interface to programmers and Intellisense feature of Visual Studio .NET is one of them. This example shows you an example of how to implement a feature like that.

In fact when we press comma, the editor gets a drop-down list of words we can use. This example is built with a MS SQL Server 2000, using ADO.NET

Description

First of all you have to copy the Intellisense.mdf and Intellisense.log in your database folder.

In this example, I used a textbox and listbox object. The first thing is to have a connection to the database. In this case I used MS SQL Server 2000, using ADO.NET. obviously the choice of what kind of data provider to use is extremely easy: SQLProvider. So the first step is to declare the SQLConnection and the SQLCommand. Then you need to set them: in this case I did it in the InizializeComponent. Lets have a look at the code:

//
// conn
//
this.conn.ConnectionString = "data source=rohan;initial catalog=Intellisense;integrated security=SSPI;persist security" + " info=False;workstation id=" + Environment.MachineName + ";packet size=" + conn.PacketSize;
//
// cmd
//
this.cmd.Connection = this.conn; 
           

the connectionstring is really important, dont forget to change datasource=rohan with datasource = YourServerMachine. 

Now, have look at the GUI:

 

when you start typing: 

 

so, the code is all in the KeyPress event.

Source Code

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// if you press any keys differently than backspace then
// str is increased by a char
string str = "";
if (e.KeyChar != Convert.ToChar(Keys.Back))
str = textBox1.Text + e.KeyChar.ToString();
//if you press backspace we need to remove the ascii code from the str
else
{
if (textBox1.Text.Length > 0)
str = textBox1.Text.Remove(textBox1.Text.Length-1,1);
}
if (str.Trim() != "")
{
System.Data.SqlClient.SqlDataReader rdr;
try
{
cmd.CommandText = "sp_Comuni_LikeComune " +
this.CheckStringa(str);
conn.Open();
rdr = cmd.ExecuteReader();
listBox1.Items.Clear();
while (rdr.Read())
listBox1.Items.Add (rdr.GetSqlString(0).ToString().Trim());
if (listBox1.Items.Count > 0)
listBox1.Visible =
true;
else
listBox1.Visible = false;
}
catch (System.Exception caught)
{
MessageBox.Show(caught.Message);
}
finally
{
conn.Close();
}
}
}


Similar Articles