Switch to full style
Project under GPL source codes are posted here
Post a reply

AutoComplete TextBox

Fri Nov 07, 2008 1:15 pm

 Project Name:   AutoComplete TextBox
 Programmer:   pfemiani
 Type:   Application
 Technology:  C#
 IDE:   NONE
 Description:   I was in need of a simple auto-completing textbox for a project I was working on at work when I realized that there were really not a whole lot of good choices for this. Many of them derive from a combo box which will not work properly for a dropdown list that continues to filter out items as you continue to type

csharp code
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using Femiani.Forms.UI.Input;

namespace AutoCompleteTextBox.Demo
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private CoolTextBox coolTextBox1;
private System.Windows.Forms.Button Done;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

this.coolTextBox1.Text = string.Empty;

// Add some sample auto complete entry items...
this.coolTextBox1.Items.Add(new AutoCompleteEntry("Phoenix, Az", "Phoenix, Az", "Az", "PHX"));
this.coolTextBox1.Items.Add(new AutoCompleteEntry("Tempe, Az", "Tempe, Az", "Az", "TPE"));
this.coolTextBox1.Items.Add(new AutoCompleteEntry("Chandler, Az", "Chandler, Az", "Az", "CHA"));
this.coolTextBox1.Items.Add(new AutoCompleteEntry("Boxford, Ma", "Boxford, Ma", "Ma", "BXF"));
this.coolTextBox1.Items.Add(new AutoCompleteEntry("Topsfield, Ma", "Topsfield, Ma", "Ma", "TPF"));
this.coolTextBox1.Items.Add(new AutoCompleteEntry("Danvers, Ma", "Danvers, Ma", "Ma", "DNV"));

this.Validate();

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.coolTextBox1 = new Femiani.Forms.UI.Input.CoolTextBox();
this.Done = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// coolTextBox1
//
this.coolTextBox1.BackColor = System.Drawing.SystemColors.Window;
this.coolTextBox1.BorderColor = System.Drawing.Color.LightSteelBlue;
this.coolTextBox1.DockPadding.All = 4;
this.coolTextBox1.Location = new System.Drawing.Point(16, 8);
this.coolTextBox1.Name = "coolTextBox1";
this.coolTextBox1.PopupWidth = 208;
this.coolTextBox1.SelectedItemBackColor = System.Drawing.Color.LightSteelBlue;
this.coolTextBox1.SelectedItemForeColor = System.Drawing.SystemColors.ControlText;
this.coolTextBox1.Size = new System.Drawing.Size(216, 21);
this.coolTextBox1.TabIndex = 3;
this.coolTextBox1.Validating += new System.ComponentModel.CancelEventHandler(this.coolTextBox1_Validating);
//
// Done
//
this.Done.Location = new System.Drawing.Point(160, 32);
this.Done.Name = "Done";
this.Done.Size = new System.Drawing.Size(72, 23);
this.Done.TabIndex = 4;
this.Done.Text = "&Done";
this.Done.Click += new System.EventHandler(this.Done_Click);
//
// MainForm
//
this.AcceptButton = this.Done;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(250, 63);
this.Controls.Add(this.Done);
this.Controls.Add(this.coolTextBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "MainForm";
this.Text = "AutoComplete Demo";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}

private void coolTextBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (this.coolTextBox1.Text == string.Empty)
{
this.coolTextBox1.BorderColor = Color.Firebrick;
e.Cancel = true;
}
else
{
this.coolTextBox1.BorderColor = Color.LightSteelBlue;
}
}

private void Done_Click(object sender, System.EventArgs e)
{
this.Close();
}

}
}

AutoCompleteTextBox.jpg
AutoCompleteTextBox.jpg (9.74 KiB) Viewed 5211 times


