Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

From a datareader..

 

myDropDownList.DataSource = mydatareader;

myDropDownList.DataTextField ="DataName"

myDropDownList.DataValueField = "DataValue";

myDropDownList.Databind():

  • Administrators
Posted

try

[DefaultProperty("AutoCompleteEnabled"),
DefaultEvent("SelectedIndexChanged")]
public class AutoCompleteComboBox :	ComboBox
{
	
	/// 
	/// Event raised if a match isn't found and 
	/// is set to true
	/// 
	public event CancelEventHandler MatchNotFound;
 
	///	
	///	Internal value to track	if we should bother	auto completing	or not
	///	
	private	bool _AutoCompleteEnabled =	true;
	///	
	///	Internal value to track	if we should limit to list or not.
	///	
	private	bool _LimitToList =	true;

	///	
	///	Enable or disable autocomplete
	///	
	[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);
	}
}

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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);

}

brown m+m, green m+m, it all comes out the same in the end
Posted

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.

Posted
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!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...