AutoComplete ComboBox with DroppedDown=true



Hi
I have create an autocomplete combobox in VS.NET 1.1 based on some
articles I found on the net. Every thing works fine except when you
turn on DroppedDown=true to make the combo behave like an IE
autocomplete combobox. The combox seems to be working fine but when you
try to get combobox.Text or combobox.SelectedIndex through code they
point to the previous selection instead of showing the value currently
selected. Any one figure out how to get around this issue ????

Below is the code. If you comment the line DroppedDown=true; everything
should work fine. But if you leave it there you can reproduce the above
problem.

public class ACComboBox : System.Windows.Forms.ComboBox
{
public ACComboBox():base()
{
}

protected override void
OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
DroppedDown=true;
if (char.IsControl(e.KeyChar))
return;

string ToFind = Text.Substring(0,SelectionStart) + e.KeyChar;
int Index = FindStringExact(ToFind);
if (Index == -1)
Index = FindString(ToFind);

if (Index == -1)
return;

SelectedIndex = Index;
SelectionStart = ToFind.Length;
SelectionLength = Text.Length - SelectionStart;
e.Handled = true;
base.OnKeyPress(e);
}

protected override void OnLeave(System.EventArgs e)
{
string text=Text;
int index=FindStringExact(text,-1);
if(index<0)
index=0;

SelectedIndex = index;
base.OnLeave(e);
}
}

.



Relevant Pages

  • Re: AutoComplete ComboBox with DroppedDown=true
    ... hours of struggling with this thing I finally stumbled on the solution. ... string ToFind = Text.Substring+ ... e.KeyChar; int Index = FindStringExact; ... SelectionStart = ToFind.Length; ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: AutoComplete ComboBox with DroppedDown=true
    ... I was unable to reproduce the problem using a simple form with your ACComboBox and a button. ... int Index = FindStringExact; ... SelectionLength = Text.Length - SelectionStart; ...
    (microsoft.public.dotnet.framework.windowsforms.controls)