Autocomplete BOX
csharp code
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Femiani.Forms.UI.Input
{
/// <summary>
/// Summary description for AutoCompleteTextBox.
/// </summary>
[Serializable]
public class AutoCompleteTextBox : TextBox
{

#region Classes and Structures

public enum EntryMode
{
Text,
List
}

/// <summary>
/// This is the class we will use to hook mouse events.
/// </summary>
private class WinHook : NativeWindow
{
private AutoCompleteTextBox tb;

/// <summary>
/// Initializes a new instance of <see cref="WinHook"/>
/// </summary>
/// <param name="tbox">The <see cref="AutoCompleteTextBox"/>
//the hook is running for.//</param>
public WinHook(AutoCompleteTextBox tbox)
{
this.tb = tbox;
}

/// <summary>
/// Look for any kind of mouse activity that is not in the
/// text box itself, and hide the popup if it is visible.
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case Win32.Messages.WM_LBUTTONDOWN:
case Win32.Messages.WM_LBUTTONDBLCLK:
case Win32.Messages.WM_MBUTTONDOWN:
case Win32.Messages.WM_MBUTTONDBLCLK:
case Win32.Messages.WM_RBUTTONDOWN:
case Win32.Messages.WM_RBUTTONDBLCLK:
case Win32.Messages.WM_NCLBUTTONDOWN:
case Win32.Messages.WM_NCMBUTTONDOWN:
case Win32.Messages.WM_NCRBUTTONDOWN:
{
// Lets check to see where the event took place
Form form = tb.FindForm();
Point p = form.PointToScreen(new Point((int)m.LParam));
Point p2 = tb.PointToScreen(new Point(0, 0));
Rectangle rect = new Rectangle(p2, tb.Size);
// Hide the popup if it is not in the text box
if (!rect.Contains(p))
{
tb.HideList();
}
} break;
case Win32.Messages.WM_SIZE:
case Win32.Messages.WM_MOVE:
{
tb.HideList();
} break;
// This is the message that gets sent when a childcontrol gets activity
case Win32.Messages.WM_PARENTNOTIFY:
{
switch ((int)m.WParam)
{
case Win32.Messages.WM_LBUTTONDOWN:
case Win32.Messages.WM_LBUTTONDBLCLK:
case Win32.Messages.WM_MBUTTONDOWN:
case Win32.Messages.WM_MBUTTONDBLCLK:
case Win32.Messages.WM_RBUTTONDOWN:
case Win32.Messages.WM_RBUTTONDBLCLK:
case Win32.Messages.WM_NCLBUTTONDOWN:
case Win32.Messages.WM_NCMBUTTONDOWN:
case Win32.Messages.WM_NCRBUTTONDOWN:
{
// Same thing as before
Form form = tb.FindForm();
Point p = form.PointToScreen(new Point((int)m.LParam));
Point p2 = tb.PointToScreen(new Point(0, 0));
Rectangle rect = new Rectangle(p2, tb.Size);
if (!rect.Contains(p))
{
tb.HideList();
}
} break;
}
} break;
}

base.WndProc (ref m);
}
}

#endregion

#region Members

private ListBox list;
protected Form popup;
private AutoCompleteTextBox.WinHook hook;

#endregion

#region Properties

private AutoCompleteTextBox.EntryMode mode =

EntryMode.Text;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public AutoCompleteTextBox.EntryMode Mode
{
get
{
return this.mode;
}
set
{
this.mode = value;
}
}

private AutoCompleteEntryCollection items =

