Autocomplete combobox with dropdownstyle set to dropdownlist

G

Guest

Hi,

I have seen many autocomplete comboboxes but with dropdownstyle set to
dropdown.
But i want an autocomplete combobox with dropdownstle set to DropDownList.
Any help will be appreciated.

Thanks in advance.

regards,
Venu.
 
W

Wiktor Zychla

I have seen many autocomplete comboboxes but with dropdownstyle set to
dropdown.
But i want an autocomplete combobox with dropdownstle set to DropDownList.
Any help will be appreciated.

there is no edit area in the combo when dropdownstyle=DropDownList.
how do you imagine the autocompletion then?
 
G

Guest

by auto completion i mean't...

Capturing multiple key strokes and accordingly higlight the corresponding
list item.

For example if there are two items:
bangalore
bombay

When I press b..it should select bangalore
ba...bangalore
bo...bombay

I hope that's understood.
 
S

Sebastian Pusz

I also needed such a combobox so I wrote it by my self. I was using VS2005
Beta 1 (.NET 2.0) and there is a combobox with autocompletition feature, so
I only change it a little. May be it's not a final version but it works, and
now I don't have much time to develope it. If this code will be usefull to
you, it's yours. I would be greatefull for any changes in the code.

How to use it:
set DropDownStyle to DropDown (not DropDownLitst!!)
set AutoCompleteMode to Suggest
set AutoCompleteSource to ListItems
and
set the RestrictToDropDownList to true

and it should works.

Regards
Sebastian Pusz

/// <summary>
/// Extended ComboBox control. Additional features:
/// FocusedBackColor
/// </summary>
[ToolboxItem(true), ToolboxBitmap(typeof(ComboBox))]
public class ComboBoxEx : System.Windows.Forms.ComboBox
{
private bool restrictToDropDownList = false;
private Color backColor;
private Color focusedBackColor = SystemColors.Window;
private String lastText = String.Empty;
private Int32 lastSelectionStart = 0;
private Int32 lastSelectionLength = 0;

/// <summary>
/// Gets or sets if control disallow to enter the text that does not
exists in the drop down list
/// </summary>
/// <value><c>true</c> if control should disallow to enter the text that
dose not exists in the drop down list, otherwise <c>false</c>.</value>
[DefaultValue(false), Description("Allow to enter the text only if it
exists in the drop down list.")]
public bool RestrictToDropDownList
{
get
{
return restrictToDropDownList && (this.DropDownStyle ==
ComboBoxStyle.DropDown);
}

set
{
restrictToDropDownList = value && (this.DropDownStyle ==
ComboBoxStyle.DropDown);
}
}

/// <summary>
/// Gets or sets the background color for the control when it has focus.
/// </summary>
/// <value>A <see cref="Color"/> that represents the background color of
the control when it has focus. The default is the value of the <see
cref="SystemColors.Window"/> property.</value>
[Category("Appearance"), DefaultValue(typeof(Color), "Window"),
Description("Specifies the back color when control has got focus.")]
public Color FocusedBackColor
{
get
{
return focusedBackColor;
}

set
{
focusedBackColor = value;
}
}

/// <summary>
/// Raises the GotFocus event.
/// </summary>
/// <param name="e">An EventArgs that contains the event data.</param>
protected override void OnGotFocus(EventArgs e)
{
backColor = BackColor;
BackColor = focusedBackColor;
base.OnGotFocus(e);
}

/// <summary>
/// Raises the KeyDown event.
/// </summary>
/// <param name="e">A KeyEventArgs that contains the event data.</param>
protected override void OnKeyDown(KeyEventArgs e)
{
if (RestrictToDropDownList)
{
lastText = Text;
lastSelectionStart = this.SelectionStart;
lastSelectionLength = this.SelectionLength;
}

base.OnKeyDown(e);
}

/// <summary>
/// Raises the TextChanged event.
/// </summary>
/// <param name="e">A KeyEventArgs that contains the event data.</param>
protected override void OnTextChanged(EventArgs e)
{
if (RestrictToDropDownList)
{
Int32 index = FindString(Text);
if (index == -1)
{
Text = lastText;
this.SelectionStart = lastSelectionStart;
this.SelectionLength = lastSelectionLength;
}
}
base.OnTextChanged(e);
}

/// <summary>
/// Raises the LostFocus event.
/// </summary>
/// <param name="e">An EventArgs that contains the event data.</param>
protected override void OnLostFocus(EventArgs e)
{
BackColor = backColor;
base.OnLostFocus(e);
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top