autofill a dropdown list

From a datareader..

myDropDownList.DataSource = mydatareader;
myDropDownList.DataTextField ="DataName"
myDropDownList.DataValueField = "DataValue";
myDropDownList.Databind():
 
try
C#:
	[DefaultProperty("AutoCompleteEnabled"),
	DefaultEvent("SelectedIndexChanged")]
	public class AutoCompleteComboBox :	ComboBox
	{
		
		/// <summary>
		/// Event raised if a match isn't found and <c LimitToList></c>
		/// is set to true
		/// </summary>
		public event CancelEventHandler MatchNotFound;
  
		///	<summary>
		///	Internal value to track	if we should bother	auto completing	or not
		///	</summary>
		private	bool _AutoCompleteEnabled =	true;
		///	<summary>
		///	Internal value to track	if we should limit to list or not.
		///	</summary>
		private	bool _LimitToList =	true;

		///	<summary>
		///	Enable or disable autocomplete
		///	</summary>
		[System.ComponentModel.Category("Behavior"), 
		System.ComponentModel.Description("Enables or disables the autocompletion functionality")]
		public virtual bool	AutoCompleteEnabled
		{
			get	{return	_AutoCompleteEnabled;}
			set	{_AutoCompleteEnabled =	value;}
		}

		protected override void	OnValidating(CancelEventArgs e)
		{
			if (LimitToList)
			{
				int	i	= this.FindStringExact(Text);
			
				if (i	== -1)
				{
					OnNotInList(e);
				}
				else
				{
					SelectedIndex = i;
				}
			}
   
			base.OnValidating(e);
		}
		
  
		[Category("Behavior"), 
		System.ComponentModel.Description("Limits the selection	to items in	the	list.")]
		public bool	LimitToList
		{
			get	{ return _LimitToList; }
			set	{ _LimitToList = value;	}
		}
  

		protected virtual void OnNotInList(CancelEventArgs e)
		{
			if (MatchNotFound != null)
			{
				MatchNotFound(this,	e);
			}
		}	
  
		protected override void OnKeyPress(KeyPressEventArgs e)
		{
			string currentText = Text;
			int i;

            if (e.KeyChar == Convert.ToChar(Keys.Back) )
			{
                if(Text.Length == 0)
				{return;} //Don't backspace when nothing there
                Text = Text.Substring(0, SelectionStart - 1);
                currentText = Text;
				SelectionStart=Text.Length;
			}																 
		

            i = FindString(currentText);

			if( i >= 0 )
			{
				SelectedIndex = i;
				Select (currentText.Length, Text.Length);
			}
		base.OnKeyPress (e);
		}
	}
 
Thanks for the help.

I tried this but I get errors because there is no ComboBox... do I have to include a certain library?

Wouldn't you want to inherit from a DropDownList for this instead?

Thanks.
 
Whoops the one I posted would only work for a windows app - the older article you refered to in your PM was also for a windows control - I didn't look at the forum before I posted :-\

To do this in a web browser would probably require some client side javascript to work.
 
when i'm filling a ddl it's usually as follows:

ListItem litemp;

while(sqlDR.Read()){
string strText, strValue;
// set the strings to whatever
litemp = new ListItem(strText, strValue);
ddlDropdownlist.Items.Add(litemp);
}
 
Sorry, I should re-ohrase my question. I didn't mean filling a dropdown list with database entries - I use a databind for that...

I would like to have a dropdownlist that the user can type into and instead of going to the entry of the first letter match, it would try to match the string the user is typing...flike the FAT client drop down.

Does anyone know where the code is for the ASP.NET dropdownlist to select the item with the same first letter? Maybe I could just alter that.
 
PlausiblyDamp said:
Whoops the one I posted would only work for a windows app - the older article you refered to in your PM was also for a windows control - I didn't look at the forum before I posted :-\

To do this in a web browser would probably require some client side javascript to work.

Sorry - my bad!
 
Back
Top