new AutoCompleteEntryCollection();
[System.ComponentModel.Editor

(typeof(AutoCompleteEntryCollection.AutoCompleteEntryCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public AutoCompleteEntryCollection Items
{
get
{
return this.items;
}
set
{
this.items = value;
}
}

private AutoCompleteTriggerCollection triggers = new AutoCompleteTriggerCollection();
[System.ComponentModel.Editor(typeof(AutoCompleteTriggerCollection.AutoCompleteTriggerCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public AutoCompleteTriggerCollection Triggers
{
get
{
return this.triggers;
}
set
{
this.triggers = value;
}
}

[Browsable(true)]
[Description("The width of the popup (-1 will auto-size the popup to the width of the textbox).")]
public int PopupWidth
{
get
{
return this.popup.Width;
}
set
{
if (value == -1)
{
this.popup.Width = this.Width;
}
else
{
this.popup.Width = value;
}
}
}

public BorderStyle PopupBorderStyle
{
get
{
return this.list.BorderStyle;
}
set
{
this.list.BorderStyle = value;
}
}

private Point popOffset = new Point(12, 0);
[Description("The popup defaults to the lower left edge of the textbox.")]
public Point PopupOffset
{
get
{
return this.popOffset;
}
set
{
this.popOffset = value;
}
}

private Color popSelectBackColor = SystemColors.Highlight;
public Color PopupSelectionBackColor
{
get
{
return this.popSelectBackColor;
}
set
{
this.popSelectBackColor = value;
}
}

private Color popSelectForeColor = SystemColors.HighlightText;
public Color PopupSelectionForeColor
{
get
{
return this.popSelectForeColor;
}
set
{
this.popSelectForeColor = value;
}
}

private bool triggersEnabled = true;
protected bool TriggersEnabled
{
get
{
return this.triggersEnabled;
}
set
{
this.triggersEnabled = value;
}
}

[Browsable(true)]
public override string Text
{
get
{
return base.Text;
}
set
{
this.TriggersEnabled = false;
base.Text = value;
this.TriggersEnabled = true;
}
}

#endregion

public AutoCompleteTextBox()
{
// Create the form that will hold the list
this.popup = new Form();
this.popup.StartPosition = FormStartPosition.Manual;
this.popup.ShowInTaskbar = false;
this.popup.FormBorderStyle = FormBorderStyle.None;
this.popup.TopMost = true;
this.popup.Deactivate += new EventHandler(Popup_Deactivate);

// Create the list box that will hold mathcing items
this.list = new ListBox();
this.list.Cursor = Cursors.Hand;
this.list.BorderStyle = BorderStyle.None;
this.list.SelectedIndexChanged += new EventHandler(List_SelectedIndexChanged);
this.list.MouseDown += new MouseEventHandler(List_MouseDown);
this.list.ItemHeight = 14;
this.list.DrawMode = DrawMode.OwnerDrawFixed;
this.list.DrawItem += new DrawItemEventHandler(List_DrawItem);
this.list.Dock = DockStyle.Fill;

// Add the list box to the popup form
this.popup.Controls.Add(this.list);

// Add default triggers.
this.triggers.Add(new TextLengthTrigger(2));
this.triggers.Add(new ShortCutTrigger(Keys.Enter, TriggerState.SelectAndConsume));
this.triggers.Add(new ShortCutTrigger(Keys.Tab, TriggerState.Select));
this.triggers.Add(new ShortCutTrigger(Keys.Control | Keys.Space, TriggerState.ShowAndConsume));
this.triggers.Add(new ShortCutTrigger(Keys.Escape, TriggerState.HideAndConsume));
}

protected virtual bool DefaultCmdKey(ref Message msg, Keys keyData)
{
bool val = base.ProcessCmdKey (ref msg, keyData);

if (this.TriggersEnabled)
{
switch (this.Triggers.OnCommandKey(keyData))
{
case TriggerState.ShowAndConsume:
{
val = true;
this.ShowList();
} break;
case TriggerState.Show:
{
this.ShowList();
} break;
case TriggerState.HideAndConsume:
{
val = true;
this.HideList();
} break;
case TriggerState.Hide:
{
this.HideList();
} break;
case TriggerState.SelectAndConsume:
{
if (this.popup.Visible == true)
{
val = true;
this.SelectCurrentItem();
}
} break;
case TriggerState.Select:
{
if (this.popup.Visible == true)
{
this.SelectCurrentItem();
}
} break;
default:
break;
}
}

return val;
}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Up:
{
this.Mode = EntryMode.List;
if (this.list.SelectedIndex > 0)
{
this.list.SelectedIndex--;
}
return true;
} break;
case Keys.Down:
{
this.Mode = EntryMode.List;
if (this.list.SelectedIndex < this.list.Items.Count - 1)
{
this.list.SelectedIndex++;
}
return true;
} break;
default:
{
return DefaultCmdKey(ref msg, keyData);
} break;
}
}

protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged (e);

if (this.TriggersEnabled)
{
switch (this.Triggers.OnTextChanged(this.Text))
{
case TriggerState.Show:
{
this.ShowList();
} break;
case TriggerState.Hide:
{
this.HideList();
} break;
default:
{
this.UpdateList();
} break;
}
}
}

protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus (e);

if (!(this.Focused || this.popup.Focused || this.list.Focused))
{
this.HideList();
}
}

protected virtual void SelectCurrentItem()
{
if (this.list.SelectedIndex == -1)
{
return;
}

this.Focus();
this.Text = this.list.SelectedItem.ToString();
if (this.Text.Length > 0)
{
this.SelectionStart = this.Text.Length;
}

this.HideList();
}

protected virtual void ShowList()
{
if (this.popup.Visible == false)
{
this.list.SelectedIndex = -1;
this.UpdateList();
Point p = this.PointToScreen(new Point(0,0));
p.X += this.PopupOffset.X;
p.Y += this.Height + this.PopupOffset.Y;
this.popup.Location = p;
if (this.list.Items.Count > 0)
{
this.popup.Show();
if (this.hook == null)
{
this.hook = new WinHook(this);
this.hook.AssignHandle(this.FindForm().Handle);
}
this.Focus();
}
}
else
{
this.UpdateList();
}
}

protected virtual void HideList()
{
this.Mode = EntryMode.Text;
if (this.hook != null)
this.hook.ReleaseHandle();
this.hook = null;
this.popup.Hide();
}

protected virtual void UpdateList()
{
object selectedItem = this.list.SelectedItem;

this.list.Items.Clear();
this.list.Items.AddRange(this.FilterList(this.Items).ToObjectArray());

if (selectedItem != null &&
this.list.Items.Contains(selectedItem))
{
EntryMode oldMode = this.Mode;
this.Mode = EntryMode.List;
this.list.SelectedItem = selectedItem;
this.Mode = oldMode;
}

if (this.list.Items.Count == 0)
{
this.HideList();
}
else
{
int visItems = this.list.Items.Count;
if (visItems > 8)
visItems = 8;

this.popup.Height = (visItems * this.list.ItemHeight) + 2;
switch (this.BorderStyle)
{
case BorderStyle.FixedSingle:
{
this.popup.Height += 2;
} break;
case BorderStyle.Fixed3D:
{
this.popup.Height += 4;
} break;
case BorderStyle.None:
default:
{
} break;
}

this.popup.Width = this.PopupWidth;

if (this.list.Items.Count > 0 &&
this.list.SelectedIndex == -1)
{
EntryMode oldMode = this.Mode;
this.Mode = EntryMode.List;
this.list.SelectedIndex = 0;
this.Mode = oldMode;
}

}
}

protected virtual AutoCompleteEntryCollection FilterList(AutoCompleteEntryCollection list)
{
AutoCompleteEntryCollection newList = new AutoCompleteEntryCollection();
foreach (IAutoCompleteEntry entry in list)
{
foreach (string match in entry.MatchStrings)
{
if (match.ToUpper().StartsWith(this.Text.ToUpper()))
{
newList.Add(entry);
break;
}
}
}
return newList;
}

private void List_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.Mode != EntryMode.List)
{
SelectCurrentItem();
}
}

private void List_MouseDown(object sender, MouseEventArgs e)
{
for (int i=0; i<this.list.Items.Count; i++)
{
if (this.list.GetItemRectangle(i).Contains(e.X, e.Y))
{
this.list.SelectedIndex = i;
this.SelectCurrentItem();
}
}
this.HideList();
}

private void List_DrawItem(object sender, DrawItemEventArgs e)
{
Color bColor = e.BackColor;
if (e.State == DrawItemState.Selected)
{
e.Graphics.FillRectangle(new SolidBrush(this.PopupSelectionBackColor), e.Bounds);
e.Graphics.DrawString(this.list.Items[e.Index].ToString(),

e.Font, new SolidBrush(this.PopupSelectionForeColor), e.Bounds, StringFormat.GenericDefault);
}
else
{
e.DrawBackground();
e.Graphics.DrawString(this.list.Items[e.Index].ToString(),

e.Font, new SolidBrush(e.ForeColor), e.Bounds, StringFormat.GenericDefault);
}
}

private void Popup_Deactivate(object sender, EventArgs e)
{
if (!(this.Focused || this.popup.Focused || this.list.Focused))
{
this.HideList();
}
}

}
}



Attachments
AutoCompleteTextBox_demo.zip
(174.59 KiB) Downloaded 1507 times
AutoCompleteTextBox_src.zip
(174.59 KiB) Downloaded 1714 times

Re: AutoComplete TextBox

Fri Mar 18, 2011 9:06 am

thank you for sharing the project...

can i use it for website project? can you show me how i can use it for web application?


thank you very much....

Regards,

Re: AutoComplete TextBox

Sun Jan 20, 2013 9:37 pm

updated.

Post a reply
  Related Posts  to : AutoComplete TextBox
 how to show hashmap value in a textbox     -  
 Disable TextBox html by php     -  
 add data from form1 textbox to form2 combo box     -  
 calling method to form- displaying textbox data in messagbox     -  

Topic Tags

C# Projects